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 java.util.EnumMap;
35  import java.util.Map;
36  import org.spf4j.base.avro.LogLevel;
37  
38  
39  /**
40   * An enum for log levels
41   * @author Zoltan Farkas
42   */
43  public enum Level {
44    TRACE(java.util.logging.Level.FINEST),
45    DEBUG(java.util.logging.Level.FINE),
46    INFO(java.util.logging.Level.INFO),
47    WARN(java.util.logging.Level.WARNING),
48    ERROR(java.util.logging.Level.SEVERE),
49    OFF(java.util.logging.Level.OFF);
50  
51  
52    static final class Lazy {
53      private static final Map<Level, LogLevel> CONVERT_MAP = new EnumMap<>(Level.class);
54      static {
55        CONVERT_MAP.put(TRACE, LogLevel.TRACE);
56        CONVERT_MAP.put(DEBUG, LogLevel.DEBUG);
57        CONVERT_MAP.put(INFO, LogLevel.INFO);
58        CONVERT_MAP.put(WARN, LogLevel.WARN);
59        CONVERT_MAP.put(ERROR, LogLevel.ERROR);
60        CONVERT_MAP.put(OFF, LogLevel.UNKNOWN);
61      }
62    }
63  
64    private final java.util.logging.Level julLevel;
65  
66    Level(final java.util.logging.Level julLevel) {
67      this.julLevel = julLevel;
68    }
69  
70    public int getIntValue() {
71      return julLevel.intValue();
72    }
73  
74    public java.util.logging.Level getJulLevel() {
75      return julLevel;
76    }
77  
78    public LogLevel getAvroLevel() {
79      return Lazy.CONVERT_MAP.get(this);
80    }
81  
82    public static Level fromAvroLevel(final LogLevel level) {
83      switch (level) {
84        case UNKNOWN:
85        case DEBUG:
86          return Level.DEBUG;
87        case ERROR:
88          return Level.ERROR;
89        case INFO:
90          return Level.INFO;
91        case TRACE:
92          return Level.TRACE;
93        case WARN:
94          return Level.WARN;
95        default:
96          throw new IllegalArgumentException("Unsupported LogLevel " + level);
97      }
98    }
99  
100 
101   public static Level fromJulLevel(final int severity) {
102      if (severity <= TRACE.getIntValue()) {
103           return TRACE;
104     } else if (severity <= DEBUG.getIntValue()) {
105       return DEBUG;
106     } else if (severity <= INFO.getIntValue()) {
107       return INFO;
108     } else if (severity <= WARN.getIntValue()) {
109       return WARN;
110     } else if (severity <= ERROR.getIntValue()) {
111       return ERROR;
112     } else {
113       return OFF;
114     }
115   }
116 
117 }
118