logback日志归档策略配置

logback日志归档策略配置

背景

线上日志文件大,动不动十几个G,查一次信息比较慢,怎么办?

举措

  • 剔除无效日志

  • 按照时间进行切分
    默认按天,可以考虑按照小时

  • 按日期和大小切分

rollingPolicy

常用的策略

  • TimeBasedRollingPolicy

  • SizeAndTimeBasedRollingPolicy

参数说明

maxHistory
归档文件的最大数量

The optional maxHistory property controls the maximum number of archive files to keep, asynchronously deleting older files.

totalSizeCap
所有文档文件的总大小

The optional totalSizeCap property controls the total size of all archive files. Oldest archives are deleted asynchronously when the total size cap is exceeded.

maxFileSize
每个文件的大小上限

Each time the current log file reaches maxFileSize before the current time period ends, it will be archived with an increasing index, starting at 0.

appender

  • 配置信息
    <property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}"/>
    <property name="FILE_LOG_PATTERN"
              value="%d{yyyy/MM/dd-HH:mm:ss:SSS} [%tid] [%sid][%pid] %X{sessionTokenId} [%thread] %-5level %logger- %jsonMsg%n"/>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH}/app.logfile>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/app-%d{yyyy-MM-dd}-%i.logfileNamePattern>
        <maxFileSize>2GBmaxFileSize>
        <maxHistory>30maxHistory>
    rollingPolicy>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}pattern>
    encoder>
appender>

reference

  • https://logback.qos.ch/manual/appenders.html

你可能感兴趣的:(Java,Linux,logback)