美文网首页
从零搭建项目开发框架-26Logback的配置

从零搭建项目开发框架-26Logback的配置

作者: 孔垂云 | 来源:发表于2017-07-08 22:29 被阅读0次

    logback是日志的基础配置,运行时默认放在WEB-INF/classes/logback.xml

    先看一下本项目的日志配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="30 seconds">
        <property name="logBase" value="d:\\logs\\bdp\\"/>
        <!-- appender -->
        <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} - %m%n
                </pattern>
                <charset>GBK</charset>
            </encoder>
        </appender>
    
        <!-- 系统日志配置 -->
        <appender name="sysLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/sys.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/sys-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>[%d{yyyyMMdd HH:mm:ss}] - %m%n</Pattern>
            </encoder>
        </appender>
        <appender name="daoLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/dao.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/dao-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>[%d{yyyyMMdd HH:mm:ss}] [%-5level] [%-30logger{0}][%-3L]
                    [SeqId:%X{SeqId}] - %m%n
                </Pattern>
            </encoder>
        </appender>
        <appender name="serviceLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/service.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/service-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>[%d{yyyyMMdd HH:mm:ss}] - %m%n</Pattern>
            </encoder>
        </appender>
        <appender name="controllerLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/controller.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/controller-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>[%d{yyyyMMdd HH:mm:ss}]- %m%n</Pattern>
            </encoder>
        </appender>
    
        <appender name="operationLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/operation.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/operation-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>%m%n</Pattern>
            </encoder>
        </appender>
        <appender name="jobLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/job.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/job-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>[%d{yyyyMMdd HH:mm:ss}]- %m%n</Pattern>
            </encoder>
        </appender>
        <appender name="performanceLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logBase}/performance.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logBase}/%d{yyyyMMdd}/performance-%d{yyyyMMdd}.log
                </FileNamePattern>
                <maxHistory>365</maxHistory>
            </rollingPolicy>
            <encoder>
                <ImmediateFlush>true</ImmediateFlush>
                <charset>utf-8</charset>
                <Pattern>[%d{yyyyMMdd HH:mm:ss}] - %m%n</Pattern>
            </encoder>
        </appender>
        <!-- additivity为false不向root控制台输出 -->
        <logger name="sysLog" additivity="false" level="info">
            <appender-ref ref="stdout"/>
            <appender-ref ref="sysLogAppender"/>
        </logger>
        <logger name="daoLog" additivity="false" level="ERROR">
            <appender-ref ref="stdout"/>
            <appender-ref ref="daoLogAppender"/>
        </logger>
        <logger name="serviceLog" additivity="false" level="ERROR">
            <appender-ref ref="stdout"/>
            <appender-ref ref="serviceLogAppender"/>
        </logger>
        <logger name="controllerLog" additivity="false" level="ERROR">
            <appender-ref ref="stdout"/>
            <appender-ref ref="controllerLogAppender"/>
        </logger>
        <logger name="operationLog" additivity="false" level="INFO">
            <appender-ref ref="stdout"/>
            <appender-ref ref="operationLogAppender"/>
        </logger>
        <logger name="performanceLog" additivity="false" level="INFO">
            <appender-ref ref="stdout"/>
            <appender-ref ref="performanceLogAppender"/>
        </logger>
        <logger name="jobLog" additivity="false" level="INFO">
            <appender-ref ref="stdout"/>
            <appender-ref ref="jobLogAppender"/>
        </logger>
    
        <!-- root 默认日志配置 -->
        <root level="info">
            <appender-ref ref="stdout"/>
        </root>
        <logger name="org.springframework" level="INFO"/>
    </configuration>
    

    讲一下主要配置属性

    • <property name="logBase" value="d:\\logs\\bdp\\"/>这地方定义日志的存储根目录,实际生产中只需要改该目录就可以了。
    • <appender name="sysLogAppender"这是定义日志的种类,可以有很多
    • <file>${logBase}/sys.log</file>定义日志文件的名称
    • <rollingPolicy定义日志的滚动策略,这里定义的是当前日志后面加上-yyyyMMdd进行存储,并放在${logBase}/%d{yyyyMMdd}下面
    • <encoder>定义日志的编码格式及输出规范
    • <logger name="sysLog" additivity="false" level="info">这里定义日志名称及输出级别,一般一个日志名称对应一个日志输出
    • <logger name="org.springframework" level="INFO"/>这里定义spring的参数输出,是info级别的。

    日志的配置很重要,尤其是在分布式部署的情况下, 日志的输出更为重要。

    源码下载

    本阶段详细源码

    相关文章

      网友评论

          本文标题:从零搭建项目开发框架-26Logback的配置

          本文链接:https://www.haomeiwen.com/subject/ntgocxtx.html