KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > CommandLineLogger


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

16
17 /* $Id: CommandLineLogger.java 426576 2006-07-28 15:44:37Z jeremias $ */
18
19 package org.apache.fop.util;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 /**
25  * This is a commons-logging logger for command line use.
26  */

27 public class CommandLineLogger implements Log {
28     /** "Trace" level logging. */
29     public static final int LOG_LEVEL_TRACE = 1;
30     /** "Debug" level logging. */
31     public static final int LOG_LEVEL_DEBUG = 2;
32     /** "Info" level logging. */
33     public static final int LOG_LEVEL_INFO = 3;
34     /** "Warn" level logging. */
35     public static final int LOG_LEVEL_WARN = 4;
36     /** "Error" level logging. */
37     public static final int LOG_LEVEL_ERROR = 5;
38     /** "Fatal" level logging. */
39     public static final int LOG_LEVEL_FATAL = 6;
40
41     private int logLevel;
42     private String JavaDoc logName;
43     
44     /**
45      * Construct the logger with a default log level taken from the LogFactory
46      * attribute "level".
47      * @param logName the logger name.
48      */

49     public CommandLineLogger(String JavaDoc logName) {
50         this.logName = logName;
51         setLogLevel((String JavaDoc) LogFactory.getFactory().getAttribute("level"));
52     }
53     
54     /**
55      * Set a log level for the logger.
56      * @param level the log level
57      */

58     public void setLogLevel(String JavaDoc level) {
59         if ("fatal".equals(level)) {
60             logLevel = LOG_LEVEL_FATAL;
61         } else if ("error".equals(level)) {
62             logLevel = LOG_LEVEL_ERROR;
63         } else if ("warn".equals(level)) {
64             logLevel = LOG_LEVEL_WARN;
65         } else if ("info".equals(level)) {
66             logLevel = LOG_LEVEL_INFO;
67         } else if ("debug".equals(level)) {
68             logLevel = LOG_LEVEL_DEBUG;
69         } else if ("trace".equals(level)) {
70             logLevel = LOG_LEVEL_TRACE;
71         } else {
72             logLevel = LOG_LEVEL_INFO;
73         }
74     }
75     
76     /**
77      * @see org.apache.commons.logging.Log#isTraceEnabled()
78      */

79     public final boolean isTraceEnabled() {
80         return logLevel <= LOG_LEVEL_TRACE;
81     }
82
83     /**
84      * @see org.apache.commons.logging.Log#isDebugEnabled()
85      */

86     public final boolean isDebugEnabled() {
87         return logLevel <= LOG_LEVEL_DEBUG;
88     }
89
90     /**
91      * @see org.apache.commons.logging.Log#isInfoEnabled()
92      */

93     public final boolean isInfoEnabled() {
94         return logLevel <= LOG_LEVEL_INFO;
95     }
96     
97     /**
98      * @see org.apache.commons.logging.Log#isWarnEnabled()
99      */

100     public final boolean isWarnEnabled() {
101         return logLevel <= LOG_LEVEL_WARN;
102     }
103
104     /**
105      * @see org.apache.commons.logging.Log#isErrorEnabled()
106      */

107     public final boolean isErrorEnabled() {
108         return logLevel <= LOG_LEVEL_ERROR;
109     }
110
111     /**
112      * @see org.apache.commons.logging.Log#isFatalEnabled()
113      */

114     public final boolean isFatalEnabled() {
115         return logLevel <= LOG_LEVEL_FATAL;
116     }
117     
118     /**
119      * @see org.apache.commons.logging.Log#trace(java.lang.Object)
120      */

121     public final void trace(Object JavaDoc message) {
122         if (isTraceEnabled()) {
123             log(LOG_LEVEL_TRACE, message, null);
124         }
125     }
126
127     /**
128      * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
129      */

130     public final void trace(Object JavaDoc message, Throwable JavaDoc t) {
131         if (isTraceEnabled()) {
132             log(LOG_LEVEL_TRACE, message, t);
133         }
134     }
135
136     /**
137      * @see org.apache.commons.logging.Log#debug(java.lang.Object)
138      */

139     public final void debug(Object JavaDoc message) {
140         if (isDebugEnabled()) {
141             log(LOG_LEVEL_DEBUG, message, null);
142         }
143     }
144
145     /**
146      * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
147      */

148     public final void debug(Object JavaDoc message, Throwable JavaDoc t) {
149         if (isDebugEnabled()) {
150             log(LOG_LEVEL_DEBUG, message, t);
151         }
152     }
153
154     /**
155      * @see org.apache.commons.logging.Log#info(java.lang.Object)
156      */

157     public final void info(Object JavaDoc message) {
158         if (isInfoEnabled()) {
159             log(LOG_LEVEL_INFO, message, null);
160         }
161     }
162
163     /**
164      * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
165      */

166     public final void info(Object JavaDoc message, Throwable JavaDoc t) {
167         if (isInfoEnabled()) {
168             log(LOG_LEVEL_INFO, message, t);
169         }
170     }
171
172     /**
173      * @see org.apache.commons.logging.Log#warn(java.lang.Object)
174      */

175     public final void warn(Object JavaDoc message) {
176         if (isWarnEnabled()) {
177             log(LOG_LEVEL_WARN, message, null);
178         }
179     }
180
181     /**
182      * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
183      */

184     public final void warn(Object JavaDoc message, Throwable JavaDoc t) {
185         if (isWarnEnabled()) {
186             log(LOG_LEVEL_WARN, message, t);
187         }
188     }
189
190     /**
191      * @see org.apache.commons.logging.Log#error(java.lang.Object)
192      */

193     public final void error(Object JavaDoc message) {
194         if (isErrorEnabled()) {
195             log(LOG_LEVEL_ERROR, message, null);
196         }
197     }
198
199     /**
200      * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
201      */

202     public final void error(Object JavaDoc message, Throwable JavaDoc t) {
203         if (isErrorEnabled()) {
204             log(LOG_LEVEL_ERROR, message, t);
205         }
206     }
207
208     /**
209      * @see org.apache.commons.logging.Log#fatal(java.lang.Object)
210      */

211     public final void fatal(Object JavaDoc message) {
212         if (isFatalEnabled()) {
213             log(LOG_LEVEL_FATAL, message, null);
214         }
215     }
216
217     /**
218      * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
219      */

220     public final void fatal(Object JavaDoc message, Throwable JavaDoc t) {
221         if (isFatalEnabled()) {
222             log(LOG_LEVEL_FATAL, message, t);
223         }
224     }
225     
226     /**
227      * Do the actual logging.
228      * This method assembles the message and prints it to
229      * and then calls <code>write()</code> to cause it to be written.</p>
230      *
231      * @param type One of the LOG_LEVEL_XXX constants defining the log level
232      * @param message The message itself (typically a String)
233      * @param t The exception whose stack trace should be logged
234      */

235     protected void log(int type, Object JavaDoc message, Throwable JavaDoc t) {
236         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
237         // Append the message
238
buf.append(String.valueOf(message));
239         if (t != null) {
240             buf.append("\n");
241             // Append a stack trace or just the stack trace message.
242
if (!isDebugEnabled()) {
243                 buf.append(t.toString());
244                 buf.append("\n");
245             } else {
246                 java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc(1024);
247                 java.io.PrintWriter JavaDoc pw = new java.io.PrintWriter JavaDoc(sw);
248                 t.printStackTrace(pw);
249                 pw.close();
250                 buf.append(sw.toString());
251             }
252         }
253
254         // Print to the appropriate destination
255
if (type >= LOG_LEVEL_WARN) {
256             System.err.println(buf);
257         } else {
258             System.out.println(buf);
259         }
260
261     }
262 }
263
Popular Tags