KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > test > BufferedLogger


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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