<!--Web依赖-->
<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>
<!-- log4j2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
1 依赖如上
2 再配置log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="Info">
<!-- 变量配置 -->
<Properties>
<Property name="proj_name">demo</Property>
<!--由于我是docker部署的项目,挂载日志目录时采用的绝对路径,所以此处也是用的绝对路径。-->
<Property name="log_path">/log4</Property>
</Properties>
<!-- appender配置 -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level}[%thread] %style{%logger{36}}{cyan} : %msg%n" disableAnsi="false" noConsoleNoAnsi="false"/>
</Console>
<!--immediateFlush="false" 普通日志不设置实时写入日志,减少性能开销-->
<RollingFile name="DailyRollingFileInfo" immediateFlush="false" append="true" fileName="${log_path}/sysLog/${proj_name}-info.log"
filePattern="${log_path}/sysLog/${proj_name}-%d{yyyy-MM-dd}-info.log">
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] [%thread] %C.%M:%L|%m%n" />
<Policies>
<!--根据filePattern 中的 {yyyy-MM-dd} 设置 一天滚动一次(一天生成一个文件)-->
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
</Policies>
</RollingFile>
<!--immediateFlush="true" 报错日志设置实时写入日志-->
<RollingFile name="DailyRollingFileError" immediateFlush="true" append="true" fileName="${log_path}/errorLog/${proj_name}-error.log"
filePattern="${log_path}/errorLog/${proj_name}-%d{yyyy-MM-dd}-error.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] [%thread] %C.%M:%L|%m%n" />
<Policies>
<!--根据filePattern 中的 {yyyy-MM-dd} 设置 一天滚动一次(一天生成一个文件)-->
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.cmnit.micro" level="Info" additivity="false" includeLocation="true">
<AppenderRef ref="Console" />
<AppenderRef ref="DailyRollingFileInfo" />
<AppenderRef ref="DailyRollingFileError" />
</Logger>
<Logger name="org.springframework" level="Info" additivity="false" includeLocation="true">
<AppenderRef ref="Console" />
<AppenderRef ref="DailyRollingFileInfo" />
<AppenderRef ref="DailyRollingFileError" />
</Logger>
<Logger name="org.hibernate" level="Info" additivity="false" includeLocation="true">
<AppenderRef ref="Console" />
<AppenderRef ref="DailyRollingFileInfo" />
<AppenderRef ref="DailyRollingFileError" />
</Logger>
<Logger name="redis.clients" level="Info" additivity="false" includeLocation="true">
<AppenderRef ref="Console" />
<AppenderRef ref="DailyRollingFileInfo" />
<AppenderRef ref="DailyRollingFileError" />
</Logger>
<Root level="Info" includeLocation="true">
<AppenderRef ref="Console" />
<AppenderRef ref="DailyRollingFileInfo" />
<AppenderRef ref="DailyRollingFileError" />
</Root>
</Loggers>
</Configuration>
3 启动项目报错:
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
解决:翻译过来就是 log4j-slf4j-impl 和 log4j-to-slf4j 不能共存
(但是 log4j-slf4j-impl 是 slf4j 跟 log4j2 的桥接层 必须保留。)
利用maven helper 找到依赖 去掉 log4j-to-slf4j
去掉后maven变动 (spring - jbdc导致)
4 然后再启动项目,还是报错
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/D:/maven-responsity/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory
解决:去掉依赖 logback (logback 跟 log4j2 冲突 ,二选一,保留log4j2)
去掉后maven变动 (fastDfs依赖导致的)
5 再次启动成功
6 测试结果
直接使用lombok 日志注解 @Slf4j
@Slf4j
@RestController
@RequestMapping("/log4j")
@Api(tags = "Demo:log4j测试")
public class Log4jController {
@GetMapping("/log1")
@ApiOperation(value = "debug测试")
public void log1(){
log.info("这是info");
log.warn("这是warn");
log.error("这是error");
}
}
控制台输出:
生成文件: