LOG4J supports two configuration file formats, one is the Java property file (the key-value) and the other is the XML format file.
Here is a description of how to configure the Java properties file.
First look at the configuration file: Log4j.properties
Log4j.rootcategory=debug,stdout,r
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n
Log4j.appender.r=org.apache.log4j.rollingfileappender
Log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%p%t%c-%m%n
#输出结果将写入 Example.log
Log4j.appender.r.file=example.log
log4j.appender.r.maxfilesize=100kb
Log4j.appender.r.maxbackupindex=1
1, log4j.rootcategory=[level | null],appendername,appendername, ....
Level: Is the priority of the journaling record, which is divided into off from high to low, FATAL, ERROR, WARN, INFO, DEBUG, all. LOG4J recommends using only the four levels of fatal, ERROR, WARN, INFO, and Debug. The idea is to see the null, so it can be empty here.
Appendername: Specifies where the log information is to be exported, and multiple output destinations can be specified at the same time.
2, the Appender syntax is:
Log4j.appender.appendername=full name of Appender ' s class
Log4j.appender.appendername.kind=kind1
...
Log4j.appender.appendername.kind=kindn
There are 5 kinds of appender, respectively
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)
3, the layout syntax is:
Log4j.appender.appendername.layout=full name of layout ' s class
Log4j.appender.appendername.layout.kind=kind
...
Log4j.appender.appendername.layout.kind=kindn
There are 4 kinds of layout, respectively
Org.apache.log4j.HTMLLayout (layout in HTML form)
Org.apache.log4j.PatternLayout (Custom layout)
Org.apache.log4j.SimpleLayout (level and information string that contains log information)
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc. of the log generation)
4, output
All the different parameters represent different formatting information (the parameters are listed in alphabetical order) after the start of%:
The full name of the class to which the%c output belongs, can be modified to%d{num}, the range of the Num class name output such as: "Org.apache.xyz.SomeClass",%c{2} will output xyz. SomeClass
%d output log time in the format of%d{yyyy-mm-dd hh:mm:ss,sss}, you can specify a format such as%d{hh:mm:ss}
%l output Log event occurrence location, including class name, occurrence thread, number of lines in code
%n line break
%m output code to specify information, such as info ("message"), output message
%p output priority, that is, FATAL, ERROR, etc.
%r output the number of milliseconds it takes to display the log information from boot to
%t output The name of the thread that generated the log event
You can get a general idea of the Log4j.properties file (through the previous steps are already a configuration file). But there are some that you should know:
5, the Threshold grammar
Log4j.threshold=level
Level is the previous said off, FATAL, ERROR, WARN, INFO, DEBUG, all. Threshold is a global filter that filters out information that is below the level set.
Look at an example:
Log4j.rootlogger=debug, CON.
Log4j.appender.con=org.apache.log4j.consoleappender
Log4j.appender.con.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.CON.LAYOUT.CONVERSIONPATTERN=[%T]%-5p%c-%m%n
#Only Print Log statement of level WARN or above regardless of (print only information at WARN levels)
#logger.
Log4j.threshold=warn
The use of threshold is flexible and can be added to the appender threshold
Log4j.rootlogger=debug, C
Log4j.appender.c=org.apache.log4j.consoleappender
# Set The appender threshold to INFO
Log4j.appender.c.threshold=info
Log4j.appender.c.layout=org.apache.log4j.patternlayout
log4j.appender.c.layout.conversionpattern=%-4r [%t]%-5p%c%x-%m%n
This initial setting level is debug, but Log4j.appender.c.threshold=info is set to INFO. Because Info>debug so if the DEBUG level information is included, it will be filtered.
6. Level of a logger
Similar to threshold, filtering is not all but narrowing the range it filters to the loggers level.
See Likewise an example (designation in the CHAPTER3 package inside)
Log4j.rootlogger=debug, CON.
Log4j.appender.con=org.apache.log4j.consoleappender
Log4j.appender.con.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.CON.LAYOUT.CONVERSIONPATTERN=[%T]%-5p%c-%m%n
# Print only messages of the priority WARN or above in package ' Chapter3 '.
Log4j.logger.chapter3=warn
The following is a description of the level of the Chapter3 package
Logger name assigned level effective level
Root Debug Debug
Chapter3 WARN WARN
Chapter3. MYAPP2 NULL WARN
Chapter3. Foo NULL WARN
Then change the example above:
Log4j.rootlogger=debug, CON.
Log4j.appender.con=org.apache.log4j.consoleappender
Log4j.appender.con.layout=org.apache.log4j.patternlayout
log4j.appender.con.layout.conversionpattern=%d%-5p%c-%m%n
# Allow Requests level WARN or above in ' Chapter3 ' package except in
# "Chapter3." Foo "where DEBUG or above is allowed.
Log4j.logger.chapter3=warn
Log4j.logger.chapter3.foo=debug
You can see an increase in the log4j.logger.chapter3.foo=debug on the last line
You can view the effect by running the program (this will not describe the program in detail)
Chapter3.foo's level has gone up to DEBUG.
Logger name assigned level effective level
Root Debug Debug
Chapter3 WARN WARN
Chapter3. MYAPP2 NULL WARN
Chapter3. Foo Debug Debug
Write so much believe also tired of it, then take a break, so much content to digest the understanding, continue to learn later: To understand these basic things are not enough, if you want to more in-depth understanding of the recommended log4j the Complete manual.pdf