1. Introduction of the JAR package
Log4j-1.2.13.jar
2.SRC Setup configuration file: Log4j.properties
#不 +all, write only the latter Loglog4j.rootlogger =all,systemout,logrollingfile #输出到控制台 Log4j.appender.systemOut = Org.apache.log4j.ConsoleAppender Log4j.appender.systemOut.layout = Org.apache.log4j.PatternLayout # "%-5p": Message type (DEBUG? INFO? ), left to it, 5 characters # "%l": that file that method which line output log# "%n": NewLine # "%m": Log Message Log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%- 22d{yyyy/mm/dd HH:mm:ss. sss}][%l]%n%m%n# threshold (Log level: Debug,info,error,warn) Log4j.appender.systemOut.Threshold = debug# setting file size output to file Log4j.appender.logRollingFile = Org.apache.log4j.RollingFileAppender Log4j.appender.logRollingFile.layout = Org.apache.log4j.PatternLayout Log4j.appender.logRollingFile.layout.ConversionPattern =%m% Nlog4j.appender.logRollingFile.Threshold = DEBUG Log4j.appender.logRollingFile.File = c:/log/ahlog.txt Log4j.appender.logRollingFile.MaxFileSize = 1MB Log4j.appender.logRollingFile.MaxBackupIndex = ten
Reference:
http://blog.csdn.net/dr_guo/article/details/50718063 "Log4j.properties configuration detailed and examples "
3. Write the Java test file:
Import Org.apache.log4j.Logger; Public class testlog4j { static Logger log = Logger.getlogger (testlog4j. Class. GetName ()); Public Static void Main (string[] args) { log.debug ("AH Debug"); Log.info ("AH info"); Log.error ("AH error"); Log.warn ("AH warn");} }
Java uses log4j