In order to achieve automatic log cleaning, logback is used instead of log4j. Not much to say, the following is the implementation method, is it super simple?
add dependencies
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.11</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.11</version> </dependency>Configure logback.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- To make the configuration file reload automatically, you need to set the scan property to true. By default, it will scan once every minute. You can specify the scan interval: scanPeriod The available unit is milliseconds,seconds,minutes 和 hours --> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <!--Define the storage address of log files Do not use relative paths in the configuration of LogBack--> <property name="LOG_HOME" value="./logs" /> <property name="fileLayoutPattern" value="%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%logger{50}][%thread] [%-4relative]) [%X{hostName} %X{requestId} %X{processId} %X{service.timing} ][%level] %msg%n" /> <property name="consoleLayoutPattern" value="%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%logger{50}][%thread] [%-4relative]) [%X{hostName} %X{requestId} %X{processId} %X{service.timing} ][%level] %msg%n" /> <!-- console output --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout name="StandardFormat" class="ch.qos.logback.classic.PatternLayout"> <pattern>${consoleLayoutPattern}</pattern> </layout> </appender> <!-- Generate log files on a daily basis--> <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/info.log</file> <!-- Log filter, only record warning level logs, if you want to configure other levels, copy the appender, modify the level, and configure it in root --> <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.%d{yyyy-MM-dd}.log </fileNamePattern> <!--Number of days to keep log files--> <MaxHistory>30</MaxHistory> </rollingPolicy> <layout> <pattern>${fileLayoutPattern}</pattern> </layout> </appender> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/error.log</file> <!-- Log filter, only record warning level logs, if you want to configure other levels, copy the appender, modify the level, and configure it in root --> <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.%d{yyyy-MM-dd}.log </fileNamePattern> <!--Number of days to keep log files--> <MaxHistory>30</MaxHistory> </rollingPolicy> <layout> <pattern>${fileLayoutPattern}</pattern> </layout> </appender> <!-- log output level--> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="INFO" /> <appender-ref ref="ERROR" /> </root> </configuration>
java code
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main { public static Logger log = LoggerFactory.getLogger(Main.class); public static void main(String[] args) { log.info("Oh"); log.error("额。。。。"); } }