Detailed logback configuration (a) and


Detailed finishing logback common configuration

Not the official website of the manual translated version, but the use of summary, aims to faster and more thorough understanding of its configuration

About logback

logback common configuration in detail (a) <configuration> and <logger>

Detailed logback common configuration (ii) <the appender>

Detailed logback common configuration (c) <filter>

logback Chinese Manual (official website of the manual translated version, where the Chinese version of the translator - Chen Hua, say Headshake)


Detailed Configuration logback (a) <configuration> and <logger>

A: root <configuration> properties comprising:

scan:

When this property is set to true, if the profile change, will be reloaded, the default value is true.

scanPeriod:

Is set with a modified profile monitoring interval, if not given time unit, the default milliseconds. When the scan is true, this property take effect. The default interval is 1 minute.

debug:

When this property is set to true, will print logback internal log information, real-time view logback running. The default value is false.

E.g:

Xml Code


  

<configurationscan="true"scanPeriod="60 seconds"debug="false">

<!-- 其他配置省略-->

</configuration>

II: root <configuration> child nodes:

25093456_Njyu.png

2.1 Set Context Name: <contextName>

Each logger is associated to the logger context, the default context name is "default". May be used <contextName> Other names provided for recording distinguishing different applications. Once set, you can not be modified.

Xml Code


  

<configurationscan="true"scanPeriod="60 seconds"debug="false">

<contextName>myAppName</contextName>

<!-- 其他配置省略-->

</configuration>

2.2 Set the variable: <Property>

Tag is used to define the value of the variable, <property> has two attributes, name and value; wherein the value of the name is the name of the variable, the value of the variable value defined in the value. It will be inserted into the context of a value logger <property> defined. After the defined variables can be made "$ {}" to use variables.

For example, using <property> defined context name, and then use the context provided logger <contentName>.

Xml Code


  

<configurationscan="true"scanPeriod="60 seconds"debug="false">

<propertyname="APP_Name"value="myAppName"/>

<contextName>${APP_Name}</contextName>

<!-- 其他配置省略-->

</configuration>

2.3 Get timestamp string: <timestamp>

Two attributes key: identifying this <timestamp> name; The datePattern: Set the current time (time resolved profile) is converted to the pattern string, followed java.txt.SimpleDateFormat format.

      For example, as a time profile parsed context name:

Xml Code


  

<configurationscan="true"scanPeriod="60 seconds"debug="false">

<timestampkey="bySecond"datePattern="yyyyMMdd'T'HHmmss"/>

<contextName>${bySecond}</contextName>

<!-- 其他配置省略-->

</configuration>

2.4 Setting loger:

<loger>

To set the logging level to print one specific packet or a certain class, and designated <appender>. <Loger> only a name attribute, an optional level addtivity and an optional attribute.

name:

Loger packet to specify one or a specific constraint by this one class.

level:

Used to set the level of printing, its case is: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF, there is a Japanese popular value INHERITED synonyms or NULL, on behalf of the higher level of enforcement.

If this property is not set, then the current level loger will inherit superiors.

addtivity:

Whether passing information to the superior print loger. The default is true.

<Loger> can contain zero or more <appender-ref> element, the identification will be added to the appender loger.

<root>

Also <loger> element, but it is the root loger. Only one level property, should have been named "root".

level:

Used to set the level of printing, its case is: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF, or can not be set INHERITED synonymous NULL.

The default is DEBUG.

<Root> can contain zero or more <appender-ref> element, the identification will be added to the appender loger.

E.g:

LogbackDemo.java class

Java code


  

package logback;  

import org.slf4j.Logger;  

import org.slf4j.LoggerFactory;  

publicclass LogbackDemo {  

privatestatic Logger log = LoggerFactory.getLogger(LogbackDemo.class);  

publicstaticvoid main(String[] args) {  

       log.trace("======trace");  

       log.debug("======debug");  

       log.info("======info");  

       log.warn("======warn");  

       log.error("======error");  

   }  

}  

logback.xml profile

Item 1: Only configure root

Xml Code


  

<configuration>

<appendername="STDOUT"class="ch.qos.logback.core.ConsoleAppender">

<!-- encoder 默认配置为PatternLayoutEncoder -->

<encoder>

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

</encoder>

</appender>

<rootlevel="INFO">

<appender-refref="STDOUT"/>

</root>

</configuration>

Appender configuration which represents printed to the console ( explained in detail later appender );

<Root level = "INFO"> print root level is set to "INFO", specifies the name to "STDOUT" the appender.


When executing the main method logback.LogbackDemo class, root level to the "INFO" and greater than "INFO" configured to log information appender Handled named "STDOUT" the, "STDOUT" appender print to the control station;

Print results are as follows:

Xml Code


  

13:30:38.484 [main] INFO  logback.LogbackDemo - ======info  

