springboot整合log4j2
-
导入依赖
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
<!--log4j2--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.2</version> </dependency>
!!!注意,屏蔽内部日志部分必须添加上,因为其自带的是logback的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!--屏蔽内置日志--> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
-
添加配置文件
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="patternLayout">%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Property> <!--生成日志保存位置--> <Property name="logdir">D:/home/demo</Property> </Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="${patternLayout}"/> <!-- <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>--> <!--控制台将会打印执行sql及结果--> <!--<ThresholdFilter level="TRACE"/>--> <ThresholdFilter level="DEBUG"/> </Console> <RollingFile name="InfoLogger" fileName="D:/home/demo/debug/log.log" filePattern="D:/home/demo/debug/log_%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${patternLayout}"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <Policies> <!--根据时间来滚动 --> <TimeBasedTriggeringPolicy modulate="true" interval="1"/> </Policies> <!-- 日志保留策略,配置只保留七天 --> <DefaultRolloverStrategy> <Delete basePath="${logDir}/debug/" maxDepth="1"> <IfFileName glob="log_*.log"/> <IfLastModified age="7d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> <!-- sql:保存sql相关日志 --> <RollingFile name="sql" fileName="${logdir}/sql/sql.log" filePattern="${logdir}/sql/sql_%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${patternLayout}"/> <Policies> <TimeBasedTriggeringPolicy modulate="true" interval="1"/> </Policies> <DefaultRolloverStrategy> <Delete basePath="${logDir}/sql/" maxDepth="1"> <IfFileName glob="sql_*.log"/> <IfLastModified age="7d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> </Appenders> <Loggers> <!-- 开启mybatis的sql日志, name和mybatis-config.xml的logPrefix一致 . 级别为debug或trace--> <Logger name="com.example.demo.mybatis.plus.dao" level="TRACE" additivity="false"> <AppenderRef ref="Console"/> <AppenderRef ref="sql"/> </Logger> <Root level="debug"> <AppenderRef ref="Console"/> <AppenderRef ref="InfoLogger"/> </Root> </Loggers> </Configuration>
配置文件名字及位置如下图:
log4j2.xml
-
application.yml配置文件
# 日志配置 logging: config: classpath:log4j2.xml level: # 查询接口路径 com.example.demo.mybatis.plus.dao: DEBUG
-
常见问题及解决
① 常见问题
问题原因是没有排除spring-boot中自带的日志。11:34:08.938 [Thread-1] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@21253577 Logging system failed to initialize using configuration from 'classpath:log4j2.xml' java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:17 - no applicable action for [Properties], current ElementPath is [[Configuration][Properties]]
② 解决方案
依赖中添加org.springframework.boot
,屏蔽内置日志。