Can log4jdbc be used with spring boot?

3.6k views Asked by At

I have a Spring boot app I'm trying to add database logging to which is better than

spring.jpa.properties.hibernate.show_sql=true

log4jdbc, from

https://github.com/marcosemiao/log4jdbc

seems to be the most up to date fork around, seems to format nicely, fills in parameters and adds timing, exactly what I want.

But when I configure it as stated in the readme, changing

spring.datasource.url=jdbc:mysql://localhost:3306/coindatabase?useSSL=false

to

spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/coindatabase?useSSL=false

something seems to not like my reference to mysql and seems to try to fall back to H2:

Caused by: java.lang.RuntimeException: Driver org.h2.Driver claims to not accept jdbcUrl, jdbc:log4jdbc:mysql://localhost:3306/coindatabase?useSSL=false
  at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:106)

Is there some easy way to make this work together?

3

There are 3 answers

1
Ian On BEST ANSWER

log4jdbc for spring boot wrapper:

<groupId>com.integralblue</groupId>
<artifactId>log4jdbc-spring-boot-starter</artifactId>

which seems to pull in the implementation from:

<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
0
judos On

You need to use this library in your build.gradle:

// https://mvnrepository.com/artifact/com.integralblue/log4jdbc-spring-boot-starter
compile group: 'com.integralblue', name: 'log4jdbc-spring-boot-starter', version: '2.0.0'

If you get the warning:
"Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary."
you can set the correct Driver yourself via properties:

log4jdbc.drivers=com.mysql.cj.jdbc.Driver  
log4jdbc.auto.load.popular.drivers=false

The documentation for configuration can be found on Github

0
Isaac Riley On

Additional info:

Don't modify the spring.datasource.url property in your Spring Boot application.properties file; leave the URL as previously defined to access your MYSQL instance.

Instead, after grabbing the com.integralblue maven target, simply set the logging level of choice (ex logging.level.jdbc.sqltiming=info) and your previously defined log4j log will have the DB stuff in it.

See here as was well