ARTICLE AD BOX
We have a situation where we need to write the logs from an app running in multiple pods on an OpenShift project to the database so I slapped together a quick database logging POC with log4j2, and got it working. They won't let me setup any type of log aggregation like ELK or Graylog so this is what I need to do, and it works surprisingly well....kind of, until I needed to use Spring Boot.
log4j2.xml:
<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>Spring Boot app has been problematic, I've excluded the logback logging in the pom.
pom.xml:
<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>The log4j2.xml file is the same with minor changes:
<Loggers> <root level="error"> <AppenderRef ref="Console"/> </root> <Logger name="com.cspi" level="debug" additivity="false"> <AppenderRef ref="Database"/> </Logger> </Loggers>Nothing is getting logged to the database unless I use just root:
<Loggers> <root level="debug"> <AppenderRef ref="Database"/> <AppenderRef ref="Console"/> </root> </Loggers>If I add the console appender to it's own logger, I can see the logs in the console, but they don't appear in the database.
<Loggers> <root level="error"> <AppenderRef ref="Console"/> </root> <Logger name="com.cspi" level="debug" additivity="false"> <AppenderRef ref="Database"/> <AppenderRef ref="Console"/> </Logger> </Loggers>The dependency tree looks like this:
[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</pre>Any ideas on what's going on here?
