如题,我在log4j2.xmlli设置的PatternLayout不生效,我猜测应该使用的是默认的配置,但是我反复检查都没有找出问题,请大神帮忙,谢谢了!
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>BaseProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>BaseProject</name>
<description>BaseProject</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!--指定GeneratorConfig.xml路径,这里放入项目根路径,和pom.xml同级-->
<configurationFile>GeneratorConfig.xml</configurationFile>
<verbose>true</verbose>
<!--是否覆盖原文件,Mapper.xml不会覆盖-->
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug" monitorInterval="30">
<!-- 先定义所有的appender -->
<appenders>
<!-- 这个输出控制台的配置 -->
<Console name="Console" target="SYSTEM_OUT">
<!-- 控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 这个都知道是输出日志的格式 -->
<PatternLayout pattern="aaaaaa[%d{HH:mm:ss.SSS}] [%-5p] %l - %m%n"/>
</Console>
<!-- 文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用 -->
<!-- append为TRUE表示消息增加到指定文件中,false表示消息覆盖指定的文件内容,默认值是true -->
<File name="log" fileName="log/test.log" append="false">
<PatternLayout pattern="[%d{HH:mm:ss.SSS}] [%-5p] %l - %m%n"/>
</File>
<!-- 添加过滤器ThresholdFilter,可以有选择的输出某个级别以上的类别 onMatch="ACCEPT" onMismatch="DENY"意思是匹配就接受,否则直接拒绝 -->
<File name="ERROR" fileName="logs/error.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{yyyy.MM.dd 'at' HH:mm:ss z}] [%-5p] %l - %m%n"/>
</File>
<!-- 这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
<RollingFile name="RollingFile" fileName="logs/web.log"
filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="[%d{yyyy-MM-dd 'at' HH:mm:ss z}] [%-5p] %l - %m%n"/>
<SizeBasedTriggeringPolicy size="2MB"/>
</RollingFile>
</appenders>
<!-- 然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
<loggers>
<!-- 过滤掉spring和mybatis的一些无用的DEBUG信息-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<!-- 建立一个默认的root的logger -->
<root level="trace">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
<appender-ref ref="ERROR" />
<appender-ref ref="log"/>
</root>
</loggers>
</configuration>
application.properties
#jdbc
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db1
spring.datasource.username=root
spring.datasource.password=123456
#mybatis.type-aliases-package=com.example.parentdemo.entity
mybatis.mapper-locations=classpath:/mapper/*.xml
测试方法
package com.example.baseproject.controller;
import com.example.baseproject.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("logtest")
public void getUser() {
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.debug("debug log");
logger.trace("trace log");
logger.info("info log");
logger.warn("warn log");
logger.error("error log");
}
}
console输出
"C:\Program Files\Java\jdk1.8.0_351\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\lib\idea_rt.jar=53920:C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_351\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\rt.jar;D:\MyData\JavaData\WorkSpace\idea-projects\slf4j-log4j2-demo\target\classes;D:\localRepository\org\springframework\boot\spring-boot-starter-web\2.7.10\spring-boot-starter-web-2.7.10.jar;D:\localRepository\org\springframework\boot\spring-boot-starter\2.7.10\spring-boot-starter-2.7.10.jar;D:\localRepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\localRepository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-json\2.7.10\spring-boot-starter-json-2.7.10.jar;D:\localRepository\com\fasterxml\jackson\core\jackson-databind\2.13.5\jackson-databind-2.13.5.jar;D:\localRepository\com\fasterxml\jackson\core\jackson-annotations\2.13.5\jackson-annotations-2.13.5.jar;D:\localRepository\com\fasterxml\jackson\core\jackson-core\2.13.5\jackson-core-2.13.5.jar;D:\localRepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.5\jackson-datatype-jdk8-2.13.5.jar;D:\localRepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.5\jackson-datatype-jsr310-2.13.5.jar;D:\localRepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.5\jackson-module-parameter-names-2.13.5.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-tomcat\2.7.10\spring-boot-starter-tomcat-2.7.10.jar;D:\localRepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.73\tomcat-embed-core-9.0.73.jar;D:\localRepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.73\tomcat-embed-el-9.0.73.jar;D:\localRepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.73\tomcat-embed-websocket-9.0.73.jar;D:\localRepository\org\springframework\spring-web\5.3.26\spring-web-5.3.26.jar;D:\localRepository\org\springframework\spring-beans\5.3.26\spring-beans-5.3.26.jar;D:\localRepository\org\springframework\spring-webmvc\5.3.26\spring-webmvc-5.3.26.jar;D:\localRepository\org\springframework\spring-aop\5.3.26\spring-aop-5.3.26.jar;D:\localRepository\org\springframework\spring-context\5.3.26\spring-context-5.3.26.jar;D:\localRepository\org\springframework\spring-expression\5.3.26\spring-expression-5.3.26.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-log4j2\2.7.10\spring-boot-starter-log4j2-2.7.10.jar;D:\localRepository\org\apache\logging\log4j\log4j-slf4j-impl\2.17.2\log4j-slf4j-impl-2.17.2.jar;D:\localRepository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\localRepository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\localRepository\org\apache\logging\log4j\log4j-core\2.17.2\log4j-core-2.17.2.jar;D:\localRepository\org\apache\logging\log4j\log4j-jul\2.17.2\log4j-jul-2.17.2.jar;D:\localRepository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\localRepository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.3.0\mybatis-spring-boot-starter-2.3.0.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-jdbc\2.7.10\spring-boot-starter-jdbc-2.7.10.jar;D:\localRepository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;D:\localRepository\org\springframework\spring-jdbc\5.3.26\spring-jdbc-5.3.26.jar;D:\localRepository\org\springframework\spring-tx\5.3.26\spring-tx-5.3.26.jar;D:\localRepository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.3.0\mybatis-spring-boot-autoconfigure-2.3.0.jar;D:\localRepository\org\mybatis\mybatis\3.5.11\mybatis-3.5.11.jar;D:\localRepository\org\mybatis\mybatis-spring\2.1.0\mybatis-spring-2.1.0.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-actuator\2.7.10\spring-boot-starter-actuator-2.7.10.jar;D:\localRepository\org\springframework\boot\spring-boot-actuator-autoconfigure\2.7.10\spring-boot-actuator-autoconfigure-2.7.10.jar;D:\localRepository\org\springframework\boot\spring-boot-actuator\2.7.10\spring-boot-actuator-2.7.10.jar;D:\localRepository\io\micrometer\micrometer-core\1.9.9\micrometer-core-1.9.9.jar;D:\localRepository\org\hdrhistogram\HdrHistogram\2.1.12\HdrHistogram-2.1.12.jar;D:\localRepository\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\localRepository\org\springframework\boot\spring-boot-devtools\2.7.10\spring-boot-devtools-2.7.10.jar;D:\localRepository\org\springframework\boot\spring-boot\2.7.10\spring-boot-2.7.10.jar;D:\localRepository\org\springframework\boot\spring-boot-autoconfigure\2.7.10\spring-boot-autoconfigure-2.7.10.jar;D:\localRepository\com\mysql\mysql-connector-j\8.0.32\mysql-connector-j-8.0.32.jar;D:\localRepository\org\springframework\spring-core\5.3.26\spring-core-5.3.26.jar;D:\localRepository\org\springframework\spring-jcl\5.3.26\spring-jcl-5.3.26.jar;D:\localRepository\org\mybatis\generator\mybatis-generator-core\1.3.7\mybatis-generator-core-1.3.7.jar" com.example.baseproject.BaseProjectApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.10)
2023-04-05 17:59:09.705 INFO 26460 --- [ restartedMain] c.e.b.BaseProjectApplication : Starting BaseProjectApplication using Java 1.8.0_351 on DESKTOP-OQ55J3D with PID 26460 (D:\MyData\JavaData\WorkSpace\idea-projects\slf4j-log4j2-demo\target\classes started by luqing in D:\MyData\JavaData\WorkSpace\idea-projects\slf4j-log4j2-demo)
2023-04-05 17:59:09.760 INFO 26460 --- [ restartedMain] c.e.b.BaseProjectApplication : No active profile set, falling back to 1 default profile: "default"
2023-04-05 17:59:09.840 INFO 26460 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-04-05 17:59:09.840 INFO 26460 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-04-05 17:59:11.577 INFO 26460 --- [ restartedMain] o.s.b.w.e.t.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-04-05 17:59:11.579 INFO 26460 --- [ restartedMain] o.a.c.c.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.36] using APR version [1.7.2].
2023-04-05 17:59:11.580 INFO 26460 --- [ restartedMain] o.a.c.c.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
2023-04-05 17:59:11.580 INFO 26460 --- [ restartedMain] o.a.c.c.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2023-04-05 17:59:11.583 INFO 26460 --- [ restartedMain] o.a.c.c.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1t 7 Feb 2023]
2023-04-05 17:59:11.595 INFO 26460 --- [ restartedMain] o.a.c.c.StandardService : Starting service [Tomcat]
2023-04-05 17:59:11.596 INFO 26460 --- [ restartedMain] o.a.c.c.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.73]
2023-04-05 17:59:11.689 INFO 26460 --- [ restartedMain] o.a.c.c.C.[.[.[/] : Initializing Spring embedded WebApplicationContext
2023-04-05 17:59:11.689 INFO 26460 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1848 ms
2023-04-05 17:59:12.755 INFO 26460 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2023-04-05 17:59:12.761 INFO 26460 --- [ restartedMain] o.s.b.a.e.w.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2023-04-05 17:59:12.817 INFO 26460 --- [ restartedMain] o.s.b.w.e.t.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-04-05 17:59:12.835 INFO 26460 --- [ restartedMain] c.e.b.BaseProjectApplication : Started BaseProjectApplication in 3.809 seconds (JVM running for 5.464)
2023-04-05 17:59:13.262 INFO 26460 --- [)-192.168.255.1] c.z.h.HikariDataSource : HikariPool-1 - Starting...
2023-04-05 17:59:13.262 INFO 26460 --- [)-192.168.255.1] o.a.c.c.C.[.[.[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-04-05 17:59:13.263 INFO 26460 --- [)-192.168.255.1] o.s.w.s.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-04-05 17:59:13.266 INFO 26460 --- [)-192.168.255.1] o.s.w.s.DispatcherServlet : Completed initialization in 3 ms
2023-04-05 17:59:14.209 INFO 26460 --- [)-192.168.255.1] c.z.h.HikariDataSource : HikariPool-1 - Start completed.
2023-04-05 17:59:26.957 INFO 26460 --- [nio-8080-exec-2] c.e.b.c.UserController : info log
2023-04-05 17:59:26.958 WARN 26460 --- [nio-8080-exec-2] c.e.b.c.UserController : warn log
2023-04-05 17:59:26.958 ERROR 26460 --- [nio-8080-exec-2] c.e.b.c.UserController : error log
大概率还是默认的logback引用没有排除干净,所以log4j2的没生效
可以看一下maven的引用关系,找一下是谁引用了的,盲猜大概了是mybatis间接引了
判断是否生效,也可以配置spring的log level到debug,然后看启动日志,具体有没有加载相关配置