Search code examples
javaloggingapache-commons-logging

How to log Digester with Apache Commons?


I'm having trouble getting Apache Commons Digester to log anything. I'd be hugely grateful for any light anyone can shed.

In my code I'm doing this:

Digester digester = new Digester();
// some Digester set up stuff

// What on earth should go in here????
digester.setLogger(LogFactory.getLog("org.apache.commons.logging.Log"));

I have a commons-logging.properties file in my classpath as follows:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester=debug
org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester.sax=info

I just get no debug info at all.

Thanks for your help!

Update: Thanks for the answer bwawok - that's what the problem was. In the docs for Digester, they suggest that you can just enable the SimpleLog of commons-logging. Unfortunately, the Digester doesn't appear to output any INFO messages, only DEBUG, and at least on eclipse, SimpleLog doesn't output DEBUG messages at all! The result was no INFO messages (because Digester sends none) and no DEBUG messages (because SimpleLog doesn't forward them!) Once I switched to log4j, all the debug messages came spewing out! Thanks again.


Solution

  • I suggest you don't use commons logging. It has some problems, such as not playing nicely with Tomcat 5.5 and higher.

    If you are writing your own application (not a library), I suggest you code directly to Log4j. Read a tutorial at http://logging.apache.org/log4j/1.2/manual.html . You will have a log4j.properties file like so

    log4j.rootLogger=debug, stdout, R
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    
    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=example.log
    
    log4j.appender.R.MaxFileSize=100KB
    # Keep one backup file
    log4j.appender.R.MaxBackupIndex=1
    
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    

    Then in your application you would use

    Logger logger = Logger.getLogger(MyClassName.class);
    logger.info("Doing something I want to log at info... " );
    logger.warn("Danger!");
    

    If instead you are writing a library to distribute (what most people used commons logging for back in the day), look into sl4j. http://www.slf4j.org/ . Most libraries are switching to this instead of commons logging.