View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.log;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import javax.annotation.Nullable;
36  import org.slf4j.Logger;
37  import org.slf4j.Marker;
38  import org.spf4j.base.Arrays;
39  
40  /**
41   *
42   * @author Zoltan Farkas
43   */
44  public final class LogUtils {
45  
46    private LogUtils() {
47    }
48  
49    @SuppressFBWarnings({"SA_LOCAL_SELF_COMPARISON", "SF_SWITCH_FALLTHROUGH"})
50    public static void logUpgrade(final Logger log, @Nullable final Marker marker, final Level level,
51            final String format, final Object... pargs) {
52      Object[] args = pargs;
53      switch (level) {
54        case TRACE:
55          if (log.isTraceEnabled()) {
56            if (marker == null) {
57              log.trace(format, args);
58            } else {
59              log.trace(marker, format, args);
60            }
61            break;
62          } else {
63            if (args == pargs) {
64              args = Arrays.append(args, LogAttribute.origLevel(level));
65            }
66          }
67        case DEBUG:
68          if (log.isDebugEnabled()) {
69            if (marker == null) {
70              log.debug(format, args);
71            } else {
72              log.debug(marker, format, args);
73            }
74            break;
75          } else {
76            if (args == pargs) {
77              args = Arrays.append(args, LogAttribute.origLevel(level));
78            }
79          }
80        case INFO:
81          if (log.isInfoEnabled()) {
82            if (marker == null) {
83              log.info(format, args);
84            } else {
85              log.info(marker, format, args);
86            }
87            break;
88          } else {
89            if (args == pargs) {
90              args = Arrays.append(args, LogAttribute.origLevel(level));
91            }
92          }
93        case WARN:
94          if (log.isWarnEnabled()) {
95            if (marker == null) {
96              log.warn(format, args);
97            } else {
98              log.warn(marker, format, args);
99            }
100           break;
101         } else {
102           if (args == pargs) {
103             args = Arrays.append(args, LogAttribute.origLevel(level));
104           }
105         }
106       case ERROR:
107         if (log.isErrorEnabled()) {
108           if (marker == null) {
109             log.error(format, args);
110           } else {
111             log.error(marker, format, args);
112           }
113           break;
114         } else {
115           throw new IllegalStateException("Error not enabled for  " + log.getName());
116         }
117       default:
118         throw new IllegalStateException("Invalid level " + level);
119     }
120   }
121 
122   @SuppressFBWarnings({"SA_LOCAL_SELF_COMPARISON", "SF_SWITCH_FALLTHROUGH"})
123   public static void logUpgrade(final java.util.logging.Logger log, final Level plevel,
124           final String format, final Object... pargs) {
125     Level[] values = Level.values();
126     Level level = plevel;
127     while (true) {
128       java.util.logging.Level julLevel = level.getJulLevel();
129       if (log.isLoggable(julLevel)) {
130         log.log(julLevel, format, pargs);
131         return;
132       }
133       if (level == Level.ERROR) {
134         throw new IllegalStateException();
135       }
136       level = values[level.ordinal() + 1];
137     }
138   }
139 }