Project Management 5: Manage Application Logs Based on Logback

The current project requirements for output logs:

1. Different log levels are output to different files and consoles.

2. The log is output to different files according to time and specific format. It is automatically cleared after a period of time.

3. Programs under different package paths can set different log levels.

Now the logback we use can just solve the above problems, share it, and hope it will help those who see it.

The logback configuration is as follows (the following is the configuration of a common Java project):

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger [%file:%line] [%X{logid}]- %msg%n
            </Pattern>
        </encoder>
    </appender>
	
    <appender name="ERR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/logback_test.err.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>
                logs/logback_test.err.log.%d{yyyy-MM-dd}
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <!-- Threshold filter: filter out logs below the specified threshold -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <encoder>
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger [%file:%line] [%X{logid}]- %msg%n
            </Pattern>
        </encoder>
    </appender>
	
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/logback_test.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>
                logs/logback_test.log.%d{yyyy-MM-dd}
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger [%file:%line] [%X{logid}]- %msg%n
            </Pattern>
        </encoder>
    </appender>
	
    <appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/access.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>
                logs/access.log.%d{yyyy-MM-dd}
            </fileNamePattern>
            <maxHistory>120</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>
                [% X {logs}] -% msg% n
            </Pattern>
        </encoder>
    </appender>
	
    <appender name="QUERY" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/query.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>
                logs/query.log.%d{yyyy-MM-dd}
            </fileNamePattern>
            <maxHistory>120</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>
                [%X{logid}]- %d{yyyy-MM-dd HH:mm:ss}\t%msg%n
            </Pattern>
        </encoder>
    </appender>
    
    <appender name="UPDATE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/update.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>
                logs/update.log.%d{yyyy-MM-dd}
            </fileNamePattern>
            <maxHistory>120</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>
                [%X{logid}]- %d{yyyy-MM-dd HH:mm:ss}\t%msg%n
            </Pattern>
        </encoder>
    </appender>
	
    <logger name="com.mylogback">
        <level value="ERROR"/>
        <appender-ref ref="FILE"/>
    </logger>
	
    <root>
        <level value="WARN"/>
        <!--
        <appender-ref ref="FILE"/>
        -->
        <appender-ref ref="ERR"/>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>
It's easy to understand:

The code below the root package is output at the WARN level, which is output to the console and ERR file respectively.

The code below the com.mylogback package uses ERROR level output and only outputs to the Appender named FILE.

Among them, the log will create a file every day according to the time, and maxHistory is the maximum number of days for the log to be saved.

 

Expand your knowledge:

About MDC (Mapped Diagnostic Environment):

Add a unique stamp to each client, and the user puts the environment information into the MDC.

String logid = StringUtils.stripToNull(form.getFirstValue("logid", true));
if (null == logid) {
     logid = "-" + Math.abs(RANDOM_GENERATOR.nextLong());
}
MDC.put("logid", logid);

When each client connects, a random number is assigned and put into the MDC. 

Then configure in logback.xml:

<encoder>
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger [%file:%line] [%X{logid}]- %msg%n
            </Pattern>
</encoder>

Where %X{logid} represents the logid in the output MDC.

 

About filters [see Resources]:

LevelFilter : Level filter, filter according to the log level. If the log level is equal to the configured level, the filter accepts or rejects logs based on onMath and onMismatch. Has the following child nodes:

<level>: set the filter level

<onMatch>: used to configure actions that match the filter conditions

<onMismatch>: Used to configure actions that do not meet the filter conditions

<filter class="ch.qos.logback.classic.filter.LevelFilter">   

      <level>INFO</level>   

      <onMatch>ACCEPT</onMatch>   

      <onMismatch>DENY</onMismatch>   

</filter>

ThresholdFilter : Threshold filter to filter out logs below the specified threshold. When the log level is at or above the critical value, the filter returns NEUTRAL; when the log level is below the critical value, the log is rejected.

<!-- Filter out TRACE and DEBUG level logs-->   

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">   

      <level>INFO</level>   

</filter> 

EvaluatorFilter : Evaluate the filter to evaluate and identify whether the log meets the specified conditions.

<evaluator>:

Discriminator, the commonly used discriminator is JaninoEventEvaluato, which is also the default discriminator. It takes any java boolean expression as the evaluation condition. The evaluation condition is explained in the configuration file and successfully compiled dynamically, and the boolean expression returns true. Indicates that the filter conditions are met. The evaluator has a subtag <expression>, which is used to configure the evaluation conditions.

 

About Logback initialization:

You can refer to my other blog: http://shensy.iteye.com/blog/1622612 Log initialization tool

 

References:

http://blog.csdn.net/haidage/article/details/6794540 Detailed explanation of common configuration of logback.xml (series of articles)

http://hanhongke123.blog.163.com/blog/static/62223494201241741237345/ logback configuration

Guess you like

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