SpringMVC web 项目使用 logback

# Java  /  web  /  Spring  /  log

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 name="APP_NAME" value="school-project-demo" />
    <property name="LOG_HOME" value="${catalina.base}/logs/${APP_NAME}" />
    <property name="OPERATION_LOG_HOME" value="${catalina.base}/logs/operation" />
    <property name="VISIT_LOG_HOME" value="${catalina.base}/logs/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="COLLECTION" class="cn.cycore.chain.logback.BraveAppender">
    </appender>
    <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="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>7</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>
    <!-- 用户详细访问URL日志 -->
    <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.app" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
        <appender-ref ref="COLLECTION" />
    </logger>
    <logger name="com.app.cache.JedisCacheUtil" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
    </logger>
    <logger name="com.app.cache.LocalCacheUtil" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
    </logger>
    <logger name="com.rework" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
        <appender-ref ref="COLLECTION" />
    </logger>
    <logger name="conf" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ERROR_FILE" />
    </logger>
    <logger name="com.ctrip" level="ERROR" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
    <!-- 用户操作行为 -->
    <logger name="com.app.log.OperationLogUtil" level="INFO" additivity="false">
        <appender-ref ref="OPERATION_FILE" />
    </logger>
    <!-- 用户详细访问URL日志 -->
    <logger name="com.app.aq.useract.service.UserActService" level="INFO" additivity="false">
        <appender-ref ref="VISIT_FILE" />
    </logger>
    <logger name="com.app.log.LoggingAspect" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="COLLECTION" />
    </logger>

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

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

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×