Better command-line logging without losing the possibility to fine-tune Commons Logging from outside. Submitted by: Finn Bock <bckfnn.at.apache.org> plus using the new CommandLineLogger for PFMReader and TTFReader, too. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197940 13f79535-47bb-0310-9956-ffa450edef68tags/Root_Temp_KnuthStylePageBreaking
import java.util.Vector; | import java.util.Vector; | ||||
import org.apache.fop.fo.Constants; | import org.apache.fop.fo.Constants; | ||||
import org.apache.fop.util.CommandLineLogger; | |||||
// commons logging | // commons logging | ||||
import org.apache.commons.logging.Log; | import org.apache.commons.logging.Log; | ||||
* Construct a command line option object from command line arguments | * Construct a command line option object from command line arguments | ||||
* @param args command line parameters | * @param args command line parameters | ||||
* @throws FOPException for general errors | * @throws FOPException for general errors | ||||
* @throws FileNotFoundException if an input file wasn't found. | |||||
* @throws FileNotFoundException if an input file wasn't found | |||||
* @throws IOException if the the configuration file could not be loaded | |||||
*/ | */ | ||||
public CommandLineOptions(String[] args) | |||||
throws FOPException, FileNotFoundException, IOException { | |||||
public CommandLineOptions(String[] args) throws FOPException, IOException { | |||||
LogFactory logFactory = LogFactory.getFactory(); | |||||
// Enable the simple command line logging when no other logger is | |||||
// defined. | |||||
if (System.getProperty("org.apache.commons.logging.Log") == null) { | |||||
logFactory.setAttribute("org.apache.commons.logging.Log", | |||||
CommandLineLogger.class.getName()); | |||||
setLogLevel("info"); | |||||
} | |||||
log = LogFactory.getLog("FOP"); | log = LogFactory.getLog("FOP"); | ||||
parse(args); | |||||
} | |||||
private void parse(String[] args) | |||||
throws FOPException, IOException { | |||||
boolean optionsParsed = true; | boolean optionsParsed = true; | ||||
foUserAgent = new FOUserAgent(); | foUserAgent = new FOUserAgent(); | ||||
i = i + parseLanguageOption(args, i); | i = i + parseLanguageOption(args, i); | ||||
} else if (args[i].equals("-s")) { | } else if (args[i].equals("-s")) { | ||||
suppressLowLevelAreas = Boolean.TRUE; | suppressLowLevelAreas = Boolean.TRUE; | ||||
} else if (args[i].equals("-d")) { | |||||
setLogLevel("debug"); | |||||
} else if (args[i].equals("-q") || args[i].equals("--quiet")) { | |||||
setLogLevel("error"); | |||||
} else if (args[i].equals("-fo")) { | } else if (args[i].equals("-fo")) { | ||||
i = i + parseFOInputOption(args, i); | i = i + parseFOInputOption(args, i); | ||||
} else if (args[i].equals("-xsl")) { | } else if (args[i].equals("-xsl")) { | ||||
} | } | ||||
} | } | ||||
private void setLogLevel(String level) { | |||||
// Set the evel for future loggers. | |||||
LogFactory.getFactory().setAttribute("level", level); | |||||
if (log instanceof CommandLineLogger) { | |||||
// Set the level for the logger creates already. | |||||
((CommandLineLogger) log).setLogLevel(level); | |||||
} | |||||
} | |||||
/** | /** | ||||
* checks whether all necessary information has been given in a consistent way | * checks whether all necessary information has been given in a consistent way | ||||
*/ | */ | ||||
} | } | ||||
if ((outputmode == RENDER_AWT || outputmode == RENDER_PRINT) && outfile != null) { | if ((outputmode == RENDER_AWT || outputmode == RENDER_PRINT) && outfile != null) { | ||||
throw new FOPException("Output file may not be specified " + | |||||
"for AWT or PRINT output"); | |||||
throw new FOPException("Output file may not be specified " | |||||
+ "for AWT or PRINT output"); | |||||
} | } | ||||
if (inputmode == XSLT_INPUT) { | if (inputmode == XSLT_INPUT) { | ||||
/** | /** | ||||
* Create an InputHandler object based on command-line parameters | * Create an InputHandler object based on command-line parameters | ||||
* @return a new InputHandler instance | * @return a new InputHandler instance | ||||
* @throws IllegalStateException if invalid/missing parameters | |||||
* @throws IllegalArgumentException if invalid/missing parameters | |||||
*/ | */ | ||||
private InputHandler createInputHandler() throws IllegalArgumentException { | private InputHandler createInputHandler() throws IllegalArgumentException { | ||||
switch (inputmode) { | switch (inputmode) { | ||||
"\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] " | "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] " | ||||
+ "[-awt|-pdf|-mif|-rtf|-pcl|-ps|-txt|-at|-print] <outfile>\n" | + "[-awt|-pdf|-mif|-rtf|-pcl|-ps|-txt|-at|-print] <outfile>\n" | ||||
+ " [OPTIONS] \n" | + " [OPTIONS] \n" | ||||
+ " -d debug mode \n" | |||||
+ " -x dump configuration settings \n" | + " -x dump configuration settings \n" | ||||
+ " -q quiet mode \n" | |||||
+ " -c cfg.xml use additional configuration file cfg.xml\n" | + " -c cfg.xml use additional configuration file cfg.xml\n" | ||||
+ " -l lang the language to use for user information \n" | + " -l lang the language to use for user information \n" | ||||
+ " -s for area tree XML, down to block areas only\n" | + " -s for area tree XML, down to block areas only\n" |
if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) { | if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) { | ||||
System.exit(0); | System.exit(0); | ||||
} | } | ||||
} catch (FOPException e) { | |||||
if (e.getMessage() == null) { | |||||
System.err.println("Exception occured with a null error message"); | |||||
} else { | |||||
System.err.println("" + e.getMessage()); | |||||
} | |||||
if (options != null && options.getLogger().isDebugEnabled()) { | |||||
e.printStackTrace(); | |||||
} else { | |||||
System.err.println("Turn on debugging for more information"); | |||||
} | |||||
System.exit(1); | |||||
} catch (java.io.IOException e) { | |||||
System.err.println("" + e.getMessage()); | |||||
if (options != null && options.getLogger().isDebugEnabled()) { | |||||
e.printStackTrace(); | |||||
} else { | |||||
System.err.println("Turn on debugging for more information"); | |||||
} catch (Exception e) { | |||||
if (options != null) { | |||||
options.getLogger().error("Exception", e); | |||||
} | } | ||||
System.exit(1); | System.exit(1); | ||||
} | } |
//FOP | //FOP | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.fonts.type1.PFMFile; | import org.apache.fop.fonts.type1.PFMFile; | ||||
import org.apache.fop.util.CommandLineLogger; | |||||
/** | /** | ||||
* A tool which reads PFM files from Adobe Type 1 fonts and creates | * A tool which reads PFM files from Adobe Type 1 fonts and creates | ||||
/** | /** | ||||
* logging instance | * logging instance | ||||
*/ | */ | ||||
protected Log log = LogFactory.getLog(TTFReader.class); | |||||
protected static Log log; | |||||
/** | /** | ||||
* Parse commandline arguments. put options in the HashMap and return | * Parse commandline arguments. put options in the HashMap and return | ||||
List arguments = new java.util.ArrayList(); | List arguments = new java.util.ArrayList(); | ||||
for (int i = 0; i < args.length; i++) { | for (int i = 0; i < args.length; i++) { | ||||
if (args[i].startsWith("-")) { | if (args[i].startsWith("-")) { | ||||
if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { | |||||
if ("-d".equals(args[i]) || "-q".equals(args[i])) { | |||||
options.put(args[i], ""); | |||||
} else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { | |||||
options.put(args[i], args[i + 1]); | options.put(args[i], args[i + 1]); | ||||
i++; | i++; | ||||
} else { | } else { | ||||
"java " + PFMReader.class.getName() + " [options] metricfile.pfm xmlfile.xml"); | "java " + PFMReader.class.getName() + " [options] metricfile.pfm xmlfile.xml"); | ||||
System.out.println(); | System.out.println(); | ||||
System.out.println("where options can be:"); | System.out.println("where options can be:"); | ||||
System.out.println("-d <WARN|INFO|DEBUG>"); | |||||
System.out.println(" Set debug level (default: WARN)."); | |||||
System.out.println("-d Debug mode"); | |||||
System.out.println("-q Quiet mode"); | |||||
System.out.println("-fn <fontname>"); | System.out.println("-fn <fontname>"); | ||||
System.out.println(" default is to use the fontname in the .pfm file, but"); | System.out.println(" default is to use the fontname in the .pfm file, but"); | ||||
System.out.println(" you can override that name to make sure that the"); | System.out.println(" you can override that name to make sure that the"); | ||||
Map options = new java.util.HashMap(); | Map options = new java.util.HashMap(); | ||||
String[] arguments = parseArguments(options, args); | String[] arguments = parseArguments(options, args); | ||||
//Setup simple logger for this command-line application | |||||
System.setProperty("org.apache.commons.logging.Log", | |||||
"org.apache.commons.logging.impl.SimpleLog"); | |||||
System.setProperty("org.apache.commons.logging.simplelog.showShortLogname", | |||||
"false"); | |||||
// Enable the simple command line logging when no other logger is | |||||
// defined. | |||||
LogFactory logFactory = LogFactory.getFactory(); | |||||
if (System.getProperty("org.apache.commons.logging.Log") == null) { | |||||
logFactory.setAttribute("org.apache.commons.logging.Log", | |||||
CommandLineLogger.class.getName()); | |||||
} | |||||
//Determine log level | //Determine log level | ||||
String level; | |||||
if (options.get("-d") != null) { | if (options.get("-d") != null) { | ||||
String lev = (String)options.get("-d"); | |||||
if ("DEBUG".equalsIgnoreCase(lev)) { | |||||
level = "debug"; | |||||
} else if ("INFO".equalsIgnoreCase(lev)) { | |||||
level = "info"; | |||||
} else { | |||||
level = "warn"; | |||||
} | |||||
setLogLevel("debug"); | |||||
} else if (options.get("-q") != null) { | |||||
setLogLevel("error"); | |||||
} else { | } else { | ||||
level = "warn"; | |||||
setLogLevel("info"); | |||||
} | } | ||||
System.setProperty("org.apache.commons.logging.simplelog.defaultlog", | |||||
level); | |||||
log = LogFactory.getLog(PFMReader.class); | |||||
PFMReader app = new PFMReader(); | PFMReader app = new PFMReader(); | ||||
System.out.println("PFM Reader for Apache FOP " + Fop.getVersion() + "\n"); | |||||
log.info("PFM Reader for Apache FOP " + Fop.getVersion() + "\n"); | |||||
if (options.get("-ef") != null) { | if (options.get("-ef") != null) { | ||||
embFile = (String)options.get("-ef"); | embFile = (String)options.get("-ef"); | ||||
displayUsage(); | displayUsage(); | ||||
} else { | } else { | ||||
try { | try { | ||||
System.out.println("Parsing font..."); | |||||
log.info("Parsing font..."); | |||||
PFMFile pfm = app.loadPFM(arguments[0]); | PFMFile pfm = app.loadPFM(arguments[0]); | ||||
if (pfm != null) { | if (pfm != null) { | ||||
app.preview(pfm); | app.preview(pfm); | ||||
app.writeFontXML(doc, arguments[1]); | app.writeFontXML(doc, arguments[1]); | ||||
} | } | ||||
System.out.println("\nXML font metrics file successfullly created."); | |||||
log.info("XML font metrics file successfullly created."); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
System.err.println("Error while building XML font metrics file"); | |||||
e.printStackTrace(System.err); | |||||
log.error("Error while building XML font metrics file", e); | |||||
System.exit(-1); | System.exit(-1); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private static void setLogLevel(String level) { | |||||
// Set the evel for future loggers. | |||||
LogFactory.getFactory().setAttribute("level", level); | |||||
if (log instanceof CommandLineLogger) { | |||||
// Set the level for the logger creates already. | |||||
((CommandLineLogger) log).setLogLevel(level); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Read a PFM file and returns it as an object. | * Read a PFM file and returns it as an object. |
import org.apache.fop.fonts.truetype.FontFileReader; | import org.apache.fop.fonts.truetype.FontFileReader; | ||||
import org.apache.fop.fonts.truetype.TTFCmapEntry; | import org.apache.fop.fonts.truetype.TTFCmapEntry; | ||||
import org.apache.fop.fonts.truetype.TTFFile; | import org.apache.fop.fonts.truetype.TTFFile; | ||||
import org.apache.fop.util.CommandLineLogger; | |||||
/** | /** | ||||
* A tool which reads TTF files and generates | * A tool which reads TTF files and generates | ||||
/** | /** | ||||
* logging instance | * logging instance | ||||
*/ | */ | ||||
protected Log log = LogFactory.getLog(TTFReader.class); | |||||
protected static Log log; | |||||
/** | /** | ||||
* Parse commandline arguments. put options in the HashMap and return | * Parse commandline arguments. put options in the HashMap and return | ||||
List arguments = new java.util.ArrayList(); | List arguments = new java.util.ArrayList(); | ||||
for (int i = 0; i < args.length; i++) { | for (int i = 0; i < args.length; i++) { | ||||
if (args[i].startsWith("-")) { | if (args[i].startsWith("-")) { | ||||
if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { | |||||
if ("-d".equals(args[i]) || "-q".equals(args[i])) { | |||||
options.put(args[i], ""); | |||||
} else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { | |||||
options.put(args[i], args[i + 1]); | options.put(args[i], args[i + 1]); | ||||
i++; | i++; | ||||
} else { | } else { | ||||
"java " + TTFReader.class.getName() + " [options] fontfile.ttf xmlfile.xml"); | "java " + TTFReader.class.getName() + " [options] fontfile.ttf xmlfile.xml"); | ||||
System.out.println(); | System.out.println(); | ||||
System.out.println("where options can be:"); | System.out.println("where options can be:"); | ||||
System.out.println("-d <WARN|INFO|DEBUG>"); | |||||
System.out.println(" Set debug level (default: WARN)."); | |||||
System.out.println("-d Debug mode"); | |||||
System.out.println("-q Quiet mode"); | |||||
System.out.println("-enc ansi"); | System.out.println("-enc ansi"); | ||||
System.out.println(" With this option you create a WinAnsi encoded font."); | System.out.println(" With this option you create a WinAnsi encoded font."); | ||||
System.out.println(" The default is to create a CID keyed font."); | System.out.println(" The default is to create a CID keyed font."); | ||||
Map options = new java.util.HashMap(); | Map options = new java.util.HashMap(); | ||||
String[] arguments = parseArguments(options, args); | String[] arguments = parseArguments(options, args); | ||||
//Setup simple logger for this command-line application | |||||
System.setProperty("org.apache.commons.logging.Log", | |||||
"org.apache.commons.logging.impl.SimpleLog"); | |||||
System.setProperty("org.apache.commons.logging.simplelog.showShortLogname", | |||||
"false"); | |||||
// Enable the simple command line logging when no other logger is | |||||
// defined. | |||||
LogFactory logFactory = LogFactory.getFactory(); | |||||
if (System.getProperty("org.apache.commons.logging.Log") == null) { | |||||
logFactory.setAttribute("org.apache.commons.logging.Log", | |||||
CommandLineLogger.class.getName()); | |||||
} | |||||
//Determine log level | //Determine log level | ||||
String level; | |||||
if (options.get("-d") != null) { | if (options.get("-d") != null) { | ||||
String lev = (String)options.get("-d"); | |||||
if ("DEBUG".equalsIgnoreCase(lev)) { | |||||
level = "debug"; | |||||
} else if ("INFO".equalsIgnoreCase(lev)) { | |||||
level = "info"; | |||||
} else { | |||||
level = "warn"; | |||||
} | |||||
setLogLevel("debug"); | |||||
} else if (options.get("-q") != null) { | |||||
setLogLevel("error"); | |||||
} else { | } else { | ||||
level = "warn"; | |||||
setLogLevel("info"); | |||||
} | } | ||||
System.setProperty("org.apache.commons.logging.simplelog.defaultlog", | |||||
level); | |||||
log = LogFactory.getLog(TTFReader.class); | |||||
TTFReader app = new TTFReader(); | TTFReader app = new TTFReader(); | ||||
System.out.println("TTF Reader for Apache FOP " + Fop.getVersion() + "\n"); | |||||
log.info("TTF Reader for Apache FOP " + Fop.getVersion() + "\n"); | |||||
if (options.get("-enc") != null) { | if (options.get("-enc") != null) { | ||||
String enc = (String)options.get("-enc"); | String enc = (String)options.get("-enc"); | ||||
displayUsage(); | displayUsage(); | ||||
} else { | } else { | ||||
try { | try { | ||||
System.out.println("Parsing font..."); | |||||
log.info("Parsing font..."); | |||||
TTFFile ttf = app.loadTTF(arguments[0], ttcName); | TTFFile ttf = app.loadTTF(arguments[0], ttcName); | ||||
if (ttf != null) { | if (ttf != null) { | ||||
org.w3c.dom.Document doc = app.constructFontXML(ttf, | org.w3c.dom.Document doc = app.constructFontXML(ttf, | ||||
ttcName); | ttcName); | ||||
if (isCid) { | if (isCid) { | ||||
System.out.println("Creating CID encoded metrics..."); | |||||
log.info("Creating CID encoded metrics..."); | |||||
} else { | } else { | ||||
System.out.println("Creating WinAnsi encoded metrics..."); | |||||
log.info("Creating WinAnsi encoded metrics..."); | |||||
} | } | ||||
if (doc != null) { | if (doc != null) { | ||||
} | } | ||||
if (ttf.isEmbeddable()) { | if (ttf.isEmbeddable()) { | ||||
System.out.println("This font contains no embedding license restrictions."); | |||||
log.info("This font contains no embedding license restrictions."); | |||||
} else { | } else { | ||||
System.out.println("** Note: This font contains license retrictions for\n" | |||||
log.info("** Note: This font contains license retrictions for\n" | |||||
+ " embedding. This font shouldn't be embedded."); | + " embedding. This font shouldn't be embedded."); | ||||
} | } | ||||
} | } | ||||
System.out.println("\nXML font metrics file successfullly created."); | |||||
log.info(""); | |||||
log.info("XML font metrics file successfullly created."); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
System.err.println("Error while building XML font metrics file."); | |||||
e.printStackTrace(System.err); | |||||
log.error("Error while building XML font metrics file.", e); | |||||
System.exit(-1); | System.exit(-1); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private static void setLogLevel(String level) { | |||||
// Set the evel for future loggers. | |||||
LogFactory.getFactory().setAttribute("level", level); | |||||
if (log instanceof CommandLineLogger) { | |||||
// Set the level for the logger creates already. | |||||
((CommandLineLogger) log).setLogLevel(level); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Read a TTF file and returns it as an object. | * Read a TTF file and returns it as an object. | ||||
* | * |
/* Copyright 2004 The Apache Software Foundation. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.util; | |||||
import org.apache.commons.logging.Log; | |||||
import org.apache.commons.logging.LogFactory; | |||||
/** | |||||
* This is a commons-logging logger for command line use. | |||||
*/ | |||||
public class CommandLineLogger implements Log { | |||||
/** "Trace" level logging. */ | |||||
public static final int LOG_LEVEL_TRACE = 1; | |||||
/** "Debug" level logging. */ | |||||
public static final int LOG_LEVEL_DEBUG = 2; | |||||
/** "Info" level logging. */ | |||||
public static final int LOG_LEVEL_INFO = 3; | |||||
/** "Warn" level logging. */ | |||||
public static final int LOG_LEVEL_WARN = 4; | |||||
/** "Error" level logging. */ | |||||
public static final int LOG_LEVEL_ERROR = 5; | |||||
/** "Fatal" level logging. */ | |||||
public static final int LOG_LEVEL_FATAL = 6; | |||||
private int logLevel; | |||||
private String logName; | |||||
/** | |||||
* Construct the logger with a default log level taken from the LogFactory | |||||
* attribute "level". | |||||
* @param logName the logger name. | |||||
*/ | |||||
public CommandLineLogger(String logName) { | |||||
this.logName = logName; | |||||
setLogLevel((String) LogFactory.getFactory().getAttribute("level")); | |||||
} | |||||
/** | |||||
* Set a log level for the logger. | |||||
* @param level the log level | |||||
*/ | |||||
public void setLogLevel(String level) { | |||||
if ("fatal".equals(level)) { | |||||
logLevel = LOG_LEVEL_FATAL; | |||||
} else if ("error".equals(level)) { | |||||
logLevel = LOG_LEVEL_ERROR; | |||||
} else if ("warn".equals(level)) { | |||||
logLevel = LOG_LEVEL_WARN; | |||||
} else if ("info".equals(level)) { | |||||
logLevel = LOG_LEVEL_INFO; | |||||
} else if ("debug".equals(level)) { | |||||
logLevel = LOG_LEVEL_DEBUG; | |||||
} else if ("trace".equals(level)) { | |||||
logLevel = LOG_LEVEL_TRACE; | |||||
} else { | |||||
logLevel = LOG_LEVEL_INFO; | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#isTraceEnabled() | |||||
*/ | |||||
public final boolean isTraceEnabled() { | |||||
return logLevel <= LOG_LEVEL_TRACE; | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#isDebugEnabled() | |||||
*/ | |||||
public final boolean isDebugEnabled() { | |||||
return logLevel <= LOG_LEVEL_DEBUG; | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#isInfoEnabled() | |||||
*/ | |||||
public final boolean isInfoEnabled() { | |||||
return logLevel <= LOG_LEVEL_INFO; | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#isWarnEnabled() | |||||
*/ | |||||
public final boolean isWarnEnabled() { | |||||
return logLevel <= LOG_LEVEL_WARN; | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#isErrorEnabled() | |||||
*/ | |||||
public final boolean isErrorEnabled() { | |||||
return logLevel <= LOG_LEVEL_ERROR; | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#isFatalEnabled() | |||||
*/ | |||||
public final boolean isFatalEnabled() { | |||||
return logLevel <= LOG_LEVEL_FATAL; | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#trace(java.lang.Object) | |||||
*/ | |||||
public final void trace(Object message) { | |||||
if (isTraceEnabled()) { | |||||
log(LOG_LEVEL_TRACE, message, null); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable) | |||||
*/ | |||||
public final void trace(Object message, Throwable t) { | |||||
if (isTraceEnabled()) { | |||||
log(LOG_LEVEL_TRACE, message, t); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#debug(java.lang.Object) | |||||
*/ | |||||
public final void debug(Object message) { | |||||
if (isDebugEnabled()) { | |||||
log(LOG_LEVEL_DEBUG, message, null); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable) | |||||
*/ | |||||
public final void debug(Object message, Throwable t) { | |||||
if (isDebugEnabled()) { | |||||
log(LOG_LEVEL_DEBUG, message, t); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#info(java.lang.Object) | |||||
*/ | |||||
public final void info(Object message) { | |||||
if (isInfoEnabled()) { | |||||
log(LOG_LEVEL_INFO, message, null); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable) | |||||
*/ | |||||
public final void info(Object message, Throwable t) { | |||||
if (isInfoEnabled()) { | |||||
log(LOG_LEVEL_INFO, message, t); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#warn(java.lang.Object) | |||||
*/ | |||||
public final void warn(Object message) { | |||||
if (isWarnEnabled()) { | |||||
log(LOG_LEVEL_WARN, message, null); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable) | |||||
*/ | |||||
public final void warn(Object message, Throwable t) { | |||||
if (isWarnEnabled()) { | |||||
log(LOG_LEVEL_WARN, message, t); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#error(java.lang.Object) | |||||
*/ | |||||
public final void error(Object message) { | |||||
if (isErrorEnabled()) { | |||||
log(LOG_LEVEL_ERROR, message, null); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable) | |||||
*/ | |||||
public final void error(Object message, Throwable t) { | |||||
if (isErrorEnabled()) { | |||||
log(LOG_LEVEL_ERROR, message, t); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#fatal(java.lang.Object) | |||||
*/ | |||||
public final void fatal(Object message) { | |||||
if (isFatalEnabled()) { | |||||
log(LOG_LEVEL_FATAL, message, null); | |||||
} | |||||
} | |||||
/** | |||||
* @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable) | |||||
*/ | |||||
public final void fatal(Object message, Throwable t) { | |||||
if (isFatalEnabled()) { | |||||
log(LOG_LEVEL_FATAL, message, t); | |||||
} | |||||
} | |||||
/** | |||||
* Do the actual logging. | |||||
* This method assembles the message and prints it to | |||||
* and then calls <code>write()</code> to cause it to be written.</p> | |||||
* | |||||
* @param type One of the LOG_LEVEL_XXX constants defining the log level | |||||
* @param message The message itself (typically a String) | |||||
* @param t The exception whose stack trace should be logged | |||||
*/ | |||||
protected void log(int type, Object message, Throwable t) { | |||||
StringBuffer buf = new StringBuffer(); | |||||
// Append the message | |||||
buf.append(String.valueOf(message)); | |||||
if (t != null) { | |||||
buf.append("\n"); | |||||
// Append a stack trace or just the stack trace message. | |||||
if (!isDebugEnabled()) { | |||||
buf.append(t.toString()); | |||||
buf.append("\n"); | |||||
} else { | |||||
java.io.StringWriter sw = new java.io.StringWriter(1024); | |||||
java.io.PrintWriter pw = new java.io.PrintWriter(sw); | |||||
t.printStackTrace(pw); | |||||
pw.close(); | |||||
buf.append(sw.toString()); | |||||
} | |||||
} | |||||
// Print to the appropriate destination | |||||
if (type >= LOG_LEVEL_WARN) { | |||||
System.err.println(buf); | |||||
} else { | |||||
System.out.println(buf); | |||||
} | |||||
} | |||||
} |