java日志常用配置

<!-- pom.xml -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<!-- logback.xml -->
<configuration>
    <!-- 自定义日志输出路径-->
    <property name="PATH" value="./logs/"/>
    <!--自定义日志存留天数-->
    <property name="LOG_MAX_HISTORY" value="5"/>
    <!--自定义单个日志文件的大小,超过将生成新文件-->
    <property name="LOG_MAX_FILE_SIZE" value="20MB"/>
    <!--自定义日志总大小,超过将删除最旧存档-->
    <property name="LOG_TOTAL_SIZE_CAP" value="1GB"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{50}:%L) - %msg%n</pattern>
        </encoder>
        <!-- 此日志文件只记录INFO以上级别的 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </appender>

    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${PATH}/debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${PATH}/trace.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <!--保存多少天的日志-->
            <maxHistory>${LOG_MAX_HISTORY}</maxHistory>
            <!--自定义日志总大小-->
            <totalSizeCap>${LOG_TOTAL_SIZE_CAP}</totalSizeCap>
            <!--单个文件大小上限-->
            <maxFileSize>${LOG_MAX_FILE_SIZE}</maxFileSize>
            <!--在只有设置为true并且 maxHistory 不为零才能起效。-->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <!--输出格式-->
        <encoder>
            <!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-50logger{50}:%-4line %green(%-8X{UUID}) %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
            <charset>utf8</charset>
        </encoder>
        <!-- 此日志文件只记录DEBUG以上级别的 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>

    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${PATH}/error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${PATH}/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <!--单个文件大小上限-->
            <maxFileSize>${LOG_MAX_FILE_SIZE}</maxFileSize>
            <!--保存多少天的日志-->
            <maxHistory>${LOG_MAX_HISTORY}</maxHistory>
            <!--自定义日志总大小-->
            <totalSizeCap>${LOG_TOTAL_SIZE_CAP}</totalSizeCap>
            <!--在只有设置为true并且 maxHistory 不为零才能起效。-->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <!--输出格式-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
            <charset>utf8</charset>
        </encoder>
        <!-- 此日志文件只记录ERROR级别 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <root level="TRACE">
        <appender-ref ref="ERROR_FILE"/>
        <appender-ref ref="DEBUG_FILE"/>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

posted @ 2024-11-19 10:44  chenzechao  阅读(4)  评论(0编辑  收藏  举报