1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.jspf;
21
22 import org.apache.james.jspf.core.Logger;
23
24 /**
25 * Logger sending everything to the standard output streams.
26 * This is mainly for the cases when you have a utility that
27 * does not have a logger to supply.
28 */
29 public final class ConsoleLogger implements Logger {
30
31 /** Typecode for debugging messages. */
32 public static final int LEVEL_DEBUG = 0;
33
34 /** Typecode for informational messages. */
35 public static final int LEVEL_INFO = 1;
36
37 /** Typecode for warning messages. */
38 public static final int LEVEL_WARN = 2;
39
40 /** Typecode for error messages. */
41 public static final int LEVEL_ERROR = 3;
42
43 /** Typecode for fatal error messages. */
44 public static final int LEVEL_FATAL = 4;
45
46 /** Typecode for disabled log levels. */
47 public static final int LEVEL_DISABLED = 5;
48
49 private final int m_logLevel;
50
51 /**
52 * Current logger path.
53 */
54 private String m_path;
55
56 /**
57 * Creates a new ConsoleLogger with the priority set to DEBUG.
58 */
59 public ConsoleLogger()
60 {
61 this( LEVEL_DEBUG , "ROOT");
62 }
63
64 /**
65 * Creates a new ConsoleLogger.
66 * @param logLevel log level typecode
67 */
68 public ConsoleLogger( final int logLevel, final String path )
69 {
70 m_logLevel = logLevel;
71 m_path = path;
72 }
73
74 /**
75 * Logs a debugging message.
76 *
77 * @param message a <code>String</code> value
78 */
79 public void debug( final String message )
80 {
81 debug( message, null );
82 }
83
84 /**
85 * Logs a debugging message and an exception.
86 *
87 * @param message a <code>String</code> value
88 * @param throwable a <code>Throwable</code> value
89 */
90 public void debug( final String message, final Throwable throwable )
91 {
92 if( m_logLevel <= LEVEL_DEBUG )
93 {
94 System.out.print( "[DEBUG] " );
95 System.out.print( m_path+" " );
96 System.out.println( message );
97
98 if( null != throwable )
99 {
100 throwable.printStackTrace( System.out );
101 }
102 }
103 }
104
105 /**
106 * Returns <code>true</code> if debug-level logging is enabled, false otherwise.
107 *
108 * @return <code>true</code> if debug-level logging
109 */
110 public boolean isDebugEnabled()
111 {
112 return m_logLevel <= LEVEL_DEBUG;
113 }
114
115 /**
116 * Logs an informational message.
117 *
118 * @param message a <code>String</code> value
119 */
120 public void info( final String message )
121 {
122 info( message, null );
123 }
124
125 /**
126 * Logs an informational message and an exception.
127 *
128 * @param message a <code>String</code> value
129 * @param throwable a <code>Throwable</code> value
130 */
131 public void info( final String message, final Throwable throwable )
132 {
133 if( m_logLevel <= LEVEL_INFO )
134 {
135 System.out.print( "[INFO] " );
136 System.out.print( m_path+" " );
137 System.out.println( message );
138
139 if( null != throwable )
140 {
141 throwable.printStackTrace( System.out );
142 }
143 }
144 }
145
146 /**
147 * Returns <code>true</code> if info-level logging is enabled, false otherwise.
148 *
149 * @return <code>true</code> if info-level logging is enabled
150 */
151 public boolean isInfoEnabled()
152 {
153 return m_logLevel <= LEVEL_INFO;
154 }
155
156 /**
157 * Logs a warning message.
158 *
159 * @param message a <code>String</code> value
160 */
161 public void warn( final String message )
162 {
163 warn( message, null );
164 }
165
166 /**
167 * Logs a warning message and an exception.
168 *
169 * @param message a <code>String</code> value
170 * @param throwable a <code>Throwable</code> value
171 */
172 public void warn( final String message, final Throwable throwable )
173 {
174 if( m_logLevel <= LEVEL_WARN )
175 {
176 System.out.print( "[WARNING] " );
177 System.out.print( m_path+" " );
178 System.out.println( message );
179
180 if( null != throwable )
181 {
182 throwable.printStackTrace( System.out );
183 }
184 }
185 }
186
187 /**
188 * Returns <code>true</code> if warn-level logging is enabled, false otherwise.
189 *
190 * @return <code>true</code> if warn-level logging is enabled
191 */
192 public boolean isWarnEnabled()
193 {
194 return m_logLevel <= LEVEL_WARN;
195 }
196
197 /**
198 * Logs an error message.
199 *
200 * @param message a <code>String</code> value
201 */
202 public void error( final String message )
203 {
204 error( message, null );
205 }
206
207 /**
208 * Logs an error message and an exception.
209 *
210 * @param message a <code>String</code> value
211 * @param throwable a <code>Throwable</code> value
212 */
213 public void error( final String message, final Throwable throwable )
214 {
215 if( m_logLevel <= LEVEL_ERROR )
216 {
217 System.out.print( "[ERROR] " );
218 System.out.print( m_path+" " );
219 System.out.println( message );
220
221 if( null != throwable )
222 {
223 throwable.printStackTrace( System.out );
224 }
225 }
226 }
227
228 /**
229 * Returns <code>true</code> if error-level logging is enabled, false otherwise.
230 *
231 * @return <code>true</code> if error-level logging is enabled
232 */
233 public boolean isErrorEnabled()
234 {
235 return m_logLevel <= LEVEL_ERROR;
236 }
237
238 /**
239 * Logs a fatal error message.
240 *
241 * @param message a <code>String</code> value
242 */
243 public void fatalError( final String message )
244 {
245 fatalError( message, null );
246 }
247
248 /**
249 * Logs a fatal error message and an exception.
250 *
251 * @param message a <code>String</code> value
252 * @param throwable a <code>Throwable</code> value
253 */
254 public void fatalError( final String message, final Throwable throwable )
255 {
256 if( m_logLevel <= LEVEL_FATAL )
257 {
258 System.out.print( "[FATAL ERROR] " );
259 System.out.print( m_path+" " );
260 System.out.println( message );
261
262 if( null != throwable )
263 {
264 throwable.printStackTrace( System.out );
265 }
266 }
267 }
268
269 /**
270 * Returns <code>true</code> if fatal-level logging is enabled, false otherwise.
271 *
272 * @return <code>true</code> if fatal-level logging is enabled
273 */
274 public boolean isFatalErrorEnabled()
275 {
276 return m_logLevel <= LEVEL_FATAL;
277 }
278
279 /**
280 * Just returns this logger (<code>ConsoleLogger</code> is not hierarchical).
281 *
282 * @param name ignored
283 * @return this logger
284 */
285 public Logger getChildLogger( final String name )
286 {
287 return new ConsoleLogger(m_logLevel, m_path+"."+name);
288 }
289 }