The log4j configuration file (Configuration file) is used to set the level, the repository, and the layout of the logger, and it can be set in key=value format or in XML format information. By configuring, you can create a log4j running environment. The log4j configuration file is a file called log4j.properties .
log4j.properties File:
#newhappy log4j.properties Start
Log4j.rootlogger=debug,myconsole,mylogfile #console Appender log4j.appender.myconsole= Org.apache.log4j.ConsoleAppender Log4j.appender.myconsole.layout=org.apache.log4j.patternlayout log4j.appender.myconsole.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n Log4j.appender.myconsole.threshold=fatal # Rolling file Appender Log4j.appender.mylogfile=org.apache.log4j.rollingfileappender log4j.appender.myLogFile.File= MyLog.log log4j.appender.mylogfile.maxfilesize=100kb log4j.appender.mylogfile.maxbackupindex=2 Log4j.appender.mylogfile.layout=org.apache.log4j.patternlayout Log4j.appender.mylogfile.layout.conversionpattern=%d{mmm d,yyyy Hh:mm:ss A}:%p [%t]%m%n Log4j.appender.mylogfile.threshold=error
#newhappy Log4j.properties End
1. The basic format of the profile log4j configuration file is as follows: #配置根Logger Log4j.rootlogger = [level], appenderName1, appenderName2, ... #配置 Log information Output destination Appender Log4j.appender.appenderName = Fully.qualified.name.of.appender.class Log4j.appender.appenderName.option1 = value1 ... Log4j.appender.appenderName.optionN = Valuen #配置日志信息的格式 (layout) log4 J.appender.appendername.layout = Fully.qualified.name.of.layout.class Log4j.appender.appenderName.layout.option1 = Value1 ... Log4j.appender.appenderName.layout.optionN = Valuen
where [level] is the log output levels, there are 5 levels:
FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7
Appender for the log output destinations, LOG4J provides the following appender:
Org.apache.log4j.ConsoleAppender (console), Org.apache.log4j.FileAppender (file), Org.apache.log4j.DailyRollingFileAppender (a log file is generated every day), Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size), Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)
Layout: Log output format, LOG4J provides the following Layout:
Org.apache.log4j.HTMLLayout (layout in HTML table), Org.apache.log4j.PatternLayout (you can specify layout patterns flexibly), Org.apache.log4j.SimpleLayout (The level and information string that contains the log information), Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)
Print Parameters: LOG4J uses a print format similar to the printf function in C to format log information as follows:
%m Output code specified in the message %p output priority, that is, debug,info,warn,error,fatal Strong>%r Output The number of milliseconds it takes to output this log information % c output the class to which it belongs, usually the name of the class in which it is used %t Output The thread name that generated the log event %n output a carriage return line break, Windows platform is "/R /n ", a date or time on which the UNIX platform is"/n " %d output log point-in-time, the default format is ISO8601, or the format can be specified thereafter, such as:%d{yyy mmm dd hh:mm:ss, SSS}, output similar: October 18, 2002 22:10:28, 921 %l Output Log event occurrence Location, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10)
2. Initializing logger in code: 1) calling Basicconfigurator.configure () in the program Method: Add a consoleappender to the root logger, the output format is set to %-4r [%t]%-5p%c-%m%n , and the default level of the root logger is Level.debug . 2) configuration is placed in a file, passed the file name through the command-line arguments, and is parsed and configured by Propertyconfigurator.configure (args[x]) ; 3 configuration placed in the file, through the environment variables to pass the file name and other information, using log4j default initialization process analysis and configuration, 4 configuration placed in the file, through the application server configuration to pass the file name and other information, using a special servlet to complete the configuration. 3. To set the log output level for different appender: when debugging a system, we tend to notice only the abnormal level of log output, but usually all levels of output are placed in a file, if the level of log output is a bug. Then go and find it slowly. At this point, we might think how good it would be to output the exception information to a single file. Of course, LOG4J has provided such a feature, we only need to modify the Appender in the configuration Threshold can be implemented, such as the following example: [config file ]
### set log levels ### log4j.rootlogger = Debug, stdout, d, E ### output to control Taiwan ### log4j.appender.stdout = Org.apache.log4j.ConsoleAppender Log4j.appender.stdout.Target = System.out Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout Log4j.appender.stdout.layout.ConversionPattern = %d{absolute} %5p %c{1}:%l - %m%n ### output to log file ### Log4j.appender.D = Org.apache.log4j.DailyRollingFileAppender Log4j.appender.d.file = Logs/log.log Log4j.appender.d.append = True LOG4J.APPENDER.D. Threshold = debug ## output DEBUG-level log log4j.appender.d.layout = Org.apache.log4j.PatternLayout Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd hh:mm:ss} [%t :%r]-[%p] %m%n ### Save exception information to a separate file ### LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.d.file = logs/error.log # # Exception Log file name Log4j.appender.d.append = True LOG4J.APPENDER.D. Threshold = Error # # Only logs above the ERROR level are output!!! Log4j.appender.d.layout = Org.apache.log4j.PatternLayout Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:MM:SS} [%t:%r]-[%p]%m%n
[use in code]
public class Testlog4j {public static void main (string[] args) {propertyconfigurator.configure (" D:/code/conf/log4j.properties "); Logger Logger = Logger.getlogger (testlog4j. Class); Logger.debug ("Debug"); Logger.error ("error"); } }
Run to see if the exception information is saved in a separate file error.log. 4. Set different output information level for different packages eg: print Oppensessioninviewfilter Debug Level information method: Log4j.logger.org.springframework.orm.hibernate3.support.opensessioninviewfilter=debug How to print warn level information in the Struts tab: Log4j.logger.org.apache.struts.util=warn
Principle: Suppose we are no longer interested in seeing the output of any component& nbsp; belonging to the com.foo package. following configuration file shows one possible way of achieving this. log4j.rootlogger=debug, a1 log4j.appender.a1= org.apache.log4j.consoleappender Log4j.appender.a1.layout=org.apache.log4j.patternlayout # print the date in iso 8601 format log4j.appender.a1.layout.conversionpattern=%d [%t] %-5p %c - %m%n # print only messages of level WARN or above in the package com.foo. log4j.logger.com.foo=warn