1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jmonit.log;
17
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20
21
22
23
24
25
26 public class ConsoleLogger
27 extends Log
28 {
29
30
31 private static final int ERROR = 2;
32
33
34 private static final int WARN = 3;
35
36
37 private static final int INFO = 4;
38
39
40 private static final int DEBUG = 5;
41
42
43 private Class caller;
44
45
46 public static final String LEVEL = "jmonit.logger.level";
47
48
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
62
63
64
65 public boolean isDebugEnabled()
66 {
67 return level >= DEBUG;
68 }
69
70
71
72
73
74
75 public ConsoleLogger( Class caller )
76 {
77 super();
78 this.caller = caller;
79 }
80
81
82
83
84
85
86 public void debug( String message )
87 {
88 if ( level >= DEBUG )
89 {
90 log( DEBUG, message, null );
91 }
92 }
93
94
95
96
97
98
99 public void info( String message )
100 {
101 if ( level >= INFO )
102 {
103 log( INFO, message, null );
104 }
105 }
106
107
108
109
110
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
122
123
124
125 public void warn( String message )
126 {
127 if ( level >= WARN )
128 {
129 log( WARN, message, null );
130 }
131 }
132
133
134
135
136
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
148
149
150
151 public void error( String message )
152 {
153 if ( level >= ERROR )
154 {
155 log( ERROR, message, null );
156 }
157 }
158
159
160
161
162
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
174
175
176
177
178
179
180 private void log( int messageLevel, String message, Exception e )
181 {
182 log( messageLevel, message, e );
183 }
184
185
186
187
188
189
190
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
234
235 public static int getLevel()
236 {
237 return level;
238 }
239
240
241
242
243 public static void setLevel( int level )
244 {
245 ConsoleLogger.level = level;
246 }
247 }