KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > BufferedLogger


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.logger;
9
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.PrintStream JavaDoc;
12
13 import org.apache.avalon.framework.logger.Logger;
14
15 /**
16  * Simple Logger which logs all information to an internal StringBuffer.
17  * When logging is complete call toString() on the logger to obtain the
18  * logged output. Useful for testing.
19  *
20  * @author <a HREF="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
21  * @version CVS $Revision: 1.1 $ $Date: 2002/01/24 02:55:22 $
22  * @since 4.0
23  */

24 public class BufferedLogger implements Logger {
25
26     private StringBuffer JavaDoc _sb;
27
28     /*---------------------------------------------------------------
29      * Constructors
30      *-------------------------------------------------------------*/

31
32     /**
33      * Create a new <code>BufferedLogger</code>.
34      */

35     public BufferedLogger() {
36         _sb = new StringBuffer JavaDoc();
37     }
38
39     /*---------------------------------------------------------------
40      * Logger Methods
41      *-------------------------------------------------------------*/

42
43     /**
44      * Log a debug message.
45      *
46      * @param message the message
47      */

48     public void debug(String JavaDoc message) {
49         append("DEBUG", message);
50     }
51
52     /**
53      * Log a debug message.
54      *
55      * @param message the message
56      * @param throwable the throwable
57      */

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

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

76     public void info(String JavaDoc message) {
77         append("INFO", message);
78     }
79
80     /**
81      * Log a info message.
82      *
83      * @param message the message
84      * @param throwable the throwable
85      */

86     public void info(String JavaDoc message, Throwable JavaDoc throwable) {
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         return true;
97     }
98
99     /**
100      * Log a warn message.
101      *
102      * @param message the message
103      */

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

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

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

132     public void error(String JavaDoc message) {
133         append("ERROR", message);
134     }
135
136     /**
137      * Log a error message.
138      *
139      * @param message the message
140      * @param throwable the throwable
141      */

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

151     public boolean isErrorEnabled() {
152         return true;
153     }
154
155     /**
156      * Log a fatalError message.
157      *
158      * @param message the message
159      */

160     public void fatalError(String JavaDoc message) {
161         append("FATAL ERROR", message);
162     }
163
164     /**
165      * Log a fatalError message.
166      *
167      * @param message the message
168      * @param throwable the throwable
169      */

170     public void fatalError(String JavaDoc message, Throwable JavaDoc throwable) {
171         append("FATAL ERROR", message, throwable);
172     }
173
174     /**
175      * Determine if messages of priority "fatalError" will be logged.
176      *
177      * @return true if "fatalError" messages will be logged
178      */

179     public boolean isFatalErrorEnabled() {
180         return true;
181     }
182
183     /**
184      * Create a new child logger.
185      * The name of the child logger is [current-loggers-name].[passed-in-name]
186      *
187      * @param name the subname of this logger
188      * @return the new logger
189      */

190     public Logger getChildLogger(String JavaDoc name) {
191         return this;
192     }
193
194     /*---------------------------------------------------------------
195      * Public Methods
196      *-------------------------------------------------------------*/

197     private void append(String JavaDoc message) {
198
199         synchronized (_sb) {
200             _sb.append(message);
201             _sb.append("\n");
202         }
203     }
204
205     private void append(String JavaDoc level, String JavaDoc message) {
206
207         synchronized (_sb) {
208             _sb.append(level);
209             _sb.append(" - ");
210             _sb.append(message);
211             _sb.append("\n");
212         }
213     }
214
215     private void append(String JavaDoc level, String JavaDoc message, Throwable JavaDoc throwable) {
216
217         synchronized (_sb) {
218             String JavaDoc tDump = null;
219             ByteArrayOutputStream JavaDoc ba = new ByteArrayOutputStream JavaDoc();
220             PrintStream JavaDoc ps = new PrintStream JavaDoc(ba);
221
222             try {
223                 throwable.printStackTrace(ps);
224
225                 tDump = ba.toString();
226             } finally {
227                 ps.close();
228             }
229
230             _sb.append(level);
231             _sb.append(" - ");
232             _sb.append(message);
233             _sb.append(" : ");
234             _sb.append(tDump);
235             _sb.append("\n");
236         }
237     }
238
239     /**
240      * Returns the contents of the buffer.
241      *
242      * @return the buffer contents
243      *
244      */

245     public String JavaDoc toString() {
246         return _sb.toString();
247     }
248 }
249
Popular Tags