steps:
- Create a new Java project and import the log4j jar package;
- Create the Log4j.properties profile and set it in the SRC directory;
1 # Set the global output level2Log4j.rootlogger=Error,stdout,file3 4 # Output logs above the error level to the console5log4j.appender.stdout=Org.apache.log4j.ConsoleAppender6log4j.appender.stdout.target=System.out7log4j.appender.stdout.layout=Org.apache.log4j.PatternLayout8Log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%-5p%c{1}:%l-%m%N9 Ten # Output error level above log to E:\\test.log Onelog4j.appender.file=Org.apache.log4j.RollingFileAppender Alog4j.appender.file.file=E:\\test.log -Log4j.appender.file.maxfilesize=5MB -log4j.appender.file.maxbackupindex=10 thelog4j.appender.file.layout=Org.apache.log4j.PatternLayout -Log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd Hh\:mm\:ss}%-5p%c{1}\:%l-%m%n
- Logging in Java code;
1 PackageBasic;2 3 ImportOrg.apache.log4j.Logger;4 5 Public classLog4jtest {6 7 Private Static FinalLogger Logger = Logger.getlogger (log4jtest.class);8 9 Public Static voidMain (string[] args) {TenLogger.error ("This is debug message.")); One } A}
- Output Result:
Results of the console output:
Results of the log file output:
log4j (i)--log4j Introductory example