logback配置文件

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <timestamp key = "timePattern" datePattern = "yyyy-MM-dd"/>
    <springProperty scope="context" name="sysCode" source="open.tenant.code" defaultValue="default"/>
    <springProperty scope="context" name="appCode" source="spring.application.name" defaultValue="default"/>
    <springProperty scope="context" name="appInstGrpCode" source="spring.profiles.active" defaultValue="default"/>
    <springProperty scope="context" name="appInstId" source="open.tenant.instant-id" defaultValue=""/>
    <property name="log.charset" value="UTF-8"/>
    <property name="log.file-size" value="100 MB"/>

    <!--输出到控制台-->
    <appender name="debugConsoleWithColor" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%highlight([%-5p]) %blue([${PID:- }]) %yellow([%date{yyyy-MM-dd HH:mm:ss.SSS}]) %green([${sysCode:-},${appCode:-},${appInstGrpCode:-},${appInstId:-}]) %magenta([%t] %-40.40logger{39}[%line]) : %cyan(%msg%n)</pattern>
            <charset>${log.charset}</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
    </appender>

    <!--输出到控制台-->
    <appender name="debugConsole" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%-5p] [${PID:- }] [%date{yyyy-MM-dd HH:mm:ss.SSS}] [${sysCode:-},${appCode:-},${appInstGrpCode:-},${appInstId:-}] [%t] %-40.40logger{39} [%line] :%msg%n</pattern>
            <charset>${log.charset}</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
    </appender>

    <!--输出到文件-->
    <appender name="info-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>[%-5p] [${PID:- }] [%date{yyyy-MM-dd HH:mm:ss.SSS}] [${sysCode:-},${appCode:-},${appInstGrpCode:-},${appInstId:-}] [%t] %-40.40logger{39} [%line] :%msg%n </pattern>
            <charset>${log.charset}</charset>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily 配置日志所生成的目录以及生成文件名的规则 -->
            <fileNamePattern>/app/logs/%d{yyyy-MM-dd}/${appCode}/${appCode}-info.%i.log</fileNamePattern>
            <MaxHistory>36</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 64 MB -->
                <maxFileSize>${log.file-size}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>info</level>
        </filter>
    </appender>
    <!--输出到文件-->
    <appender name="error-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>[%-5p] [${PID:- }] [%date{yyyy-MM-dd HH:mm:ss.SSS}] [${sysCode:-},${appCode:-},${appInstGrpCode:-},${appInstId:-}] [%t] %-40.40logger{39} [%line] :%msg%n </pattern>
            <charset>${log.charset}</charset>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily 配置日志所生成的目录以及生成文件名的规则 -->
            <fileNamePattern>/app/logs/%d{yyyy-MM-dd}/${appCode}/${appCode}-error.%i.log</fileNamePattern>
            <MaxHistory>36</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 64 MB -->
                <maxFileSize>${log.file-size}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>error</level>
        </filter>
    </appender>

    <springProfile name="production||testing||pro||test">
        <root level = "INFO">
            <appender-ref ref="debugConsole"/>
        </root>
    </springProfile>


    <springProfile name="!(production||testing||pro||test)">
        <logger name="org.springframework.jdbc.datasource" level="debug" />
        <logger name="org.mybatis.spring.transaction" level="debug" />
        <logger name="com.ibatis" level="debug" />
        <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="debug" />
        <logger name="com.ibatis.common.jdbc.ScriptRunner" level="debug" />
        <logger name="com.ibatis.common.sqlmap.engine.impl.SqlMapClientDelegate" level="debug" />
        <logger name="java.sql.Connection" level="debug" />
        <logger name="java.sql.Statement" level="debug" />
        <logger name="java.sql.ResultSet" level="debug" />
        <logger name="java.sql.PreparedStatement" level="debug" />
        <root level = "INFO">
            <appender-ref ref="debugConsoleWithColor"/>
        </root>
    </springProfile>

</configuration>
上一篇:定义防止sql注入的拦截器


下一篇:通过注解和切面实现日志记录