Log4j usage and simple example

// ===== log4j.config =====
// #
// log4j.rootLogger=info, stdout
// log4j.logger.default=DEBUG
 
// log4j.appender.stdout=org.apache.log4j.ConsoleAppender
// log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
// log4j.appender.stdout.layout.ConversionPattern=%d [%t] %5p (%F:%L) - %m%n
// #log4j.appender.stdout.layout=%p [%t] (%F:%L) - %m%n
// =====
 
import org.apache.log4j.*;
 
public class TLog
{
    private final static Logger LOGGER = Logger.getLogger("default");
 
    public static void main(String[] args)
    {
        PropertyConfigurator.configure("log4j.config");
        LOGGER.info("Info");
        LOGGER.warn("Warn");
        LOGGER.error("Error");
        LOGGER.debug("Debug");
        LOGGER.fatal("Fatal");
    }
}

Log4j config file example

log4j.rootLogger=debug, stdout, tofile
log4j.logger.default=ALL
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %5p (%F:%L) - %m%n
#log4j.appender.stdout.layout=%p [%t] (%F:%L) - %m%n
 
 
log4j.appender.tofile=org.apache.log4j.RollingFileAppender
log4j.appender.tofile.File=example.log
# Control the maximum log file size
log4j.appender.tofile.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.tofile.MaxBackupIndex=1
log4j.appender.tofile.layout=org.apache.log4j.PatternLayout
log4j.appender.tofile.layout.ConversionPattern=%d [%t] %5p (%F:%L) - %m%n
 
#org.mnain.Dir.Main.logger=DEBUG
#org.mnain.Dir=DEBUG
 
org.mnain.Dir.Main.logger.default=DEBUG

Another example of multiple destinations for log4j

log4j.rootLogger=debug, tofile, altfile
log4j.logger.default=ALL
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %5p (%F:%L) - %m%n
#log4j.appender.stdout.layout=%p [%t] (%F:%L) - %m%n


log4j.appender.tofile=org.apache.log4j.RollingFileAppender
log4j.appender.tofile.File=c:\\madan\\alt_example.log
# Control the maximum log file size
log4j.appender.tofile.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.tofile.MaxBackupIndex=1
log4j.appender.tofile.layout=org.apache.log4j.PatternLayout
log4j.appender.tofile.layout.ConversionPattern=%d [%t] %5p (%F:%L) - %m%n

log4j.appender.altfile=org.apache.log4j.RollingFileAppender
log4j.appender.altfile.File=c:\\madan\\example.log
# Control the maximum log file size
log4j.appender.altfile.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.altfile.MaxBackupIndex=1
log4j.appender.altfile.layout=org.apache.log4j.PatternLayout
log4j.appender.altfile.layout.ConversionPattern=%d [%t] %5p (%F:%L) - %m%n

#org.mnain.Dir.Main.logger=DEBUG
#org.mnain.Dir=DEBUG

org.mnain.Dir.Main.logger.default=DEBUG
org.mnain.Dir.Main.logger.appender=tofile

log4jusage.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0