Multiple unrelated log files with log4j and commons logging

I want 4 log files in my application. Three of the log files are typical log4j Info, Warn, and Error logs. The 4th log file is completely unrelated and is used to record 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 text "Hello" is correctly written to the info.log file. But the data.log file is never created or written.

Why is the DATA_LOG.info line ("Some data") not written to the data.log file, and what code change needs to be done in order for this to happen?

+3
source share
1 answer

commons ,

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

   import org.apache.commons.logging.Log;

:

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

- ?

+2

Source: https://habr.com/ru/post/1790877/


All Articles