1 /*
2 ~ Copyright 2006-2007 Nicolas De Loof.
3 ~
4 ~ Licensed under the Apache License, Version 2.0 (the "License");
5 ~ you may not use this file except in compliance with the License.
6 ~ You may obtain a copy of the License at
7 ~
8 ~ http://www.apache.org/licenses/LICENSE-2.0
9 ~
10 ~ Unless required by applicable law or agreed to in writing, software
11 ~ distributed under the License is distributed on an "AS IS" BASIS,
12 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ~ See the License for the specific language governing permissions and
14 ~ limitations under the License.
15 */
16 package org.jmonit.log;
17
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20
21 /**
22 * <code>Logger</code> implementation that write long to console (System.out).
23 *
24 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
25 */
26 public class ConsoleLogger
27 extends Log
28 {
29
30 /** Only errors */
31 private static final int ERROR = 2;
32
33 /** warnings */
34 private static final int WARN = 3;
35
36 /** Information messages */
37 private static final int INFO = 4;
38
39 /** debug */
40 private static final int DEBUG = 5;
41
42 /** Logging class */
43 private Class caller;
44
45 /** System property for setting logging level */
46 public static final String LEVEL = "jmonit.logger.level";
47
48 /** Loging level */
49 private static int level = WARN;
50
51 static
52 {
53 String prop = System.getProperty( LEVEL );
54 if ( prop != null )
55 {
56 level = Integer.parseInt( prop );
57 }
58 }
59
60 /**
61 * {@inheritDoc}
62 *
63 * @see info.jmonit.logger.Log#isDebugEnabled()
64 */
65 public boolean isDebugEnabled()
66 {
67 return level >= DEBUG;
68 }
69
70 /**
71 * Constructeur
72 *
73 * @param caller target logging class
74 */
75 public ConsoleLogger( Class caller )
76 {
77 super();
78 this.caller = caller;
79 }
80
81 /**
82 * {@inheritDoc}
83 *
84 * @see info.jmonit.logger.Log#debug(java.lang.String)
85 */
86 public void debug( String message )
87 {
88 if ( level >= DEBUG )
89 {
90 log( DEBUG, message, null );
91 }
92 }
93
94 /**
95 * {@inheritDoc}
96 *
97 * @see info.jmonit.logger.Log#info(java.lang.String)
98 */
99 public void info( String message )
100 {
101 if ( level >= INFO )
102 {
103 log( INFO, message, null );
104 }
105 }
106
107 /**
108 * {@inheritDoc}
109 *
110 * @see info.jmonit.logger.Log#info(java.lang.String, Throwable)
111 */
112 public void info( String message, Throwable t )
113 {
114 if ( level >= INFO )
115 {
116 log( INFO, message, t );
117 }
118 }
119
120 /**
121 * {@inheritDoc}
122 *
123 * @see info.jmonit.logger.Log#warn(java.lang.String)
124 */
125 public void warn( String message )
126 {
127 if ( level >= WARN )
128 {
129 log( WARN, message, null );
130 }
131 }
132
133 /**
134 * {@inheritDoc}
135 *
136 * @see info.jmonit.logger.Log#warn(java.lang.String, Throwable)
137 */
138 public void warn( String message, Throwable t )
139 {
140 if ( level >= WARN )
141 {
142 log( WARN, message, t );
143 }
144 }
145
146 /**
147 * {@inheritDoc}
148 *
149 * @see info.jmonit.logger.Log#error(java.lang.String)
150 */
151 public void error( String message )
152 {
153 if ( level >= ERROR )
154 {
155 log( ERROR, message, null );
156 }
157 }
158
159 /**
160 * {@inheritDoc}
161 *
162 * @see info.jmonit.logger.Log#error(java.lang.String, Throwable)
163 */
164 public void error( String message, Throwable t )
165 {
166 if ( level >= ERROR )
167 {
168 log( ERROR, message, t );
169 }
170 }
171
172 /**
173 * Log a message
174 *
175 * @param messageLevel message level
176 * @param message message String
177 * @param e exception to log (or null)
178 * @deprecated Use {@link #log(int,String,Throwable)} instead
179 */
180 private void log( int messageLevel, String message, Exception e )
181 {
182 log( messageLevel, message, e );
183 }
184
185 /**
186 * Log a message
187 *
188 * @param messageLevel message level
189 * @param message message String
190 * @param t exception to log (or null)
191 */
192 private void log( int messageLevel, String message, Throwable t )
193 {
194 StringBuffer stb = new StringBuffer( "[" );
195 switch ( messageLevel )
196 {
197 case DEBUG:
198 stb.append( "DEBUG" );
199 break;
200 case INFO:
201 stb.append( "INFO" );
202 break;
203 case WARN:
204 stb.append( "WARN" );
205 break;
206 default:
207 stb.append( "ERROR" );
208 break;
209 }
210 stb.append( "] (" );
211 stb.append( caller.getName() );
212 stb.append( ") " );
213 stb.append( message );
214 if ( t != null )
215 {
216 StringWriter writer = new StringWriter();
217 t.printStackTrace( new PrintWriter( writer ) );
218 stb.append( "\n" );
219 stb.append( writer.toString() );
220 }
221
222 if ( level < ERROR )
223 {
224 System.out.println( stb.toString() );
225 }
226 else
227 {
228 System.err.println( stb.toString() );
229 }
230 }
231
232 /**
233 * @return the level
234 */
235 public static int getLevel()
236 {
237 return level;
238 }
239
240 /**
241 * @param level the level to set
242 */
243 public static void setLevel( int level )
244 {
245 ConsoleLogger.level = level;
246 }
247 }