Common tags used in logback xml configuration analysis

The following content is a summary of personal understanding and use, I hope it can help you

Logback  configuration understanding:

The use of Logback mainly depends on the three classes Logger , Appender  and  Layout  .

     Logger: Logging is used. After it is associated with the corresponding context of the application, it is mainly used to store log objects. It can also define log types and levels.

Appender: used to specify the destination of log output, the destination can be console, file, database, etc.

Layout: Responsible for converting events into strings and outputting formatted log information.

 

1. About the sequence of configuration files:

logback  first looks  for logback.groovy in the project root directory , if not, use logback-test.xml , if there is no logback-test.xml , use  logback.xml  if none of the above three are output to the console. (I am using logback[-test].xml )

2. Root node  configuration :

configuration  has three optional properties:

    a , scan attribute: used to set whether to reload when the configuration file changes, the value is  true\false  , the default is true , that is , reload when the configuration changes

    b . scanPeriod attribute: used to set how often to check whether the configuration changes, the default is 1 minute, and the default unit is milliseconds

    c , debug attribute: used to set whether to print out the internal log of logback , that is, output the configuration loading information at startup. The value is true\false  and the default is false

3. property  node :

       Set the variable to be used or shared, property contains two attributes name and value ; the value of name is the name of the variable, and the value of value is the value represented by the variable. Values ​​defined by <property> can be added to the logger context using " ${ variable name } " . E.g:

       

<!--Define the directory of the log log output file-->
<property name="outFilePath" value="E:/个人/demo/logs/demo.log" />
<!--Define log output format-->
<property name="enPattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
<appender name="STDOUT" class="ch.qos.logback.core.FileAppender">
        <file>${outFilePath}</file>
        <append>true</append>
        <encoder charset="UTF-8">
            <pattern>${enPattern}</pattern>
        </encoder>
</appender>

 

The outFilePath and enPattern defined by the Property in the above example are used in the file node in the appender node . In this way, it is convenient to uniformly adjust the directory of the log output.

4. There can be multiple appender nodes , and different output for different packages or classes. If the appender outputs to a file, you can set the append subnode value true\false , whether to append the log at the end or replace the log, when the value is true means to append.

You can use the filter node (filter) in the appender to filter the log level (specified by the level node) in this node. After filtering, an enumeration value will be returned, that is , one of DENY , NEUTRAL , and ACCEPT :

    A. Return to DENY , the log will be discarded immediately and no longer pass through other filters;

    B. Return to NEUTRAL , and the next filter in the ordered list will then process the log;

    C. Return to ACCEPT , the log will be processed immediately without passing through the remaining filters

If you need to use these values ​​for processing, you need to use onMatch ( level or greater than level ) and onMismatch (level less than level )

5. logger node:

Used to set the log printing level of a package or a specific class, and specify the appender . The logger has only one name attribute and two optional level and  addtivity attributes.

Name attribute : used to specify a package or a specific class that is constrained by this logger .

Level property: used to set the print level, case-insensitive: TRACE, DEBUG, INFO, WARN, ERROR

If this property is not set, the current logger will inherit the level of the superior.

Addtivity property: used to set whether to pass printing information to the upper-level logger . Default is true .

A logger can contain zero or more appender-ref elements, identifying the appender that will be added to this logger

6. The root node:

Also a <loger> element, but it is the root logger . There is only one level attribute, which should have been named "root".

Level : used to set the print level, case-insensitive: TRACE, DEBUG, INFO, WARN, ERROR . Default is DEBUG .

root can contain zero or more appender-ref elements, identifying this appender will be added to this logger

7. Explanation of the output pattern defined by the pattern node:

%d{HH:mm:ss.SSS}   current time;

[%thread] [ current thread name ]

%-5level logger level

%logger{36} logger name

Output defined by -%msg%n  

%line    output log line number

nNewline

%date and %d{yyyy-MM-dd HH:mm:ss.SSS}    current time includes year month day

{%C.java:%L}  The java class, line and path where the output log is located can be replaced by [%thread]

 

Official reference: http://logback.qos.ch/manual/index.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326990577&siteId=291194637