logback配置按天产生日志文件

Posted Questions张

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了logback配置按天产生日志文件相关的知识,希望对你有一定的参考价值。

1 依赖Jar包 pom配置 也可以根据自己的版本来

<dependency>      
    <groupId>org.slf4j</groupId>    
    <artifactId>slf4j-api</artifactId>     
    <version>1.7.12</version> 
 </dependency> 
 <dependency>     
    <groupId>org.slf4j</groupId>  
    <artifactId>jcl-over-slf4j</artifactId>  
    <version>1.7.12</version>  
</dependency> 
 <dependency>      
    <groupId>ch.qos.logback</groupId>      
    <artifactId>logback-core</artifactId>      
    <version>1.1.3</version>  
</dependency>  
<dependency>      
    <groupId>ch.qos.logback</groupId>      
    <artifactId>logback-classic</artifactId>      
    <version>1.1.3</version>  
</dependency>

 

2 logback.properties

logback.dir=${catalina.base} 
logback.all.filename=/logs/loanms-all
logback.info.filename=/logs/loanms-info
logback.debug.filename=/logs/loanms-debug
logback.warn.filename=/logs/loanms-warn
logback.error.filename=/logs/loanms-error
logback.maxhistory=60
logback.level=info
logback.charset=UTF-8

 


3 logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <property resource="logback.properties"/>
    <appender name="file-all" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Prudent>true</Prudent>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${logback.dir}/${logback.all.filename}.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!-- keep ${logback.maxhistory} days worth of history -->
            <MaxHistory>${logback.maxhistory}</MaxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} -%msg%n
            </Pattern>
        </layout>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="file-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <Prudent>true</Prudent>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${logback.dir}/${logback.info.filename}.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!-- keep ${logback.maxhistory} days worth of history -->
            <MaxHistory>${logback.maxhistory}</MaxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} -%msg%n
            </Pattern>
        </layout>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{0}] %m%n</pattern>-->
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="file-warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <Prudent>true</Prudent>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${logback.dir}/${logback.warn.filename}.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!-- keep ${logback.maxhistory} days worth of history -->
            <MaxHistory>${logback.maxhistory}</MaxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} -%msg%n
            </Pattern>
        </layout>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{0}] %m%n</pattern>
        </encoder>
    </appender>
    <appender name="file-error"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Prudent>true</Prudent>
        <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>
                ${logback.dir}/${logback.error.filename}.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!-- keep ${logback.maxhistory} days worth of history -->
            <MaxHistory>${logback.maxhistory}</MaxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} -%msg%n
            </Pattern>
        </layout>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{0}] %m%n</pattern>
        </encoder>
    </appender>
    <appender name="file-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <Prudent>true</Prudent>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${logback.dir}/${logback.debug.filename}.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!-- keep ${logback.maxhistory} days worth of history -->
            <MaxHistory>${logback.maxhistory}</MaxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} -%msg%n
            </Pattern>
        </layout>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{0}] %m%n</pattern>-->
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{0}] %m%n</pattern>
        </encoder>
    </appender>
    <root>
        <level value="all" />
        <appender-ref ref="file-all" />
        <appender-ref ref="file-info" />
        <appender-ref ref="file-debug" />
        <appender-ref ref="file-warn" />
        <appender-ref ref="file-error" />
        <appender-ref ref="console" />
    </root>
</configuration>

 

以上是关于logback配置按天产生日志文件的主要内容,如果未能解决你的问题,请参考以下文章

logback 设置按天,文件切割大小,总共日志文件大小。

logback怎么根据logger输出不同文件

Spring Boot中的日志配置

logback在集群环境下面如何输出日志,有啥好的方案

springboot2.0整合logback日志(详细)

logback :项目中运行时 控制台没有logback日志产生 ?