SpringMVC web 项目使用 logback

Scroll Down

SpringMVC web 项目 logback 使用

Spring 3.X + Dynamic Web Module 2.X

gradle

dependencies {
	// 使用 logback
	compile 'ch.qos.logback:logback-classic:1.2.3'
	compile 'org.logback-extensions:logback-ext-spring:0.1.1'
	compile 'org.slf4j:log4j-over-slf4j:1.7.25'
	compile 'org.slf4j:jcl-over-slf4j:1.7.1'
}

// ...

configurations.all {
    // 在全局配置中排除掉所有的 log4j
    exclude group: "org.slf4j", module: "slf4j-log4j12"
    exclude group: "log4j", module: "log4j"
}
// 或者
configurations {
    // 在全局配置中排除log4j和slf4j-log4j12依赖,使用 logback
    compile.exclude group: 'log4j', module: 'log4j'
    compile.exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    // all*.exclude
}

web.xml 定义

    <context-param>
        <param-name>logbackConfigLocation</param-name>
        <param-value>classpath:logback.xml</param-value>
    </context-param>

    <!-- ... -->

    <listener>
        <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
    </listener>

logback.xml 定义

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <property resource="application.properties"/>
    <property name="APP_NAME" value="multitxl" />
    <property name="LOG_HOME" value="${logback.log.home}" />
    <property name="OPERATION_LOG_HOME" value="${logback.log.operation}" />
    <property name="VISIT_LOG_HOME" value="${logback.log.visit}" />

    <property name="ENCODER_PATTERN"
              value="#${APP_NAME}# %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{30} [%line] [%X{traceId}] - %msg%n" />

    <contextName>${APP_NAME}</contextName>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${ENCODER_PATTERN}</pattern>
        </encoder>
    </appender>

    <appender name="CONSOLE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/console/${APP_NAME}.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/console/${APP_NAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${logback.log.maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>${ENCODER_PATTERN}</Pattern>
        </encoder>
        <!--日志文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>${logback.log.maxFileSize}</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <!-- 错误日志 -->
    <appender name="ERROR_FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/ERROR.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${logback.log.maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${ENCODER_PATTERN}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 操作日志 -->
    <appender name="OPERATION_FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${OPERATION_LOG_HOME}/${APP_NAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!-- 访问日志 -->
    <appender name="VISIT_FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${VISIT_LOG_HOME}/${APP_NAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <logger name="com.sherlocky" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
        <appender-ref ref="CONSOLE_FILE" />
    </logger>

    <logger name="com.ctrip" level="ERROR" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="CONSOLE_FILE" />
    </logger>


    <!-- 默认日志级别是INFO -->
    <root>
        <level value="INFO" />
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
        <appender-ref ref="CONSOLE_FILE" />
    </root>
</configuration>

相关配置参数可配置在application.properties

logback.log.home=${catalina.base}/logs/${APP_NAME}
logback.log.operation=${logback.log.home}/operation
logback.log.visit=${logback.log.home}/visit
logback.log.maxHistory=180
logback.log.maxFileSize=50MB

SpringBoot2 logback 配置可参考Spring Boot学习3——日志管理