3

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?

Ian
  • 1,507
  • 3
  • 21
  • 36

3 Answers3

4

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>
Ian
  • 1,507
  • 3
  • 21
  • 36
  • this is really nice. It's a bit of a shame that the default configuration is too verbose. Luckly the documentation is really easy to read. https://github.com/candrews/log4jdbc-spring-boot-starter – soung Feb 04 '23 at 03:06
3

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

Isaac Riley
  • 290
  • 4
  • 5
2

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

judos
  • 445
  • 4
  • 14