KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > log > JavaLogAdapter


1 /*_############################################################################
2   _##
3   _## SNMP4J - JavaLogAdapter.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.log;
22
23 import java.util.Arrays JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import org.snmp4j.log.LogAdapter;
29 import org.snmp4j.log.LogLevel;
30
31 /**
32  * The <code>JavaLogAdapter</code> log adapter provides logging for SNMP4J
33  * through the Java logging (<code>java.util.logging</code>).
34  *
35  * @author Fabian Nart
36  * @author Frank Fock
37  * @version 1.8
38  * @since 1.7.2
39  */

40 public class JavaLogAdapter implements LogAdapter {
41
42   private final Logger JavaDoc logger;
43
44   public JavaLogAdapter(Logger JavaDoc logger) {
45     this.logger = logger;
46   }
47
48   // ---- Checking methods
49

50   public boolean isDebugEnabled() {
51     return isLoggable(LogLevel.DEBUG);
52   }
53
54   public boolean isInfoEnabled() {
55     return isLoggable(LogLevel.INFO);
56   }
57
58   public boolean isWarnEnabled() {
59     return isLoggable(LogLevel.WARN);
60   }
61
62   // ---- Logging methods
63

64   public void debug(Object JavaDoc message) {
65     log(LogLevel.DEBUG, message.toString(), null);
66   }
67
68   public void info(Object JavaDoc message) {
69     log(LogLevel.INFO, message.toString(), null);
70   }
71
72   public void warn(Object JavaDoc message) {
73     log(LogLevel.WARN, message.toString(), null);
74   }
75
76   public void error(Object JavaDoc message) {
77     log(LogLevel.ERROR, message.toString(), null);
78   }
79
80   public void error(Object JavaDoc message, Throwable JavaDoc t) {
81     log(LogLevel.ERROR, message.toString(), t);
82   }
83
84   public void fatal(Object JavaDoc message) {
85     log(LogLevel.FATAL, message.toString(), null);
86   }
87
88   public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
89     log(LogLevel.FATAL, message.toString(), t);
90   }
91
92   // ---- Public methods
93

94   public LogLevel getEffectiveLogLevel() {
95     return fromJavaToSnmp4jLevel(logger.getLevel());
96   }
97
98   public Iterator JavaDoc getLogHandler() {
99     return Arrays.asList(logger.getHandlers()).iterator();
100   }
101
102   public LogLevel getLogLevel() {
103     return getEffectiveLogLevel();
104   }
105
106   public String JavaDoc getName() {
107     return logger.getName();
108   }
109
110   public void setLogLevel(LogLevel logLevel) {
111     logger.setLevel(fromSnmp4jToJdk(logLevel));
112   }
113
114   // ---- Private methods
115

116   private boolean isLoggable(LogLevel logLevel) {
117     return logger.isLoggable(fromSnmp4jToJdk(logLevel));
118   }
119
120   private void log(LogLevel logLevel, String JavaDoc msg, Throwable JavaDoc t) {
121     logger.log(fromSnmp4jToJdk(logLevel), msg, t);
122   }
123
124   /**
125    * Mapping from <code>org.snmp4j.log.LogLevel</code> to
126    * <code>java.util.logging.Level</code>.
127    *
128    * @param logLevel
129    * The <code>LogLevel</code> to mapped
130    * @return the <code>Level</code>
131    * mapped to or <code>null</code> if
132    * <code>null</code> was specified as the parameter.
133    */

134   private static Level JavaDoc fromSnmp4jToJdk(LogLevel logLevel) {
135     if (logLevel == null) {
136       return null;
137     }
138     switch (logLevel.getLevel()) {
139       case LogLevel.LEVEL_ALL:
140         return Level.ALL;
141       case LogLevel.LEVEL_DEBUG:
142         return Level.FINE;
143       case LogLevel.LEVEL_TRACE:
144         return Level.FINEST;
145       case LogLevel.LEVEL_INFO:
146         return Level.INFO;
147       case LogLevel.LEVEL_WARN:
148         return Level.WARNING;
149       case LogLevel.LEVEL_ERROR:
150         return Level.SEVERE;
151       case LogLevel.LEVEL_FATAL:
152         return Level.SEVERE;
153       case LogLevel.LEVEL_OFF:
154         return Level.OFF;
155       case LogLevel.LEVEL_NONE:
156         return Level.OFF;
157       default:
158         throw new IllegalArgumentException JavaDoc(
159             "Mapping not defined from SNMP4J level " + logLevel
160             + " to Java logging level");
161     }
162   }
163
164   /**
165    * Mapping from <code>java.util.logging.Level</code> to
166    * <code>org.snmp4j.log.LogLevel</code>.
167    *
168    * @param level
169    * The <code>Level</code> to mapped
170    * @return
171    * the <code>LogLevel</code> mapped to or <code>null</code> if
172    * <code>null</code> was specified as the parameter.
173    */

174   private static LogLevel fromJavaToSnmp4jLevel(Level JavaDoc level) {
175     if (level == null) {
176       return null;
177     }
178     else if (Level.ALL.equals(level)) {
179       return LogLevel.ALL;
180     }
181     else if (Level.SEVERE.equals(level)) {
182       return LogLevel.FATAL;
183     }
184     else if (Level.WARNING.equals(level)) {
185       return LogLevel.WARN;
186     }
187     else if (Level.INFO.equals(level)) {
188       return LogLevel.INFO;
189     }
190     else if (Level.CONFIG.equals(level)) {
191       return LogLevel.DEBUG;
192     }
193     else if (Level.FINE.equals(level)) {
194       return LogLevel.DEBUG;
195     }
196     else if (Level.FINER.equals(level)) {
197       return LogLevel.TRACE;
198     }
199     else if (Level.FINEST.equals(level)) {
200       return LogLevel.TRACE;
201     }
202     else if (Level.OFF.equals(level)) {
203       return LogLevel.DEBUG;
204     }
205     else {
206       throw new IllegalArgumentException JavaDoc("Mapping not defined from Java level "
207                                          + level.getName() +
208                                          " to SNMP4J logging level");
209     }
210   }
211 }
212
Popular Tags