the three main components of the log4j configuration file:
Logger,appender and layout, respectively, log type, log output destination, log output format.
Log4j.rootlogger = [Level],appendername, Appendername, ... (Level is the error levels, Appendername is the output destination, this example is set to MyLog, you can define multiple) level priority is fatal, ERROR, WARN, INFO, DEBUG 5 levels. By defining the level, You can control the log output in the program. For example, the error level is defined here, and only the faral and error level log will be output.
log4j.appender.mylog= Output Destination (the appendername is defined above, optionally named)
The output destinations provided by LOG4J are as follows:
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (a new file is generated when the file reaches a specified size)
Org.apache.log4j.WriterAppender (send log information to any place in streaming format)
Log4j.appender.filelog.file=your File dir
Log4j.appender.filelog.maxfilesize=your FileSize
Log4j.appender.mylog.maxbackupindex=num setting the number of saved backup files
Log4j.appender.appenderName.layout = Layout type (set layout type)
LOG4J offers the following 4 kinds of layout:
Org.apache.log4j.HTMLLayout (layout in HTML tabular format)
Org.apache.log4j.SimpleLayout (level and information string that contains log information)
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)
Org.apache.log4j.PatternLayout (flexibility to specify layout patterns)
If you use the Patternlayout layout to specify the exact format of the printed information Conversionpattern, the printing parameters are as follows:
%M the message specified in the output code
%p output priority, i.e. Debug,info,warn,error,fatal
%r output the number of milliseconds it takes to boot to output the log information
The class to which the%c output belongs, usually the full name of the class in which it is located
%t output The name of the thread that generated the log event
%n output a carriage return line feed, Windows is "RN", and Unix is "n"
%d output log time, such as:%d{yyyy MMM ddhh:mm:ss,sss}, Output: May 17, 2007 19:30:00,000
%l where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code
[QC] is the beginning of log information, can be any character, generally for the project abbreviation
The program does not use the Log4j class directly, but instead uses the log class provided by Commons-logging. Commons-logging is a unified interface for all Java log implementations, and its functionality is said to be "usually weak".
Log (base logger) and logfactory (responsible for creating the log instance). When Commons-logging.jar is added to the classpath, it makes a reasonable guess at your favorite log tool and then sets it up, and the user doesn't have to do anything. The default logfactory is to follow the steps below to discover and determine which log tool will be used (in order, the search process will be aborted when the first tool is found):
1. Find the value in the current factory named Org.apache.commons.logging.Log configuration property
2. Find the value in the system named Org.apache.commons.logging.Log in the attribute
3. Use the associated wrapper (wrapper) class (Log4jlogger) If there is a log4j in the classpath of the application
4. If the application is running in a jdk1.4 system, use the associated wrapper class (Jdk14logger)
5. Use simple log packing class (Simplelog)