How to log SQL with log4jdbc?

3.9k views Asked by At

In a 2.0 Playframework project, I would like to analyze issued SQL queries by Slick. I am using log4jdbc for that but I can't get it to work.

I have added the log4jdbc.jar file in lib/ folder and changed my application.conf file to following :

db.default.driver=net.sf.log4jdbc.DriverSpy
db.default.url="jdbc:log4jdbc:mysql://127.0.0.1:3306/mydatabase"
db.default.user="username"
db.default.pass="password"

It is resulting in a Cannot connect to database [default] error. Without the log4jdbc, everything works fine. Am I missing something?

Ps : an important notice, I am also using the play-slick extension which seems to be the origin of the problem.

EDIT : By searching a little deeper, the exact error is "No suitable driver found".

3

There are 3 answers

1
mandubian On

Apparently Play DB Plugin doesn't prevent from using this kind of url. If you look in DB.scala in module play-jdbc Line 345, it just passes the url to JDBC.

conf.getString("url") match {
  case Some(PostgresFullUrl(username, password, host, dbname)) =>
    datasource.setJdbcUrl("jdbc:postgresql://%s/%s".format(host, dbname))
    datasource.setUsername(username)
    datasource.setPassword(password)
  case Some(url @ MysqlFullUrl(username, password, host, dbname)) =>
    val defaultProperties = """?useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci"""
    val addDefaultPropertiesIfNeeded = MysqlCustomProperties.findFirstMatchIn(url).map(_ => "").getOrElse(defaultProperties)
    datasource.setJdbcUrl("jdbc:mysql://%s/%s".format(host, dbname + addDefaultPropertiesIfNeeded))
    datasource.setUsername(username)
    datasource.setPassword(password)
  case Some(url @ H2DefaultUrl()) if !url.contains("DB_CLOSE_DELAY") =>
    if (Play.maybeApplication.exists(_.mode == Mode.Dev)) {
      datasource.setJdbcUrl(url + ";DB_CLOSE_DELAY=-1")
    } else {
      datasource.setJdbcUrl(url)
    }
  case Some(s: String) =>
    datasource.setJdbcUrl(s)
  case _ =>
    throw conf.globalError("Missing url configuration for database [%s]".format(conf))
}

So the pb might be in the datasource itself (BoneCP)

1
Antoine On

Have you tried setting db.default.logStatements=true in your application.conf? Maybe it's just supported by Anorm?

1
kuhnroyal On

Have you tried using logger.com.jolbox.bonecp=DEBUG and/or logger.scala.slick=DEBUG instead? That is usually more than enough logging, just can't seem to get some transaction logging going.

Oh and you need to set db.default.logStatements=true for the bonecp logging to work.