1、application.yml
logging:
config: classpath:log4j2.xml
2、log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
<Properties>
<!--System variable ${sys:logPath:-/var/logs} Retrieve the value of the system variable logPath, or set the default value if it is empty./var/logs-->
<Property name="LOG_HOME">${sys:catalina.home:-.}/logs</Property>
<Property name="LOG_BACK_HOME">${sys:catalina.home:-.}/logs/backup</Property>
<Property name="LOG_PATTERN_LAYOUT">%date %highlight{[%-5.5level]}{STYLE=Logback} [%-10.10thread] [%X{employeeCode}] [%X{X-B3-TraceId}/%X{X-B3-SpanId}] %cyan{[%-50.50class:%-4.4line]} - %msg%xEx%n </Property>
<Property name="DEFAULT_CHARSET">UTF-8</Property>
<Property name="ERROR_FILE_NAME">error</Property>
<Property name="INFO_FILE_NAME">info</Property>
</Properties>
<Appenders>
<!-- Configure daily logs -->
<RollingFile name="${INFO_FILE_NAME}" fileName="${LOG_HOME}/${INFO_FILE_NAME}.log" filePattern="${LOG_BACK_HOME}/$${date:yyyy-MM}/${INFO_FILE_NAME}-%d{yyyy-MM-dd}.log.gz" append="true">
<PatternLayout charset="${DEFAULT_CHARSET}" pattern="${LOG_PATTERN_LAYOUT}"/>
<!-- Setting Policy -->
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy >
<!--Delete log from 30 days ago-->
<Delete basePath="${LOG_BACK_HOME}" maxDepth="2">
<IfFileName glob="*/*.log.gz" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<!-- Configuration error log -->
<RollingFile name="${ERROR_FILE_NAME}" fileName="${LOG_HOME}/${ERROR_FILE_NAME}.log" filePattern="${LOG_BACK_HOME}/$${date:yyyy-MM}/${ERROR_FILE_NAME}-%d{yyyy-MM-dd}.log.gz" append="true">
<PatternLayout charset="${DEFAULT_CHARSET}" pattern="${LOG_PATTERN_LAYOUT}"/>
<!-- Setting Policy -->
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<Filters>
<!--Logs with levels greater than or equal to WARN can be written-->
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="INFO" additivity = "false">
<AppenderRef ref="${INFO_FILE_NAME}"/>
<AppenderRef ref="${ERROR_FILE_NAME}"/>
</Logger>
<Logger name="com.picc" level="DEBUG" additivity = "false">
<AppenderRef ref="${INFO_FILE_NAME}"/>
<AppenderRef ref="${ERROR_FILE_NAME}"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="${INFO_FILE_NAME}"/>
<AppenderRef ref="${ERROR_FILE_NAME}"/>
</Root>
</Loggers>
</Configuration>
3. Use result display
cd backup/
You’ll find monthly backups
cd 2020-06/
Log is compressed, self decompression view
Read More:
- ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration…
- How to Fix log4j2 warning: warn unable to identify org.fusesource.jansi.WindowsAnsiOutputStream
- [Solved] Log4j2 log startup error: javax.xml.parsers.ParserConfigurationException…
- log4j:WARN Error during default initialization [How to Solve]
- [Solved] could not open error log file: CreateFile() “logs/error.log“ fail
- How to Solve Log4j 2.5 upgrade to 2.15 error
- [Solved] IDEA Import springboot Project Error: Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:<unknown>
- [Solved] ‘build.plugins.plugin.version‘ for org.springframework.boot:spring-boot-maven-plugin is missing.
- [Solved] spring boot Startup Error: Error creating bean with name ‘requestMappingHandlerMapping‘ defined in class path
- Frequent log swiping after Nacos client starts [How to Solve]
- [Solved] Spring Boot Project Error: Failed to load property source from
- [Solved] Log Error: NAN OR INFINITY
- Spring boot real time HTML page
- [Solved] spring boot integrated PageHelper Error
- Spring-boot-maven-plugin Error [How to Solve]
- How to Solve Spring Boot Maven Build Error
- Eclipse relies on spring boot configuration processor, and there is no prompt for writing properties and YML
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- [Solved] Spring Boot Error: org.springframework.jdbc.datasource.embedded.EmbeddedData
- Spring Boot Error starting ApplicationContext. To display the auto-configuration report re-run you