日志打印及配置

1.参考博客:

2.logback.xml内容如下:

<configuration scan="true" scanPeriod="60 seconds">
    <property name="log.path" value="logs/xxx" />
    <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />

    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>

    <appender name="I" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Append>true</Append>
        <File>${log.path}/info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--按天轮转 -->
            <fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!--保存 30 天的历史记录,最大大小为 30GB -->
            <maxHistory>30</maxHistory>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>

    <appender name="E" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${log.path}/error.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--按天轮转 -->
            <fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!--保存 30 天的历史记录,最大大小为 30GB -->
            <maxHistory>30</maxHistory>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
    </appender>

    <root level="info">
        <appender-ref ref="stdout"/>
        <!--<appender-ref ref="D"/>-->
        <appender-ref ref="I"/>
        <appender-ref ref="E"/>
    </root>
</configuration>

3.代码中使用方式:

private static final  Logger logger = LoggerFactory.getLogger(xxx.class);

logger.info("xxxxx");

如果想更好的设计日志打印,请参考美团技术团队博客: https://tech.meituan.com/2021/09/16/operational-logbook.html

posted @ 2021-10-29 13:46  LHX2018  阅读(131)  评论(0编辑  收藏  举报