KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - ConsoleLogAdapter.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.Iterator JavaDoc;
24 import java.util.Collections JavaDoc;
25
26 /**
27  * The <code>ConsoleLogAdapter</code> provides simple logging to the console.
28  *
29  * @author Frank Fock
30  * @version 1.6.1
31  * @since 1.6
32  */

33 public class ConsoleLogAdapter implements LogAdapter {
34
35   private static boolean debugEnabled = false;
36   private static boolean infoEnabled = true;
37   private static boolean warnEnabled = true;
38
39   public ConsoleLogAdapter() {
40   }
41
42   /**
43    * Logs a debug message.
44    *
45    * @param message the message to log.
46    */

47   public void debug(Object JavaDoc message) {
48     if (debugEnabled) {
49       System.out.println(message.toString());
50     }
51   }
52
53   /**
54    * Logs an error message.
55    *
56    * @param message the message to log.
57    */

58   public void error(Object JavaDoc message) {
59     System.err.println(message.toString());
60   }
61
62   /**
63    * Logs an error message.
64    *
65    * @param message the message to log.
66    * @param throwable the exception that caused to error.
67    */

68   public void error(Object JavaDoc message, Throwable JavaDoc throwable) {
69     System.err.println(message.toString());
70   }
71
72   /**
73    * Logs a fatal message.
74    *
75    * @param message the message to log.
76    */

77   public void fatal(Object JavaDoc message) {
78     System.err.println(message.toString());
79   }
80
81   /**
82    * Logs a fatal message.
83    *
84    * @param message the message to log.
85    * @param throwable the exception that caused to error.
86    */

87   public void fatal(Object JavaDoc message, Throwable JavaDoc throwable) {
88     System.err.println(message.toString());
89   }
90
91   /**
92    * Logs an informational message.
93    *
94    * @param message the message to log.
95    */

96   public void info(Object JavaDoc message) {
97     if (infoEnabled) {
98       System.out.println(message.toString());
99     }
100   }
101
102   /**
103    * Checks whether DEBUG level logging is activated for this log adapter.
104    *
105    * @return <code>true</code> if logging is enabled or <code>false</code>
106    * otherwise.
107    */

108   public boolean isDebugEnabled() {
109     return debugEnabled;
110   }
111
112   /**
113    * Checks whether INFO level logging is activated for this log adapter.
114    *
115    * @return <code>true</code> if logging is enabled or <code>false</code>
116    * otherwise.
117    */

118   public boolean isInfoEnabled() {
119     return infoEnabled;
120   }
121
122   /**
123    * Checks whether WARN level logging is activated for this log adapter.
124    *
125    * @return <code>true</code> if logging is enabled or <code>false</code>
126    * otherwise.
127    */

128   public boolean isWarnEnabled() {
129     return warnEnabled;
130   }
131
132   /**
133    * Logs an warning message.
134    *
135    * @param message the message to log.
136    */

137   public void warn(Object JavaDoc message) {
138     if (warnEnabled) {
139       System.out.println(message.toString());
140     }
141   }
142
143   public static void setDebugEnabled(boolean isDebugEnabled) {
144     debugEnabled = isDebugEnabled;
145   }
146
147   public static void setWarnEnabled(boolean isWarnEnabled) {
148     warnEnabled = isWarnEnabled;
149   }
150
151   public static void setInfoEnabled(boolean isInfoEnabled) {
152     infoEnabled = isInfoEnabled;
153   }
154
155   public void setLogLevel(LogLevel level) {
156     debugEnabled = false;
157     warnEnabled = false;
158     infoEnabled = false;
159     switch (level.getLevel()) {
160       case LogLevel.LEVEL_ALL:
161         debugEnabled = true;
162         warnEnabled = true;
163         infoEnabled = true;
164         break;
165       case LogLevel.LEVEL_TRACE:
166       case LogLevel.LEVEL_DEBUG:
167         debugEnabled = true;
168         break;
169       case LogLevel.LEVEL_INFO:
170         infoEnabled = true;
171         break;
172       case LogLevel.LEVEL_WARN:
173         warnEnabled = true;
174         break;
175       default:
176     }
177   }
178
179   public String JavaDoc getName() {
180     return "";
181   }
182
183   public LogLevel getLogLevel() {
184     if (debugEnabled) {
185       return LogLevel.DEBUG;
186     }
187     else if (infoEnabled) {
188       return LogLevel.INFO;
189     }
190     else if (warnEnabled) {
191       return LogLevel.WARN;
192     }
193     return LogLevel.OFF;
194   }
195
196   public LogLevel getEffectiveLogLevel() {
197     return getLogLevel();
198   }
199
200   public Iterator JavaDoc getLogHandler() {
201     return Collections.EMPTY_LIST.iterator();
202   }
203 }
204
Popular Tags