Log4j
Jump to navigation
Jump to search
The Apache [1] Log4j is a very useful logging mechanism for Java. The code is here gives explanation to more complex usage based on the KISS principle.
Examples
top The code presented here are examples of working applications.
- Multiple loggers. Duplicates lines are the major problem. To avoid these duplicates the trick is to leave the first item on the rootlogger blank. To set the loggers use the Threshold switch. In the given example the first logger uses the console appender and the second uses a file appender.
Multiple logger 1
# Setting up the different loggers. log4j.rootLogger=,MONITOR, LOGFILE # Appender setup A1 = MONITOR log4j.appender.MONITOR=org.apache.log4j.ConsoleAppender log4j.appender.MONITOR.Threshold=INFO #log4j.appender.MONITOR.File=quiet.log # Appender setup A2 = LOGFILE log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.Threshold=DEBUG log4j.appender.LOGFILE.File=/usr/home/props/logs/StockLogger.log # MONITOR Uses Pattern Layout log4j.appender.MONITOR.layout=org.apache.log4j.PatternLayout log4j.appender.MONITOR.layout.ConversionPattern=%m%n # LOGFILE uses Pattern Layout log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Multiple logger 2
# Set root logger level to DEBUG and its only appender to A100. log4j.rootLogger=,C200,F200 # ----------------------------------------------------------------------------------------------- # C200 is set to ConsoleAppender (Output is visible in the Console) log4j.appender.C200=org.apache.log4j.ConsoleAppender log4j.appender.C200.Threshold=INFO # C200 uses PatternLayout. log4j.appender.C200.layout=org.apache.log4j.PatternLayout log4j.appender.C200.layout.ConversionPattern=%5p [%t-%d{HH:mm:ss,SSS}] (%25F:%L) - %m%n # ----------------------------------------------------------------------------------------------- # F200 is set to be a FileAppender (Output only visible in logfile) log4j.appender.F200=org.apache.log4j.FileAppender log4j.appender.F200.Threshold=DEBUG log4j.appender.F200.File=/usr/home/props/logs/SASComp.log # F200 uses PatternLayout. log4j.appender.F200.layout=org.apache.log4j.PatternLayout log4j.appender.F200.layout.ConversionPattern=%5p [%t-%d{HH:mm:ss,SSS}] (%F:%L) - %m%n
See also
- Apache Log4j HomePage of the Apache Log4j.
- Log4j Version 1.2 HomePage
- Apache Log4j Wiki
Reference
- ↑ Apache Software Foundation Home Website,The Apache Software Foundation, Celebrating a Decade of Open Source Leadership.