Maven dependency
<!--增加log4j2依赖↓--> <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> <!-- 去除旧log依赖 --> <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> <!-- 去除旧log依赖 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
If other dependencies have 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.apache.logging.log4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions>
log4j2.xml
Place it in resources Directory
<?xml version="1.0" encoding="UTF-8"?> <!-- 6个优先级从高到低依次为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL。 如果设置优先级为WARN,那么OFF、FATAL、ERROR、WARN 4个级别的log能正常输出 设置为OFF 表示不记录log4j2本身的日志, --> <!-- status:用来指定log4j本身的打印日志级别,monitorInterval:指定log4j自动重新配置的监测间隔时间 --> <configuration status="INFO" monitorInterval="30"> <!-- 自己设置属性,后面通过${}来访问 --> <!-- <properties> <property name="LOG_HOME">${web:rootDir}/logs</property> </properties>--> <appenders> <!--Appender 1. 输出到Console控制台,指定输出格式和过滤器等级为INFO --> <Console name="Console" target="SYSTEM_OUT"> <!--ThresholdFilter指定日志消息的输出最低层次--> <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. 输出到滚动保存的文件, 触发保存日志文件的条件是日志文件大于3KB,只保存最新的10个日志--> <File name="allLog" fileName="${LOG_HOME}/all.log"> <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <!--Appender 3. 输出到滚动保存的文件, 触发保存日志文件的条件是日志文件大于3KB,只保存最新的10个日志--> <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"/> <!-- DefaultRolloverStrategy 中的参数max,可以限制 SizeBasedTriggeringPolicy中size超出后,只保留max个存档--> <DefaultRolloverStrategy max="10"/> </RollingFile> <!--Appender 4. 输出到滚动保存的文件, 触发保存日志文件的条件是每分钟第一次的日志事件。ERROR日志是按分钟产生日志 --> <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{yyyy-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"/> <!--日志文件最大值 第二天压缩--> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="10 MB"/> </Policies> </RollingFile> </appenders> <!--root 默认加载--> <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 the detailed content of How SpringBoot integrates slf4j and log4j2. For more information, please follow other related articles on the PHP Chinese website!