springboot 配置logback 根据日志级别进行分级保存

1、在resources包下新建logback-spring.xml 文件

logback-spring.xml

2、logback-spring.xml


<configuration>
    
    <appender name="Console_Log" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                
                
                %d{yyyy-MM-dd HH:mm:ss.SSS} -- %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n
            pattern>
        layout>
    appender>
    
    
    <appender name="Info_File" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>logs/Info_File.logfile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} -- [%thread] %-5level %logger{36}.%M - %msg%n
            pattern>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            
            <fileNamePattern>logs/Info_File-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <maxFileSize>100MBmaxFileSize>
            <maxHistory>60maxHistory>
            <totalSizeCap>20GBtotalSizeCap>
        rollingPolicy>
    appender>
    <appender name="Error_File" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/Error_File.logfile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} -- [%thread] %-5level %logger{36}.%M - %msg%n
            pattern>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <fileNamePattern>logs/Error_File-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <maxFileSize>100MBmaxFileSize>
            <maxHistory>60maxHistory>
            <totalSizeCap>20GBtotalSizeCap>
        rollingPolicy>
    appender>
    
    <root level="info">
        <appender-ref ref="Console_Log">appender-ref>
        <appender-ref ref="Info_File">appender-ref>
        <appender-ref ref="Error_File">appender-ref>
    root>
configuration>

你可能感兴趣的:(Java学习笔记)