KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - Log4jLogAdapter.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 org.apache.log4j.*;
24 import java.util.Iterator JavaDoc;
25 import org.snmp4j.util.EnumerationIterator;
26
27 /**
28  * The <code>Log4jLogAdapter</code> implements a logging adapter for Log4J.
29  *
30  * @author Frank Fock
31  * @version 1.7
32  * @since 1.2.1
33  */

34 public class Log4jLogAdapter implements LogAdapter, Comparable JavaDoc {
35
36   private Logger logger;
37
38   /**
39    * Creates a Log4J log adapter from a Log4J Logger.
40    * @param logger
41    * the Log4J Logger to use with this adapter.
42    * @since 1.2.1
43    */

44   public Log4jLogAdapter(Logger logger) {
45     this.logger = logger;
46   }
47
48   /**
49    * Logs a debug message.
50    *
51    * @param message the message to log.
52    */

53   public void debug(Object JavaDoc message) {
54     logger.debug(message);
55   }
56
57   /**
58    * Logs an error message.
59    *
60    * @param message the message to log.
61    */

62   public void error(Object JavaDoc message) {
63     logger.error(message);
64   }
65
66   /**
67    * Logs an error message.
68    *
69    * @param message the message to log.
70    * @param throwable the exception that caused to error.
71    */

72   public void error(Object JavaDoc message, Throwable JavaDoc throwable) {
73     logger.error(message, throwable);
74   }
75
76   /**
77    * Logs an informational message.
78    *
79    * @param message the message to log.
80    */

81   public void info(Object JavaDoc message) {
82     logger.info(message);
83   }
84
85   /**
86    * Checks whether DEBUG level logging is activated for this log adapter.
87    *
88    * @return <code>true</code> if logging is enabled or <code>false</code>
89    * otherwise.
90    */

91   public boolean isDebugEnabled() {
92     return logger.isDebugEnabled();
93   }
94
95   /**
96    * Checks whether INFO level logging is activated for this log adapter.
97    *
98    * @return <code>true</code> if logging is enabled or <code>false</code>
99    * otherwise.
100    */

101   public boolean isInfoEnabled() {
102     return logger.isInfoEnabled();
103   }
104
105   /**
106    * Checks whether WARN level logging is activated for this log adapter.
107    *
108    * @return <code>true</code> if logging is enabled or <code>false</code>
109    * otherwise.
110    */

111   public boolean isWarnEnabled() {
112     return logger.isEnabledFor(Level.WARN);
113   }
114
115   /**
116    * Logs an warning message.
117    *
118    * @param message the message to log.
119    */

120   public void warn(Object JavaDoc message) {
121     logger.warn(message);
122   }
123
124   public void fatal(Object JavaDoc message) {
125     logger.fatal(message);
126   }
127
128   public void fatal(Object JavaDoc message, Throwable JavaDoc throwable) {
129     logger.fatal(message, throwable);
130   }
131
132   public void setLogLevel(LogLevel level) {
133     Level l;
134     switch (level.getLevel()) {
135       case LogLevel.LEVEL_OFF:
136         l = Level.OFF;
137         break;
138       case LogLevel.LEVEL_ALL:
139         l = Level.ALL;
140         break;
141       case LogLevel.LEVEL_TRACE:
142         l = Level.DEBUG;
143         break;
144       case LogLevel.LEVEL_DEBUG:
145         l = Level.DEBUG;
146         break;
147       case LogLevel.LEVEL_INFO:
148         l = Level.INFO;
149         break;
150       case LogLevel.LEVEL_WARN:
151         l = Level.WARN;
152         break;
153       case LogLevel.LEVEL_ERROR:
154         l = Level.ERROR;
155         break;
156       case LogLevel.LEVEL_FATAL:
157         l = Level.FATAL;
158         break;
159       default:
160         l = null;
161     }
162     logger.setLevel(l);
163   }
164
165   public Logger getLogger() {
166     return logger;
167   }
168
169   public String JavaDoc getName() {
170     return logger.getName();
171   }
172
173   public LogLevel getLogLevel() {
174     Level l = logger.getLevel();
175     return toLogLevel(l);
176   }
177
178   private LogLevel toLogLevel(Level l) {
179     if (l == null) {
180       return LogLevel.NONE;
181     }
182     switch (l.toInt()) {
183       case Level.OFF_INT:
184         return LogLevel.OFF;
185       case Level.ALL_INT:
186         return LogLevel.ALL;
187       case Level.DEBUG_INT:
188         return LogLevel.DEBUG;
189       case Level.INFO_INT:
190         return LogLevel.INFO;
191       case Level.WARN_INT:
192         return LogLevel.WARN;
193       case Level.ERROR_INT:
194         return LogLevel.ERROR;
195       case Level.FATAL_INT:
196         return LogLevel.FATAL;
197     }
198     return LogLevel.DEBUG;
199   }
200
201   public int compareTo(Object JavaDoc o) {
202     return getName().compareTo(((Log4jLogAdapter)o).getName());
203   }
204
205   public LogLevel getEffectiveLogLevel() {
206     Level l = logger.getEffectiveLevel();
207     return toLogLevel(l);
208   }
209
210   public Iterator JavaDoc getLogHandler() {
211     return new EnumerationIterator(logger.getAllAppenders());
212   }
213 }
214
Popular Tags