美文网首页
springboot logback最优配置

springboot logback最优配置

作者: BeeHoney | 来源:发表于2018-12-29 17:00 被阅读0次

说明

日志是一个系统很重要的一部分,在生产时间中,逐渐形成了一套自己的日志规范,方便大家集成。

配置文件配置 application.yml

logging:
  config:
    classpath: logback-spring.xml
    path: /var/log/${spring.application.name}
  myFileName: ${spring.application.name}
  # 分割文件设置 超过 100MB就进行分割,最大保留历史 90天
  maxFileSize: 100MB
  maxHistory: 90
  level:
    root: INFO
    org.springframework.web: INFO
    org.springframework.security: INFO
    # 自己工程命名的类
    com.project: DEBUG

logback-spring.xml 配置文件

经过实践 ,对logback进行了优化,此文件需要放入 src/main/resources下,
会生成两个文件
开发调试({MY_LOG_PATH}/{myFileName}-base.log): 包含debug级别
上生产({MY_LOG_PATH}/{myFileName}-info.log): 排除debug级别,也就是info级别,可以定义友好中文释义输出。

具体优化如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
<!--<configuration scan="true" scanPeriod="10 seconds">-->
<configuration>

    <springProperty scop="context" name="url" source="spring.datasource.url" defaultValue=""/>
    <springProperty scop="context" name="username" source="spring.datasource.username" defaultValue=""/>
    <springProperty scop="context" name="password" source="spring.datasource.password" defaultValue=""/>

    <springProperty scop="context" name="logLevel" source="logging.level.root" defaultValue="INFO"/>
    <springProperty scop="context" name="myFileName" source="logging.myFileName" defaultValue=""/>
    <springProperty scop="context" name="MY_LOG_PATH" source="logging.config.path" defaultValue="/var/log/${myFileName}"/>
    <springProperty scop="context" name="maxFileSize" source="logging.maxFileSize" defaultValue="100MB"/>
    <springProperty scop="context" name="maxHistory" source="logging.maxHistory" defaultValue="90"/>

    <!--<include resource="org/springframework/boot/logging/logback/base.xml" />-->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
           
        <encoder>   
            <!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} L%line - %msg%n</pattern>
            <charset>UTF-8</charset>  
        </encoder>
         
    </appender>

    <appender name="BASE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--<filter class="ch.qos.logback.classic.filter.LevelFilter">-->
            <!--&lt;!&ndash;过滤 Error&ndash;&gt;-->
            <!--<level>ERROR</level>-->
            <!--&lt;!&ndash;匹配到就禁止&ndash;&gt;-->
            <!--<onMatch>DENY</onMatch>-->
            <!--&lt;!&ndash;没有匹配到就允许&ndash;&gt;-->
            <!--<onMismatch>ACCEPT</onMismatch>-->
        <!--</filter>-->
        <File>${MY_LOG_PATH}/${myFileName}-base.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${MY_LOG_PATH}/${myFileName}-base-%d{yyyyMMdd}.log.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${maxFileSize}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} L%line - %msg%n</Pattern>
        </layout>
    </appender>

    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <File>${MY_LOG_PATH}/${myFileName}-info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${MY_LOG_PATH}/${myFileName}-info-%d{yyyyMMdd}.log.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${maxFileSize}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} L%line - %msg%n</Pattern>
        </layout>
    </appender>

    <!--输出到mysql数据库的appender配置     -->
    <!--<appender name="MYSQL" class="ch.qos.logback.classic.db.DBAppender">-->
    <!--<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">-->
    <!--<driverClass>com.mysql.jdbc.Driver</driverClass>-->
    <!--<url>${url}</url>-->
    <!--<user>${username}</user>-->
    <!--<password>${password}</password>-->
    <!--</connectionSource>-->
    <!--</appender>-->


    <springProfile name="pro">
        <root level="${logLevel}">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="BASE_FILE"/>
            <appender-ref ref="INFO_FILE"/>
            <!--<appender-ref ref="MYSQL"/>-->
        </root>
    </springProfile>
    <springProfile name="local">
        <root level="${logLevel}">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="BASE_FILE"/>
            <appender-ref ref="INFO_FILE"/>
            <!--<appender-ref ref="MYSQL"/>-->
        </root>
    </springProfile>

</configuration>

更多,请关注:
springboot 技术实践总结

相关文章

网友评论

      本文标题:springboot logback最优配置

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