# # # set # # #log4j. Rootlogger=debug,stdout,d,e### Output information to console # # #log4j. Appender.stdout=Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern= [%-5p]%d{yyyy-mm-dd hh:mm:ss,sss} method:%l%n%m%n### output Debug levels above the log to=e://Logs/log.log # # #LOG4J.APPENDER.D =Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file= E://Logs/log.logLog4j.appender.d.append =trueLog4j.appender.d.threshold=DEBUG log4j.appender.d.layout=Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n### output error levels above the log to=e://Logs/error.log # # #LOG4J.APPENDER.E =Org.apache.log4j.dailyrollingfileappenderlog4j.appender.e.file=e://Logs/error.logLog4j.appender.e.append =trueLog4j.appender.e.threshold=ERROR log4j.appender.e.layout=Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n#1. Configure the root logger #log4j. Rootlogger=[level], Appendername, Appendername, ... #2. Configure log information output destination Appender #org. Apache.log4j.ConsoleAppender (console), #org. Apache.log4j.FileAppender (file), #org. APA Che.log4j.DailyRollingFileAppender (generates a log file every day), #org. Apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size), #o Rg.apache.log4j.WriterAppender (send log information in stream format to any specified place) #3Configure the format (layout) of the log information #org. apache.log4j.HTMLLayout (layout in HTML table), #org. Apache.log4j.PatternLayout (with flexibility to specify layout mode), # Org.apache.log4j.SimpleLayout (contains the level and information string for log information), #org. Apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log log4j formats the log information in a print format similar to the printf function in C, with the following printing parameters:%the message specified in the M output code #%P-Output priority, i.e. Debug,info,warn,error,fatal #%r output The number of milliseconds to output the log information from the app startup #%c output belongs to the class, which is usually the full name of the class #%T output the name of the thread that generated the log event #%n outputs a carriage return newline character, the Windows platform is "RN" and the UNIX platform is "n" #%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921 #%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
PackageCom.zhao.main;ImportOrg.apache.log4j.BasicConfigurator;ImportOrg.apache.log4j.Logger;ImportOrg.apache.log4j.PropertyConfigurator; Public classTest {//1: Get logger, this logger will be responsible for controlling log information Private StaticLogger Logger = Logger.getlogger (Test.class); Public Static voidMain (string[] args) {//2: When the logger is obtained, the second step configures the LOG4J environment with the following syntax: /** Basicconfigurator.configure (); Automatically and quickly use the default log4j environment. * Propertyconfigurator.configure (* "D:\\project\\eclipse\\log4j\\src\\log4j.properties"): * Read the properties file using Java The configuration file that was written. Domconfigurator.configure (String filename) *: Reads the configuration file in XML form. */Propertyconfigurator.configure ("D:\\project\\eclipse\\log4j\\src\\log4j.properties"); //3. Inserting record information (formatting log information)//record the debug level informationLogger.debug ("This is debug message.")); //record information at info levelLogger.info ("This is info message.")); //record the error level informationLogger.error ("This is error message.")); }}
LOG4J Preliminary configuration