How to use log4j in your project ?

Description: This blog introduces two log4j log file patterns we often use in our java project


Log4j Pattern Note

Pattern1

You can use this log pattern when you want log info be written into multi-log files instead of a single large file.

#file name : log4j.properties
log4j.rootLogger = appendername

log4j.appender.appendername=org.apache.log4j.RollingFileAppender    
# which file appender pattern your choose 

log4j.appender.appendername.File=log/rlog.log                        
# name of the log file in which the program going to write 

log4j.appender.appendername.MaxFileSize = 100KB                        
# threshold of the log file , if current log file size >= 100 KB 
# log4j will create a new log file to write in

log4j.appender.appendername.MaxBackupIndex = 2                        
# limit the maximum number of log file log4j will create
# here we set max log file number equals to 2 , if log4j create 2 files
# next time , if the rlog.log.2's size is >= 100KB ,
# log4j will clean the rlog.log.1's content and continue writing new log info into rlog.log.1

log4j.appender.appendername.layout = org.apache.log4j.PatternLayout
log4j.appender.appendername.layout.ConversionPattern = %p %t %c - %m %n  # output log info pattern                                                                

Pattern2

You can use this log pattern if you would like your log info output to both console and file

This pattern also supports separating error log info into a single error log file

# file name : log4j.properties
log4j.rootLogger = debug,FILE,CONSOLE,ERRORFILE

# output log info to console
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = [%p] %t %c %l - %m %n

# %p log info level : {DEBUG,INFO,WARN,ERROR,FATAL}
# %t name of the thread that generates log info
# %c log info is generated by which class 
# %l which class , which thread , on which line 's event produce the log info
# %m output code's info

# output log info to log files
log4j.appender.FILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File = logs/rlog.log
log4j.appender.FILE.Threshold = DEBUG  
# this is used to limit the log level , 
#if the log's level is higher than DEBUG ( like INFO, ERROR) output log info

log4j.appender.FILE.MaxFileSize=50KB
log4j.appender.FILE.MaxBackupIndex = 2
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = [%p] %t %c %l - %m %n

# output error (log level >= ERROR) into a single file
log4j.appender.ERRORFILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERRORFILE = logs/error.log
log4j.appender.ERRORFILE.Threshold = ERROR
log4j.appender.ERRORFILE.layout = org.apache.log4j.PatternLayout
log4j.appender.ERRORFILE.layout.ConversionPattern =  [%p] %t %c %l - %m %n

Example

Here is a simple example to use log4j to output log info


// log4j.properties is under 'resources' folder

public class TestLog4j{
    public static void main (String [] args ){
        URL url = new URL("/log4j.properties") ;
        PropertyConfigurator.configure(url.getPath()) ;
        Logger logger = Logger.getLogger(TestLog4j.class) ;

        logger.debug("this is a debug message") ;
        logger.error("error info will output to two files") ;
    }

}

end