KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > cli > AdvancedConsoleLogger


1 /*
2  * $Id: AdvancedConsoleLogger.java,v 1.2 2003/06/11 09:25:10 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.cli;
59
60 import java.io.PrintStream JavaDoc;
61
62 import org.apache.avalon.framework.logger.Logger;
63
64 /**
65  * Special Logger implementation that can split output between stdout and stderr
66  * based on the log level and can omit the log level prefix.
67  *
68  * @author Jeremias Maerki
69  */

70 public class AdvancedConsoleLogger implements Logger {
71
72     /** Log level: debug */
73     public static final int LEVEL_DEBUG = 0;
74
75     /** Log level: info */
76     public static final int LEVEL_INFO = 1;
77
78     /** Log level: warnings */
79     public static final int LEVEL_WARN = 2;
80
81     /** Log level: errors */
82     public static final int LEVEL_ERROR = 3;
83
84     /** Log level: fatal errors */
85     public static final int LEVEL_FATAL = 4;
86
87     /** Log level: disabled */
88     public static final int LEVEL_DISABLED = 5;
89
90     private static final String JavaDoc[] LEVEL_STRINGS =
91             {"[DEBUG] ", "[INFO] ", "[WARN] ", "[ERROR] ", "[FATAL] "};
92
93
94     private int logLevel;
95     
96     private boolean prefix;
97     
98     private PrintStream JavaDoc out;
99     private PrintStream JavaDoc err;
100     
101     /**
102      * Constructor will full configurability.
103      * @param logLevel One of the AdvancedConsoleLogger.LEVEL_* constants.
104      * @param prefix false disables "[DEBUG] ", "[INFO] " prefixes
105      * @param out PrintStream to use for stdout/System.out
106      * @param err PrintStream to use for stderr/System.err
107      */

108     public AdvancedConsoleLogger(int logLevel, boolean prefix, PrintStream JavaDoc out, PrintStream JavaDoc err) {
109         this.logLevel = logLevel;
110         this.prefix = prefix;
111         this.out = out;
112         this.err = err;
113     }
114
115     /**
116      * Default constructor. Same behaviour as Avalon's ConsoleLogger.
117      */

118     public AdvancedConsoleLogger() {
119         this(LEVEL_DEBUG, true, System.out, System.err);
120     }
121
122     private void logMessage(String JavaDoc msg, Throwable JavaDoc t, int logLevel) {
123         if (logLevel >= this.logLevel) {
124             PrintStream JavaDoc stream = (logLevel >= LEVEL_ERROR ? err : out);
125             if (prefix) {
126                 stream.print(LEVEL_STRINGS[logLevel]);
127             }
128             stream.println(msg);
129
130             if (t != null) {
131                 t.printStackTrace(stream);
132             }
133         }
134     }
135
136     /**
137      * @see org.apache.avalon.framework.logger.Logger#debug(String)
138      */

139     public void debug(String JavaDoc msg) {
140         debug(msg, null);
141     }
142
143     /**
144      * @see org.apache.avalon.framework.logger.Logger#debug(String, Throwable)
145      */

146     public void debug(String JavaDoc msg, Throwable JavaDoc t) {
147         logMessage(msg, t, LEVEL_DEBUG);
148     }
149
150     /**
151      * @see org.apache.avalon.framework.logger.Logger#isDebugEnabled()
152      */

153     public boolean isDebugEnabled() {
154         return (logLevel <= LEVEL_DEBUG);
155     }
156
157     /**
158      * @see org.apache.avalon.framework.logger.Logger#info(String)
159      */

160     public void info(String JavaDoc msg) {
161         info(msg, null);
162     }
163
164     /**
165      * @see org.apache.avalon.framework.logger.Logger#info(String, Throwable)
166      */

167     public void info(String JavaDoc msg, Throwable JavaDoc t) {
168         logMessage(msg, t, LEVEL_INFO);
169     }
170
171     /**
172      * @see org.apache.avalon.framework.logger.Logger#isInfoEnabled()
173      */

174     public boolean isInfoEnabled() {
175         return (logLevel <= LEVEL_INFO);
176     }
177
178     /**
179      * @see org.apache.avalon.framework.logger.Logger#warn(String)
180      */

181     public void warn(String JavaDoc msg) {
182         warn(msg, null);
183     }
184
185     /**
186      * @see org.apache.avalon.framework.logger.Logger#warn(String, Throwable)
187      */

188     public void warn(String JavaDoc msg, Throwable JavaDoc t) {
189         logMessage(msg, t, LEVEL_WARN);
190     }
191
192     /**
193      * @see org.apache.avalon.framework.logger.Logger#isWarnEnabled()
194      */

195     public boolean isWarnEnabled() {
196         return (logLevel <= LEVEL_WARN);
197     }
198
199     /**
200      * @see org.apache.avalon.framework.logger.Logger#error(String)
201      */

202     public void error(String JavaDoc msg) {
203         error(msg, null);
204     }
205
206     /**
207      * @see org.apache.avalon.framework.logger.Logger#error(String, Throwable)
208      */

209     public void error(String JavaDoc msg, Throwable JavaDoc t) {
210         logMessage(msg, t, LEVEL_ERROR);
211     }
212
213     /**
214      * @see org.apache.avalon.framework.logger.Logger#isErrorEnabled()
215      */

216     public boolean isErrorEnabled() {
217         return (logLevel <= LEVEL_ERROR);
218     }
219
220     /**
221      * @see org.apache.avalon.framework.logger.Logger#fatalError(String)
222      */

223     public void fatalError(String JavaDoc msg) {
224         fatalError(msg, null);
225     }
226
227     /**
228      * @see org.apache.avalon.framework.logger.Logger#fatalError(String, Throwable)
229      */

230     public void fatalError(String JavaDoc msg, Throwable JavaDoc t) {
231         logMessage(msg, t, LEVEL_FATAL);
232     }
233
234     /**
235      * @see org.apache.avalon.framework.logger.Logger#isFatalErrorEnabled()
236      */

237     public boolean isFatalErrorEnabled() {
238         return (logLevel <= LEVEL_FATAL);
239     }
240
241     /**
242      * @see org.apache.avalon.framework.logger.Logger#getChildLogger(String)
243      */

244     public Logger getChildLogger(String JavaDoc name) {
245         return this;
246     }
247
248 }
249
Popular Tags