Logback. xml common configurations & lt; filter & gt;, logbacklogback. xml

Source: Internet
Author: User

Logback. xml common configurations <filter> and logbacklogback. xml

<Filter>:

If you execute a filter, an enumerated value, namely DENY, NEUTRAL, and ACCEPT, will be returned. If DENY is returned, the log will be immediately discarded and will not pass through other filters. If NEUTRAL is returned, the next filter in the ordered list will process the log. If ACCEPT is returned, the log will be processed immediately, the filter is no longer used.

Filter added<Appender>, Is<Appender>After one or more filters are added, logs can be filtered using any conditions.<Appender>When multiple filters exist, they are executed in the configuration order.

Below are several common filters:

LevelFilter:Level filter, which is based on the log level. If the log level is equal to the configuration level, the filter will receive or reject logs based on onMath and onMismatch. The following sub-nodes are available:

<Level>: sets the filtering level.

<OnMatch>: used to configure operations that meet the filtering conditions.

<OnMismatch>: used to configure operations that do not meet the filter conditions.

For example, if the Log Level of the filter is set to INFO, all INFO-level logs are sent to appender for processing. logs other than INFO-level logs are filtered out.

 1 <configuration>    2   <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">    3     <filter class="ch.qos.logback.classic.filter.LevelFilter">    4       <level>INFO</level>    5       <onMatch>ACCEPT</onMatch>    6       <onMismatch>DENY</onMismatch>    7     </filter>    8     <encoder>    9       <pattern>   10         %-4relative [%thread] %-5level %logger{30} - %msg%n   11       </pattern>   12     </encoder>   13   </appender>   14   <root level="DEBUG">   15     <appender-ref ref="CONSOLE" />   16   </root>   17 </configuration>  

ThresholdFilter:The critical value filter filters out logs lower than the specified critical value. When the log level is equal to or higher than the critical value, the filter returns NEUTRAL. When the log level is lower than the critical value, the log is rejected.

For example, filter out all logs below INFO level.

1 <configuration> 2 <appender name = "CONSOLE" 3 class = "ch. qos. logback. core. ConsoleAppender"> 4 <! -- Filter logs at the TRACE and DEBUG levels --> 5 <filter class = "ch. qos. logback. classic. filter. thresholdFilter "> 6 <level> INFO </level> 7 </filter> 8 <encoder> 9 <pattern> 10%-4 relative [% thread] %-5 level % logger {30}-% msg % n 11 </pattern> 12 </encoder> 13 </appender> 14 <root level = "DEBUG"> 15 <appender-ref = "CONSOLE "/> 16 </root> 17 </configuration>

EvaluatorFilter:Evaluate the filter to evaluate and identify whether the log meets the specified conditions. There are all-in-one nodes:

<Evaluator>:

The common authenticator is JaninoEventEvaluato, which is also the default authenticator. It uses any Java Boolean expression as the evaluate condition. The evaluate condition has been interpreted successfully in the configuration file and is dynamically compiled, if the Boolean expression returns true, it indicates that the filter conditions are met. The evaluator has a sub-tag <expression> used to configure the evaluate condition.

The evaluate expression acts on the current log. logback exposes various log fields to the evaluate expression:

Name Type Description
Event LoggingEvent The original record event associated with the record request. All the variables below come from the event. For example, event. getMessage () returns the same string as "message" below.
Message String The original message of the log. For example, if logger mylogger is set, the value of "name" is "AUB". For mylogger.info ("Hello {}", name ); "Hello {}" is the original message.
FormatedMessage String For example, if there is a logger mylogger and the value of "name" is "AUB", for mylogger.info ("Hello {}", name ); "Hello Aub" is the formatted message.
Logger String Logger name.
LoggerContext LoggerContextVO The logger context of the log.
Level int The integer corresponding to the level, soLevel> INFOIs the correct expression.
TimeStamp long The timestamp of the created log.
Marker Marker The Marker object associated with the log request. Note that "Marker" may be null, so make sure it cannot be null.
Mdc Map Including all MDC values during log creation. The access method is:Mdc. get ("myKey "). Mdc. get () returns an Object that is not a String. To call the String method, perform a strong conversion. For example,

((String) mdc.get("k")).contains("val"). MDC may be null. note when calling.

Throwable Java. lang. Throwable If no exception occurs, the "throwable" variable associated with the log is null. Unfortunately, "throwable" cannot be serialized. It is always null in the remote system. For location-independent expressions, use the following variables.throwableProxy
ThrowableProxy IThrowableProxy The exception proxy associated with the log event. If no exception is associated with a log event, the variable "throwableProxy" is null. When the exception is associated with a log event, "throwableProxy" is not null on the remote system.

 

<OnMatch>: used to configure operations that meet the filtering conditions.

<OnMismatch>: used to configure operations that do not meet the filter conditions.

For example, filter out all logs that do not contain the "billing" string.

 
1 <configuration> 2 3 <appender name = "STDOUT" class = "ch. qos. logback. core. consoleAppender "> 4 <filter class =" ch. qos. logback. core. filter. evaluatorFilter "> 5 <evaluator> <! -- The default value is ch. qos. logback. classic. boolex. janinoEventEvaluator --> 6 <expression> return message. contains ("billing "); </expression> 7 </evaluator> 8 <OnMatch> ACCEPT </OnMatch> 9 <OnMismatch> DENY </OnMismatch> 10 </filter> 11 <encoder> 12 <pattern> 13%-4 relative [% thread] %-5 level % logger-% msg % n 14 </pattern> 15 </encoder> 16 </appender> 17 18 <root level = "INFO"> 19 <appender-ref = "STDOUT"/> 20 </root> 21 </configuration>

<Matcher>:

Although the matches () method of the String class can be used for Pattern matching, a new Pattern object will be created each time a filter is called. To eliminate this overhead, you can predefine one or more matcher objects and then reference them again in the evaluate expression. <Matcher> is a sub-tag of <evaluator>.

<Matcher> contains two sub-tags: one is <name>, which is used to define the matcher name. The value expression uses this name to reference matcher. The other is <regex>, used to configure matching conditions.

 1 <configuration debug="true">    2     3   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">    4     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">    5       <evaluator>            6         <matcher>    7           <Name>odd</Name>    8           <!-- filter out odd numbered statements -->    9           <regex>statement [13579]</regex>   10         </matcher>   11            12         <expression>odd.matches(formattedMessage)</expression>   13       </evaluator>   14       <OnMismatch>NEUTRAL</OnMismatch>   15       <OnMatch>DENY</OnMatch>   16     </filter>   17     <encoder>   18       <pattern>%-4relative [%thread] %-5level %logger - %msg%n</pattern>   19     </encoder>   20   </appender>   21    22   <root level="DEBUG">   23     <appender-ref ref="STDOUT" />   24   </root>   25 </configuration>  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.