KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > thread > impl > test > BufferedLogger


1 /*
2  * Copyright 2002-2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.thread.impl.test;
18
19 import org.apache.avalon.framework.ExceptionUtil;
20 import org.apache.avalon.framework.logger.Logger;
21
22 /**
23  * Simple Logger which logs all information to an internal StringBuffer.
24  * When logging is complete call toString() on the logger to obtain the
25  * logged output. Useful for testing.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $Revision: 1.1 $ $Date: 2004/03/29 17:22:49 $
29  * @since 4.0
30  */

31 public class BufferedLogger
32     implements Logger
33 {
34     private final StringBuffer JavaDoc m_sb = new StringBuffer JavaDoc();
35
36     /**
37      * Log a debug message.
38      *
39      * @param message the message
40      */

41     public void debug( final String JavaDoc message )
42     {
43         debug( message, null );
44     }
45
46     /**
47      * Log a debug message.
48      *
49      * @param message the message
50      * @param throwable the throwable
51      */

52     public void debug( final String JavaDoc message, final Throwable JavaDoc throwable )
53     {
54         append( "DEBUG", message, throwable );
55     }
56
57     /**
58      * Determine if messages of priority "debug" will be logged.
59      *
60      * @return true if "debug" messages will be logged
61      */

62     public boolean isDebugEnabled()
63     {
64         return true;
65     }
66
67     /**
68      * Log a info message.
69      *
70      * @param message the message
71      */

72     public void info( final String JavaDoc message )
73     {
74         info( message, null );
75     }
76
77     /**
78      * Log a info message.
79      *
80      * @param message the message
81      * @param throwable the throwable
82      */

83     public void info( final String JavaDoc message, final Throwable JavaDoc throwable )
84     {
85         append( "INFO", message, throwable );
86     }
87
88     /**
89      * Determine if messages of priority "info" will be logged.
90      *
91      * @return true if "info" messages will be logged
92      */

93     public boolean isInfoEnabled()
94     {
95         return true;
96     }
97
98     /**
99      * Log a warn message.
100      *
101      * @param message the message
102      */

103     public void warn( final String JavaDoc message )
104     {
105         warn( message, null );
106     }
107
108     /**
109      * Log a warn message.
110      *
111      * @param message the message
112      * @param throwable the throwable
113      */

114     public void warn( final String JavaDoc message, final Throwable JavaDoc throwable )
115     {
116         append( "WARN", message, throwable );
117     }
118
119     /**
120      * Determine if messages of priority "warn" will be logged.
121      *
122      * @return true if "warn" messages will be logged
123      */

124     public boolean isWarnEnabled()
125     {
126         return true;
127     }
128
129     /**
130      * Log a error message.
131      *
132      * @param message the message
133      */

134     public void error( final String JavaDoc message )
135     {
136         error( message, null );
137     }
138
139     /**
140      * Log a error message.
141      *
142      * @param message the message
143      * @param throwable the throwable
144      */

145     public void error( final String JavaDoc message, final Throwable JavaDoc throwable )
146     {
147         append( "ERROR", message, throwable );
148     }
149
150     /**
151      * Determine if messages of priority "error" will be logged.
152      *
153      * @return true if "error" messages will be logged
154      */

155     public boolean isErrorEnabled()
156     {
157         return true;
158     }
159
160     /**
161      * Log a fatalError message.
162      *
163      * @param message the message
164      */

165     public void fatalError( final String JavaDoc message )
166     {
167         fatalError( message, null );
168     }
169
170     /**
171      * Log a fatalError message.
172      *
173      * @param message the message
174      * @param throwable the throwable
175      */

176     public void fatalError( final String JavaDoc message, final Throwable JavaDoc throwable )
177     {
178         append( "FATAL ERROR", message, throwable );
179     }
180
181     /**
182      * Determine if messages of priority "fatalError" will be logged.
183      *
184      * @return true if "fatalError" messages will be logged
185      */

186     public boolean isFatalErrorEnabled()
187     {
188         return true;
189     }
190
191     /**
192      * Create a new child logger.
193      * The name of the child logger is [current-loggers-name].[passed-in-name]
194      *
195      * @param name the subname of this logger
196      * @return the new logger
197      */

198     public Logger getChildLogger( final String JavaDoc name )
199     {
200         return this;
201     }
202
203     /**
204      * Returns the contents of the buffer.
205      *
206      * @return the buffer contents
207      *
208      */

209     public String JavaDoc toString()
210     {
211         return m_sb.toString();
212     }
213
214     private void append( final String JavaDoc level,
215                          final String JavaDoc message,
216                          final Throwable JavaDoc throwable )
217     {
218         synchronized( m_sb )
219         {
220             m_sb.append( level );
221             m_sb.append( " - " );
222             m_sb.append( message );
223
224             if( null != throwable )
225             {
226                 final String JavaDoc stackTrace =
227                     ExceptionUtil.printStackTrace( throwable );
228                 m_sb.append( " : " );
229                 m_sb.append( stackTrace );
230             }
231             m_sb.append( "\n" );
232         }
233     }
234 }
235
Popular Tags