multiple unrelated log files with log4j and commons logging

5.2k Views Asked by At

I want my app to have 4 log files. Three of the log files are the typical log4j Info, Warn and Error logs. The 4th log file is completely unrelated and is used to log some data.

My log4j.properties file looks like this:

log4j.threshold=ALL
log4j.rootLogger=ALL, InfoAppender, WarnAppender, ErrorAppender
log4j.DATA_LOGGER=INFO, DataAppender
log4j.additivity.DATA_LOGGER = false

log4j.appender.DataAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DataAppender.File=/app_logs/data.log

log4j.appender.InfoAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.InfoAppender.File=/app_logs/info.log

log4j.appender.WarnAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.WarnAppender.File=/app_logs/warn.log

log4j.appender.ErrorAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorAppender.File=/app_logs/error.log

And my Java code looks like this:

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

private static final Log LOG = LogFactory.getLog(SomeClassInMyApp.class);
private static final Log DATA_LOG = LogFactory.getLog("DATA_LOGGER");

private static void foo() {
    LOG.info("Hello");
    DATA_LOG.info("Some data");
}

When I run this, the "Hello" text is correctly written to the info.log file. But the data.log file is never created or written to.

Why is the DATA_LOG.info("Some data") line not writing to the data.log file, and what code change do I need to make in order for that to happen?

1

There are 1 best solutions below

3
On BEST ANSWER

commons should work as well, can you try this

   import org.apache.log4j.Logger;
   ...
   ...
   Logger log = Logger.getLogger("DATA_LOGGER");
   ...
   ...
   log.info("my data");

instead of this

   import org.apache.commons.logging.Log;

Update Just tested this code works:

log4j.properties

log4j.rootLogger = DEBUG, R

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=E:/testroot.log

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{DATE} %-5p %c:%L %x - %m%n


##########################################################################
################## Appender for Other Logger  ############################
##########################################################################
log4j.logger.TestLog=DEBUG, cache
log4j.additivity.TestLog=false

log4j.appender.cache=org.apache.log4j.DailyRollingFileAppender
log4j.appender.cache.File=E:/testcache.log
log4j.appender.cache.layout=org.apache.log4j.PatternLayout
log4j.appender.cache.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n

Test Code

private static final Logger DATA_LOG = Logger.getLogger("TestLog");
private static final Logger LOG = Logger.getLogger(Test.class);

public static final void main(String[] args){
    LOG.error("MSG1");
    DATA_LOG.error("MSG2");
}

Output

testroot.log
09 Feb 2011 12:18:29,671 ERROR in.naishe.so.Test:11  - MSG1

testcache.log
09/02/2011 12:18:29 TestLog MSG2

Are you missing something?