13:30:38.500 [main] WARN  logback.LogbackDemo - ======warn  

13:30:38.500 [main] ERROR logback.LogbackDemo - ======error  

No. 2: with loger configuration, no level is specified, do not specify the appender,

Xml Code


  

<configuration>

<appendername="STDOUT"class="ch.qos.logback.core.ConsoleAppender">

<!-- encoder 默认配置为PatternLayoutEncoder -->

<encoder>

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

</encoder>

</appender>

<!-- logback为java中的包 -->

<loggername="logback"/>

<rootlevel="DEBUG">

<appender-refref="STDOUT"/>

</root>

</configuration>

Appender configuration which represents printed to the console ( explained in detail later appender );

<Logger name = "logback" /> to control the printing of all classes logs under logback package, but with no print setting level, so inherited his superiors <root> log level "DEBUG";

Not set addtivity, the default is true, print information is transmitted to the higher this loger;

Not set appender, this loger itself does not print any information.

<Root level = "DEBUG"> print root level is set to "DEBUG", specifies the name to "STDOUT" the appender.


When executing the main method logback.LogbackDemo class, because LogbackDemo logback in the package, it is performed first <logger name = "logback" />, the level "DEBUG" and greater than "DEBUG" log information is transmitted to the root, itself not printed;

root subordinate to the transfer of information, has been configured to handle appender named "STDOUT", and "STDOUT" appender information printed to the console;

Print results are as follows:

Xml Code


  

13:19:15.406 [main] DEBUG logback.LogbackDemo - ======debug  

13:19:15.406 [main] INFO  logback.LogbackDemo - ======info  

13:19:15.406 [main] WARN  logback.LogbackDemo - ======warn  

13:19:15.406 [main] ERROR logback.LogbackDemo - ======error  

No. 3: loger configuration with a plurality of specified level, designated appender

Xml Code


  

<configuration>

<appendername="STDOUT"class="ch.qos.logback.core.ConsoleAppender">

<!-- encoder 默认配置为PatternLayoutEncoder -->

<encoder>

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

</encoder>

</appender>

<!-- logback为java中的包 -->

<loggername="logback"/>

<!--logback.LogbackDemo:类的全路径 -->

<loggername="logback.LogbackDemo"level="INFO"additivity="false">

<appender-refref="STDOUT"/>

</logger>

<rootlevel="ERROR">

<appender-refref="STDOUT"/>

</root>

</configuration>

Appender configuration which represents printed to the console ( explained in detail later appender );


<Logger name = "logback" /> to control the printing of all classes logs under logback package, but with no print setting level, so inherited his superiors <root> log level "DEBUG";

Not set addtivity, the default is true, print information is transmitted to the higher this loger;

Not set appender, this loger itself does not print any information.

<Logger name = "logback.LogbackDemo" level = "INFO" additivity = "false"> control logback.LogbackDemo class log printing, print level "INFO";

additivity property is false, this indicates that the print information is no longer transmitted to the higher loger,

It specifies the name to "STDOUT" the appender.

<Root level = "ERROR"> print root level is set to "ERROR", specifies the name to "STDOUT" the appender.

When executing the main method logback.LogbackDemo class, perform <logger name = "logback.LogbackDemo" level = "INFO" additivity = "false">, log information level is "INFO" and greater than "INFO" to the this loger specified appender named "STDOUT" processing console play log, no loger the superior views of <logger name = "logback" /> transmitting print information;

<Logger name = "logback" /> not received any print information, of course, it does not give any superior root pass print information;

Print results are as follows:

Xml Code


  

14:05:35.937 [main] INFO  logback.LogbackDemo - ======info  

14:05:35.937 [main] WARN  logback.LogbackDemo - ======warn  

14:05:35.937 [main] ERROR logback.LogbackDemo - ======error  

If the <logger name = "logback.LogbackDemo" level = "INFO" additivity = "false"> changed to <logger name = "logback.LogbackDemo" level = "INFO" additivity = "true"> That is what will print the results it?

Yes, the log is printed twice, surely we all know why, because the print information passed to their superiors, logger itself is printed once, root and then print once received

Print results are as follows:

Xml Code


  

14:09:01.531 [main] INFO  logback.LogbackDemo - ======info  

14:09:01.531 [main] INFO  logback.LogbackDemo - ======info  

14:09:01.531 [main] WARN  logback.LogbackDemo - ======warn  

14:09:01.531 [main] WARN  logback.LogbackDemo - ======warn  

14:09:01.531 [main] ERROR logback.LogbackDemo - ======error  

14:09:01.531 [main] ERROR logback.LogbackDemo - ======error  

This article comes from " AUB " blog, be sure to keep this source http://aubdiy.blog.51cto.com/2978849/815750

Reproduced in: https: //my.oschina.net/aub/blog/680835

Guess you like

Origin blog.csdn.net/weixin_34405332/article/details/91550796