Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create 2 separate log files with one log4j config file?

I can't figure out how to configure my log4j so that my debugLog and my reportsLog are separate from each other (not additive). Why is it, that in the configuration below, the reportsLog is always empty?

log4j.rootLogger=TRACE, stdout, debugLog  log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n  log4j.appender.debugLog=org.apache.log4j.FileAppender log4j.appender.debugLog.File=logs/debug.log log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n  log4j.reportsLog=DEBUG,reportsLog log4j.appender.reportsLog=org.apache.log4j.FileAppender log4j.appender.reportsLog.File=logs/reports.log log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n 

And here is my Java code:

package test;  import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator;  public class HelloLogger {      static final Logger debugLog = Logger.getLogger("debugLog");     static final Logger resultLog = Logger.getLogger("reportsLog");      public static void main(String[] args) {         PropertyConfigurator.configure("log4j.properties");         debugLog.debug("Hello debugLog message");            resultLog.debug("Hello reportsLog message");     }    } 
like image 243
djangofan Avatar asked Mar 11 '12 02:03

djangofan


People also ask

How do you create a new log file for each time the application runs log4j?

File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

Does log4j create log file?

Following is a sample configuration file log4j. properties to generate log files rolling over at midday and midnight of each day. If you wish to have an XML configuration file, you can generate the same as mentioned in the initial section and add only additional parameters related to DailyRollingFileAppender.

How can logger be used in multiple classes?

Use @Log4j annotation with all the classes in which you wish using logger. Benefits: Easy to maintain, easy to track down. Only one object of the logger is created which will be used through out.


1 Answers

Try the following configuration:

log4j.rootLogger=TRACE, stdout  log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n  log4j.appender.debugLog=org.apache.log4j.FileAppender log4j.appender.debugLog.File=logs/debug.log log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n  log4j.appender.reportsLog=org.apache.log4j.FileAppender log4j.appender.reportsLog.File=logs/reports.log log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n  log4j.category.debugLogger=TRACE, debugLog log4j.additivity.debugLogger=false  log4j.category.reportsLogger=DEBUG, reportsLog log4j.additivity.reportsLogger=false 

Then configure the loggers in the Java code accordingly:

static final Logger debugLog = Logger.getLogger("debugLogger"); static final Logger resultLog = Logger.getLogger("reportsLogger"); 

Do you want output to go to stdout? If not, change the first line of log4j.properties to:

log4j.rootLogger=OFF 

and get rid of the stdout lines.

like image 126
laz Avatar answered Oct 04 '22 14:10

laz