Spring Boot整合Log4j2数据库Appender时自定义日志器无法写入数据库的问题求助
Spring Boot整合Log4j2数据库Appender时自定义日志器无法写入数据库的问题求助
各位好,我现在卡在一个Spring Boot结合Log4j2的日志问题上了,折腾好一阵没头绪,来请大家帮忙看看!
背景情况
因为环境限制不能用ELK、Graylog这类日志聚合工具,所以我打算用Log4j2的JDBC Appender直接把日志写入数据库。这个方案在普通Java项目里已经跑通了,但迁移到Spring Boot项目后就出问题了。
我的配置细节
1. Maven依赖(已排除默认Logback)
我在pom.xml里给所有starter都排除了spring-boot-starter-logging,引入了spring-boot-starter-log4j2,具体配置如下:
<properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</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-webmvc</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-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2. Log4j2配置文件
我用了自定义的ConnectionFactory来获取数据库连接,JDBC Appender的配置如下,Spring Boot里我调整了Loggers部分:
<Configuration status="debug"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SS} %p %c.%M:%L - %m%n" /> </Console> <JDBC name="Database" tableName="CSPI_LOGS" bufferSize="100"> <ConnectionFactory class="com.cspi.ConnectionFactory" method="getDatabaseConnection"/> <Column name="SERVER" literal="'${sys:jvm.name}'" /> <Column name="FACILITY" pattern="%marker" /> <Column name="LOG_TIME" isEventTimestamp="true" /> <Column name="LOG_LEVEL" pattern="%level" /> <Column name="CODE_LOC" pattern="%c.%M:%L" /> <Column name="THREAD" pattern="[%t:%tid]"/> <Column name="MESSAGE" pattern="%message" /> <Column name="EXCEPTION" pattern="%ex{full}" /> </JDBC> </Appenders> <Loggers> <root level="error"> <AppenderRef ref="Console"/> </root> <logger name="org" level="warn" additivity="false"></logger> <Logger name="com.cspi" level="debug" additivity="false"> <AppenderRef ref="Database"/> <AppenderRef ref="Console"/> </Logger> </Loggers> </Configuration>
诡异的问题现象
- 当我把
DatabaseAppender挂在root日志器上时(如下配置),日志能正常写入数据库:
<Loggers> <root level="debug"> <AppenderRef ref="Database"/> <AppenderRef ref="Console"/> </root> </Loggers>
- 但如果用我自定义的
com.cspi日志器,情况就不对了:- 控制台Appender能正常输出
com.cspi包下的debug级日志 - 但完全没有日志写入数据库,哪怕我给这个日志器加了
DatabaseAppender,级别设为debug,additivity设为false
- 控制台Appender能正常输出
我还检查了Maven依赖树,确认没有残留的Logback或者其他日志框架冲突,依赖树如下:
[INFO] +- org.springframework.boot:spring-boot-starter-quartz:jar:4.0.0:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:4.0.0:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:4.0.0:compile [INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:3.0.0:compile [INFO] | | \- org.yaml:snakeyaml:jar:2.5:compile [INFO] | \- org.springframework.boot:spring-boot-quartz:jar:4.0.0:compile [INFO] | +- org.springframework.boot:spring-boot-transaction:jar:4.0.0:compile [INFO] | | +- org.springframework.boot:spring-boot-persistence:jar:4.0.0:compile [INFO] | | \- org.springframework:spring-tx:jar:7.0.1:compile [INFO] | +- org.quartz-scheduler:quartz:jar:2.5.1:compile [INFO] | | \- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.4:runtime [INFO] | | \- jakarta.activation:jakarta.activation-api:jar:2.1.4:runtime [INFO] | \- org.springframework:spring-context-support:jar:7.0.1:compile [INFO] | +- org.springframework:spring-beans:jar:7.0.1:compile [INFO] | +- org.springframework:spring-context:jar:7.0.1:compile [INFO] | \- org.springframework:spring-core:jar:7.0.1:compile [INFO] | +- commons-logging:commons-logging:jar:1.3.5:compile [INFO] | \- org.jspecify:jspecify:jar:1.0.0:compile [INFO] +- org.springframework.boot:spring-boot-starter-webmvc:jar:4.0.0:compile [INFO] | +- org.springframework.boot:spring-boot-starter-jackson:jar:4.0.0:compile [INFO] | | \- org.springframework.boot:spring-boot-jackson:jar:4.0.0:compile [INFO] | | \- tools.jackson.core:jackson-databind:jar:3.0.2:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.20:compile [INFO] | | \- tools.jackson.core:jackson-core:jar:3.0.2:compile [INFO] | +- org.springframework.boot:spring-boot-http-converter:jar:4.0.0:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:4.0.0:compile [INFO] | | \- org.springframework:spring-web:jar:7.0.1:compile [INFO] | | \- io.micrometer:micrometer-observation:jar:1.16.0:compile [INFO] | | \- io.micrometer:micrometer-commons:jar:1.16.0:compile [INFO] | \- org.springframework.boot:spring-boot-webmvc:jar:4.0.0:compile [INFO] | +- org.springframework.boot:spring-boot-servlet:jar:4.0.0:compile [INFO] | \- org.springframework:spring-webmvc:jar:7.0.1:compile [INFO] | +- org.springframework:spring-aop:jar:7.0.1:compile [INFO] | \- org.springframework:spring-expression:jar:7.0.1:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:4.0.0:provided [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat-runtime:jar:4.0.0:provided [INFO] | | +- org.springframework.boot:spring-boot-web-server:jar:4.0.0:provided [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:11.0.14:provided [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:11.0.14:provided [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:11.0.14:provided [INFO] | \- org.springframework.boot:spring-boot-tomcat:jar:4.0.0:provided [INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:4.0.0:compile [INFO] | +- org.springframework.boot:spring-boot-jdbc:jar:4.0.0:compile [INFO] | | +- org.springframework.boot:spring-boot-sql:jar:4.0.0:compile [INFO] | | \- org.springframework:spring-jdbc:jar:7.0.1:compile [INFO] | \- com.zaxxer:HikariCP:jar:7.0.2:compile [INFO] | \- org.slf4j:slf4j-api:jar:2.0.17:compile [INFO] +- com.oracle.database.jdbc:ojdbc8:jar:23.9.0.25.07:compile [INFO] \- org.springframework.boot:spring-boot-starter-log4j2:jar:4.0.0:compile [INFO] +- org.apache.logging.log4j:log4j-slf4j2-impl:jar:2.25.2:compile [INFO] | \- org.apache.logging.log4j:log4j-api:jar:2.25.2:compile [INFO] +- org.apache.logging.log4j:log4j-core:jar:2.25.2:compile [INFO] \- org.apache.logging.log4j:log4j-jul:jar:2.25.2:compile
我已经排查的点
- 确认ConnectionFactory能正常获取数据库连接(root日志器模式下能写入,说明连接没问题)
- 确认自定义日志器的包名
com.cspi和代码里的Logger实例包名完全一致 - 开启了Log4j2的debug状态输出,没看到明显的错误提示
有没有朋友遇到过类似的情况?或者能帮我指出哪里可能漏了配置?感谢大家!




