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"); } }
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With