logback.xml 配置文件

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
    <!--<property name="LOG_HOME" value="${LOG_HOME}" />-->
    <property name="LOG_HOME" value="./logs" />
    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %highlight([%-5level] %logger{50} - %msg%n)</pattern>
            <!--<charset>UTF-8</charset>--><!--注释掉,解决Windows CMD 运行时 log 乱码-->
        </encoder>
    </appender>


    <appender name="SYS_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/debug.log</File>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <fileNamePattern>${LOG_HOME}/debug.log.%d{yyyy-MM-dd}</fileNamePattern>
            <!--日志文件保留天数-->
            <maxHistory>15</maxHistory>
            <!--日志文件最大的大小-->
            <!--<MaxFileSize>1000MB</MaxFileSize>-->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="SYS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/info.log</File>
        <append>true</append>
        <!--过滤器,只打INFO级别的日志,日志文件最多保留15天(如果是按天滚动)-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch> <!-- 表示匹配该级别及以上 -->
            <onMismatch>DENY</onMismatch> <!-- 表示不匹配该级别以下的 -->
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/info.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>15</maxHistory>
            <!--日志文件保留天数-->
            <maxHistory>${maxHistory}</maxHistory>
            <!--日志文件最大的大小-->
            <!--<MaxFileSize>1000MB</MaxFileSize>-->            
            <!--启用删除删除功能,默认为自动删除-->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>

        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="SYS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/error.log</File>
        <append>true</append>
        <!--过滤器,只打ERROR级别的日志-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/error.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>15</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>

        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="SYS_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/warn.log</File>
        <append>true</append>
        <!--过滤器,只打WARN级别的日志-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
            <fileNamePattern>${LOG_HOME}/warn.log.%d{yyyy-MM-dd}</fileNamePattern> 
            <maxHistory>15</maxHistory> 
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>

        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--不同业务产生的日志打印到不同文件中-->
    <appender name="API_HIS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/api_his_info.log</File>
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/api_his_info.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>15</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <appender name="API_HIS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/error_api_his.log</File>
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/error_api_his.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>15</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="API_IRON_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/api_iron_info.log</File>
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/api_iron_info.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>15</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <appender name="API_IRON_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/error_api_iron.log</File>
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/error_api_iron.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>15</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--设置 com.iron.demo 包空间下面的日志 level 显示-->
    <logger name="com.iron.demo" level="INFO"/>


    <!-- 不同的业务逻辑日志打印到指定文件中 && info 和 error 分开  additivity=false 不会加到 SYS_XXX 默认系统日志中,在使用 Spring Boot Admin 时,需要将这个去掉,因为Spring Boot Admin-Client 只能指定一个日志文件, -->
    <logger name="apiHis" level="INFO">
        <appender-ref ref="API_HIS_INFO"/>
    </logger>
    
    <logger name="apiIron" additivity="false" level="INFO">
        <!--打到控制台-->
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="API_IRON_INFO"/>
        <appender-ref ref="API_IRON_ERROR"/>
    </logger>

    <!--系统级的日志(默认) info和error分开打印-->
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="SYS_DEBUG"/>
        <appender-ref ref="SYS_INFO"/>
        <appender-ref ref="SYS_WARN"/>
        <appender-ref ref="SYS_ERROR"/>
    </root>

</configuration>

 

posted @ 2022-09-01 14:38  VipSoft  阅读(732)  评论(0编辑  收藏  举报