Log4j profile

xiaoxiao2021-03-06  39

Log4j supports two profile formats, one is a Java property file (key-value), one is an XML format file.

Here only describes the configuration method of the Java property file. First look at a configuration file: log4j.properties

Log4j.rootcategory = Debug, Stdout, R

log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =% 5p [% t] (% F:% L) - % M% N

Log4j.rapnder.r = org.apache.log4j.rollingfileappenderlog4j.rapnder.r.Layout = org.apache.log4j.patternLayoutLog4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N # output results Write example.loglog4j.Appender.r.file = example.loglog4j.rapnder.r.maxfilesize = 100kblog4j.Appender.r.maxbackupindex = 1

1, log4j.rootcategory = [level | null], appendername, appendername, ....

Level: It is the priority of the diary record, priority is divided into OFF, Fatal, Error, Warn, Info, Debug, ALL. Log4J recommends using Fatal, Error, Warn, Info, Debug four levels. The idea saw Null, so it can be empty here.

AppenderName: Specifies which place to output the log information, you can specify multiple output destinations.

2, the syntax of the appender is: log4j.appender.Appendername = Full name of appender's classlog4j.Appender.Appendername.kind = Kind1 ... log4j.appender.Appendername.kind = Kindn

There are 5 appenders, which are org.apache.log4j.consoleappender org.apache.log4j.fileappender (file) org.apache.log4j.dailyRollingFileAppender (a log file is generated every day) org.apache.log4j.writerappender Send log information in a stream format to any specified place)

3, Layout's grammatical: log4j.Appender.Appendername.Layout = Full Name of layout's class log4j.Appender.Appendername.Layout.kinder.Appendername.Layout.kinder.Appendername.Layout.kind = KINDN

There are 4 layout, which is org.apache.log4j.htmlLayout (layout in HTML format form) org.apache.log4j.patternlayout (Custom layout) org.apache.log4j.simplelayout (level and information character with log information) Strings) org.apache.log4j.ttccLayout (including information, thread, category, etc.)

4, the output is based on the% different parameters represent different formatted information (parameters are listed in alphabetical order):% C outputs the full name of the class belonging, can be modified to% D {NUM}, NUM class name output The range is as follows: "org.apache.xyz.someclass",% c {2} will output XYZ.SOMECLASS% D Output log Time Format is% d {YYYY-MM-DD HH: mm: SS, SSS}, Specify formats, such as% D {HH: mm: SS}% l Output log event occurs, including catenial names, threads, running in code% n wrap% m output code specified information, such as info ("Message "), Output Message% P output priority, ie Fatal, Error and other% R output from start to display the log information consumed by the log information to generate thread names that generate the log event can generally understand the log4j.properties file It is already a configuration file by the above steps). However, there are some things that should also be understood: 5, Threshold's grammar

Log4j.threshold = level

Level is the off, Fatal, Error, Warn, Info, Debug, ALL. Threshold is a global filter that will not display the information below the set of Level. Look at an example:

log4j.rootLogger = DEBUG, CONlog4j.appender.CON = org.apache.log4j.ConsoleAppenderlog4j.appender.CON.layout = org.apache.log4j.PatternLayoutlog4j.appender.CON.layout.ConversionPattern = [% t]% -5p% C -% M% N # ONLY Print Log Statement of Level Warn or Above Regardless of Ter (information above WARN level) # Logger.log4j.threshold = WARN

Threshold's usage is flexible, you can also add Threshold in Appender.

log4j.rootLogger = DEBUG, Clog4j.appender.C = org.apache.log4j.ConsoleAppender # Set the appender threshold to INFOlog4j.appender.C.Threshold = INFOlog4j.appender.C.layout = org.apache.log4j.PatternLayoutlog4j.appender .C.Layout.conversionPattern =% - 4R [% T]% -5p% C% X-% M% N

Here is initially set for Debug, but log4j.Appender.c.threshold = INFO is set to INFO. Because INFO> Debug is included, if the information containing the Debug level will be filtered.

6. The Level of a logger is similar to Threshold, but it is not all filtered, but it is reduced to the range of Loggers. Also see an example (specified in the chapter3 package)

log4j.rootLogger = DEBUG, CONlog4j.appender.CON = org.apache.log4j.ConsoleAppenderlog4j.appender.CON.layout = org.apache.log4j.PatternLayoutlog4j.appender.CON.layout.ConversionPattern = [% t]% -5p% c -% m% n # Print only messages of priority WARN or above in package "chapter3" .log4j.logger.chapter3 = WARN following is a description of each package which chapter3 level Logger name Assigned level Effective levelRoot DEBUG DEBUGchapter3 WARN WARNchapter3.MyApp2 null WARNchapter3 .Foo Null Warn

Then the example above about changes: log4j.rootLogger = DEBUG, CONlog4j.appender.CON = org.apache.log4j.ConsoleAppenderlog4j.appender.CON.layout = org.apache.log4j.PatternLayoutlog4j.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 = WARNLOG4J.LOGGER.CHAPTER3 .Foo = debug

You can see the last line to add log4j.logger.chapter3.foo = debug to view the effect by running the program (here will not be detailed) CHAPTER3.foo's level has been rising to debug Logger Name Assigned Level Effective LevelRoot Debug DEBUGCHAPTER3 Warn warnchapter3.myapp2 null warnchapter3.foo debug debug

I have written so much, I'm tired, then take a break, so much content is digested, continue to learn:), understand these foundations is not enough, if you want more deeper understanding. PDF

转载请注明原文地址:https://www.9cbs.com/read-76680.html

New Post(0)