Springboot整合log4j2【详细步骤】
1.去除logback中的依赖包
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter</artifactId> 4 <exclusions> 5 <exclusion> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-logging</artifactId> 8 </exclusion> 9 </exclusions> 10 </dependency>
2.添加log4j2的依赖
1 <dependency> 2 <groupId>org.apache.logging.log4j</groupId> 3 <artifactId>log4j-api</artifactId> 4 <version>2.10.0</version> 5 </dependency> 6 <dependency> 7 <groupId>org.apache.logging.log4j</groupId> 8 <artifactId>log4j-core</artifactId> 9 <version>2.10.0</version> 10 </dependency>
3.默认的properties配置对log4j2不够友好,我们应用外部配置文件,在资源文件夹src/main/resources下添加log4j2.xml或者log4j2-spring.xml,启动后spring boot自动加载,配置文件的内容如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <properties> <!-- 文件输出格式 --> <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property> </properties> <appenders> <Console name="CONSOLE" target="system_out"> <PatternLayout pattern="${PATTERN}" /> </Console> </appenders> <loggers> <logger name="com.roncoo.education" level="debug" /> <root level="info"> <appenderref ref="CONSOLE" /> </root> </loggers> </configuration>
配置完成重启工程,日志输出格式将使用该配置的格式输出