This article introduces the example code of SpringBoot integrating slf4j+log4j2. It is shared with you. The details are as follows:
Maven dependencies
<!--Increase log4j2 dependency↓--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <exclusions> <!-- Remove old log dependencies--> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- Remove old log dependencies--> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusions></dependency>
If other dependencies have log log conflicts, you can add the following configuration:
<exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>*</artifactId> </exclusion></exclusions>
log4j2.xml
Put it in the resources directory
<?xml version="1.0" encoding="UTF-8"?><!-- The 6 priority levels are: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL. If the priority is set to WARN, then the four levels of logs, OFF, FATAL, ERROR, and WARN, can output normally to OFF. This means that logs of log4j2 are not recorded. --><!-- status: used to specify the printing log level of log4j itself, monitorInterval: specify the monitoring interval time for log4j automatically reconfigured --><configuration status="INFO" monitorInterval="30"> <!-- Set the properties yourself, and then access it through ${} --><!-- <properties> <property name="LOG_HOME">${web:rootDir}/logs</property> </properties>--> <appenders> <!--Appender 1. Output to the Console console, specify the output format and filter level as INFO --> <Console name="Console" target="SYSTEM_OUT"> <!--ThresholdFilter Specifies the lowest level of output of log messages --> <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </Console> <!--Appender 2. Output to the scroll saved file, The condition that triggers the save log file is that the log file is greater than 3KB, and only the latest 10 logs are saved--> <File name="allLog" fileName="${LOG_HOME}/all.log"> <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="%d{yyyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <!--Appender 3. Export to the scroll saved file, The condition that triggers the save log file is that the log file is greater than 3KB, and only the latest 10 logs are saved --> <RollingFile name="debugLog" fileName="${LOG_HOME}/debug.log" filePattern="${log.path}/debug-%i.log"> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="[%-5level][%d{yyyy-MM-dd HH:mm:ss}][%F:%L] - %m%n"/> <SizeBasedTriggeringPolicy size="3KB"/> <!-- The parameter max in DefaultRolloverStrategy can limit the max archives after the size exceeds the size in SizeBasedTriggeringPolicy--> <DefaultRolloverStrategy max="10"/> </RollingFile> <!--Appender 4. Export to the scroll saved file, the condition that triggers the save log file is the first log event per minute. ERROR logs are generated by minutes --> <RollingFile name="errorLog" fileName="${LOG_HOME}/error.log" filePattern="${log.path}/error-%d{yyyy-MM-dd_HH-mm}.log"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="[%-5level][%d{yyyyy-MM-dd HH:mm:ss}][%C:%F:%L] - %m%n"/> <TimeBasedTriggeringPolicy/> </RollingFile> <RollingFile name="RollingFile" fileName="${LOG_HOME}/rar.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> <!--The maximum log file value is compressed the next day --> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="10 MB"/> </Policies> </RollingFile> </appenders> <!--root default loading--> <loggers> <root level="DEBUG"> <appender-ref ref="Console"/> <!--<appender-ref ref="allLog"/>--> <!--<appender-ref ref="debugLog"/>--> <!--<appender-ref ref="errorLog"/>--> <!--<appender-ref ref="RollingFile"/>--> </root> </loggers></configuration> LogTest.java
import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class LogTest { public final Logger logger=LoggerFactory.getLogger(getClass()); public static void main(String[] args) { log.trace("trace"); log.debug("debug"); log.warn("warn"); log.info("info"); log.error("error"); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.