001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: /* contributor license agreements. See the NOTICE file distributed with
003: /* this work for additional information regarding copyright ownership.
004: /* The ASF licenses this file to You under the Apache License, Version 2.0
005: /* (the "License"); you may not use this file except in compliance with
006: /* the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /* $Id: CommandLineLogger.java 563951 2007-08-08 17:37:33Z vhennebert $ */
018:
019: package org.apache.fop.util;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * This is a commons-logging logger for command line use.
026: */
027: public class CommandLineLogger implements Log {
028: /** "Trace" level logging. */
029: public static final int LOG_LEVEL_TRACE = 1;
030: /** "Debug" level logging. */
031: public static final int LOG_LEVEL_DEBUG = 2;
032: /** "Info" level logging. */
033: public static final int LOG_LEVEL_INFO = 3;
034: /** "Warn" level logging. */
035: public static final int LOG_LEVEL_WARN = 4;
036: /** "Error" level logging. */
037: public static final int LOG_LEVEL_ERROR = 5;
038: /** "Fatal" level logging. */
039: public static final int LOG_LEVEL_FATAL = 6;
040:
041: private int logLevel;
042: private String logName;
043:
044: /**
045: * Construct the logger with a default log level taken from the LogFactory
046: * attribute "level".
047: * @param logName the logger name.
048: */
049: public CommandLineLogger(String logName) {
050: this .logName = logName;
051: setLogLevel((String) LogFactory.getFactory().getAttribute(
052: "level"));
053: }
054:
055: /**
056: * Set a log level for the logger.
057: * @param level the log level
058: */
059: public void setLogLevel(String level) {
060: if ("fatal".equals(level)) {
061: logLevel = LOG_LEVEL_FATAL;
062: } else if ("error".equals(level)) {
063: logLevel = LOG_LEVEL_ERROR;
064: } else if ("warn".equals(level)) {
065: logLevel = LOG_LEVEL_WARN;
066: } else if ("info".equals(level)) {
067: logLevel = LOG_LEVEL_INFO;
068: } else if ("debug".equals(level)) {
069: logLevel = LOG_LEVEL_DEBUG;
070: } else if ("trace".equals(level)) {
071: logLevel = LOG_LEVEL_TRACE;
072: } else {
073: logLevel = LOG_LEVEL_INFO;
074: }
075: }
076:
077: /**
078: * {@inheritDoc}
079: */
080: public final boolean isTraceEnabled() {
081: return logLevel <= LOG_LEVEL_TRACE;
082: }
083:
084: /**
085: * {@inheritDoc}
086: */
087: public final boolean isDebugEnabled() {
088: return logLevel <= LOG_LEVEL_DEBUG;
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: public final boolean isInfoEnabled() {
095: return logLevel <= LOG_LEVEL_INFO;
096: }
097:
098: /**
099: * {@inheritDoc}
100: */
101: public final boolean isWarnEnabled() {
102: return logLevel <= LOG_LEVEL_WARN;
103: }
104:
105: /**
106: * {@inheritDoc}
107: */
108: public final boolean isErrorEnabled() {
109: return logLevel <= LOG_LEVEL_ERROR;
110: }
111:
112: /**
113: * {@inheritDoc}
114: */
115: public final boolean isFatalEnabled() {
116: return logLevel <= LOG_LEVEL_FATAL;
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public final void trace(Object message) {
123: if (isTraceEnabled()) {
124: log(LOG_LEVEL_TRACE, message, null);
125: }
126: }
127:
128: /**
129: * {@inheritDoc}
130: */
131: public final void trace(Object message, Throwable t) {
132: if (isTraceEnabled()) {
133: log(LOG_LEVEL_TRACE, message, t);
134: }
135: }
136:
137: /**
138: * {@inheritDoc}
139: */
140: public final void debug(Object message) {
141: if (isDebugEnabled()) {
142: log(LOG_LEVEL_DEBUG, message, null);
143: }
144: }
145:
146: /**
147: * {@inheritDoc}
148: */
149: public final void debug(Object message, Throwable t) {
150: if (isDebugEnabled()) {
151: log(LOG_LEVEL_DEBUG, message, t);
152: }
153: }
154:
155: /**
156: * {@inheritDoc}
157: */
158: public final void info(Object message) {
159: if (isInfoEnabled()) {
160: log(LOG_LEVEL_INFO, message, null);
161: }
162: }
163:
164: /**
165: * {@inheritDoc}
166: */
167: public final void info(Object message, Throwable t) {
168: if (isInfoEnabled()) {
169: log(LOG_LEVEL_INFO, message, t);
170: }
171: }
172:
173: /**
174: * {@inheritDoc}
175: */
176: public final void warn(Object message) {
177: if (isWarnEnabled()) {
178: log(LOG_LEVEL_WARN, message, null);
179: }
180: }
181:
182: /**
183: * {@inheritDoc}
184: */
185: public final void warn(Object message, Throwable t) {
186: if (isWarnEnabled()) {
187: log(LOG_LEVEL_WARN, message, t);
188: }
189: }
190:
191: /**
192: * {@inheritDoc}
193: */
194: public final void error(Object message) {
195: if (isErrorEnabled()) {
196: log(LOG_LEVEL_ERROR, message, null);
197: }
198: }
199:
200: /**
201: * {@inheritDoc}
202: */
203: public final void error(Object message, Throwable t) {
204: if (isErrorEnabled()) {
205: log(LOG_LEVEL_ERROR, message, t);
206: }
207: }
208:
209: /**
210: * {@inheritDoc}
211: */
212: public final void fatal(Object message) {
213: if (isFatalEnabled()) {
214: log(LOG_LEVEL_FATAL, message, null);
215: }
216: }
217:
218: /**
219: * {@inheritDoc}
220: */
221: public final void fatal(Object message, Throwable t) {
222: if (isFatalEnabled()) {
223: log(LOG_LEVEL_FATAL, message, t);
224: }
225: }
226:
227: /**
228: * Do the actual logging.
229: * This method assembles the message and prints it to
230: * and then calls <code>write()</code> to cause it to be written.</p>
231: *
232: * @param type One of the LOG_LEVEL_XXX constants defining the log level
233: * @param message The message itself (typically a String)
234: * @param t The exception whose stack trace should be logged
235: */
236: protected void log(int type, Object message, Throwable t) {
237: StringBuffer buf = new StringBuffer();
238: // Append the message
239: buf.append(String.valueOf(message));
240: if (t != null) {
241: buf.append("\n");
242: // Append a stack trace or just the stack trace message.
243: if (!isDebugEnabled()) {
244: buf.append(t.toString());
245: buf.append("\n");
246: } else {
247: java.io.StringWriter sw = new java.io.StringWriter(1024);
248: java.io.PrintWriter pw = new java.io.PrintWriter(sw);
249: t.printStackTrace(pw);
250: pw.close();
251: buf.append(sw.toString());
252: }
253: }
254:
255: // Print to the appropriate destination
256: if (type >= LOG_LEVEL_WARN) {
257: System.err.println(buf);
258: } else {
259: System.out.println(buf);
260: }
261:
262: }
263: }
|