logback配置按天产生日志文件

时间:2022-09-15 00:50:28

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>