Share

November 11, 2008

Using Log4J category to create specialized log files

Most Java Developers have used Log4J at some point or another...

I like most use it everyday to output messages from my application to log files on disk. One thing i been doing a lot is taken these log lines and outputting them into a specialized log file.

An example of that is cerating a WebTrackingLogger calls where i have a log method as follows

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WebTrackingLogger {

    private static final Log log = LogFactory.getLog(WebTrackingLogger.class);

    public static void log(Sting ip, String url, String.... otherValues) {
        log.info(IP + " " + URL);
        ....
       log.warn(IP + " " + URL, e);
    }

Now for me to isolate the logs from this Logger class into a new file i add the following to my log4j.properties

# Daily Rolling File appender for WebTrackingLogger
log4j.category.com.company.WebTrackingLogger.INFO, PL
log4j.appender.PL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.PL.layout=org.apache.log4j.PatternLayout
log4j.appender.PL.layout.ConversionPattern=%d %x - %m%n
log4j.appender.PL.File=/var/log/app/web-tracking-log
log4j.appender.PL.DatePattern='.'yyyy-MM-dd

and that is it...