Logback配置

2019-12-19 21:18:17 浏览数 (1)

logback日志配置,日志文件大小达10m分割一次,保存30天

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<!--
 scan  : 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。
 scan  : 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
 debug : 是否输出logback内部日志。默认false
 -->
<Configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- 上下文名称,用于区分不同应用程序的记录 -->
    <contextName>app-name</contextName>

    <!-- property 设置变量 -->
    <property name="log_home" value="logs"/>
    <property name="log_pattern_color" value="%green(%d{yyyy-MM-dd HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %cyan(%logger{50}) : %msg%n"/>
    <property name="log_pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} : %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--
                %d           时间
                %thread      线程名
                %level       日志等级
                %logger      类名
                %msg         日志内容
                %n           换行
            -->
            <pattern>${log_pattern_color}</pattern>
        </encoder>
    </appender>

    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${log_home}/app-name-info-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
            <MaxFileSize>10MB</MaxFileSize>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${log_pattern}</pattern>
        </encoder>

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>


    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${log_home}/app-name-warn-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
            <MaxFileSize>10MB</MaxFileSize>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${log_pattern}</pattern>
        </encoder>

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${log_home}/app-name-error-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
            <MaxFileSize>10MB</MaxFileSize>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${log_pattern}</pattern>
        </encoder>

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <root level="INFO">
        <!-- 控制台输出 -->
        <appender-ref ref="STDOUT"/>
        <!-- 文件输出 -->
        <appender-ref ref="INFO_FILE"/>
        <appender-ref ref="WARN_FILE"/>
        <appender-ref ref="ERROR_FILE"/>
    </root>

</Configuration>

0 人点赞