Play 2.4 - Display Ebeans SQL statement in logs

7.7k views Asked by At

How to display SQL Statements in the log ? I'm using EBeans and it fails to insert for some reasons but I can't see what's the problem.

I tried to edit my config to:

db.default.logStatements=true

and add this to logback.xml

<logger name="com.jolbox" level="DEBUG" />

to follow some answers I found online, but it doesn't seem to work for 2.4…

4

There are 4 answers

3
Nicolas B. On BEST ANSWER

Logging has changed with Play 2.4. Starting from now, to display the SQL statements in the console, simply add the following line to the conf/logback.xml file:

<logger name="org.avaje.ebean.SQL" level="TRACE" />

It should work just fine.

As @Flo354 pointed out in the comments, with Play 2.6 you should use:

<logger name="io.bean" level="TRACE" />
1
biesior On

FYI, there's nice video tutorial on Ebean's new doc page showing the way to capture SQL statements only for selected areas of the code.

Thanks to this you can log statements only in problematic places while developing and/or use the logged statements for performing tests as showed in video.

In short: add latest avaje-ebeanorm-mocker dependency to your built.sbt as usually, so later you can use it in your code like:

LoggedSql.start();
User user = User.find.byId(123);
// ... other queries
List<String> capturedLogs = LoggedSql.stop();

Note you don't even need to fetch the List of statements if you do not need to process them as they are displayed in the console as usually. So you can use it like this as well:

if (Play.isDev()) LoggedSql.start();
User user = User.find.byId(345);
// ... other queries
if (Play.isDev()) LoggedSql.stop();
0
Saeed Zarinfam On

From Play 2.5 Logging SQL statements is very easy, Play 2.5 has an easy way to log SQL statements, built on jdbcdslog, that works across all JDBC databases, connection pool implementations and persistence frameworks (Anorm, Ebean, JPA, Slick, etc). When you enable logging you will see each SQL statement sent to your database as well as performance information about how long the statement takes to run.

The SQL log statement feature in Play 2.5 can be configured by database, using logSql property:

db.default.logSql=true

After that, you can configure the jdbcdslog-exp log level by adding this lines to logback.xml:

  <logger name="org.jdbcdslog.ConnectionLogger" level="OFF"  /> <!-- Won' log connections -->
  <logger name="org.jdbcdslog.StatementLogger"  level="INFO" /> <!-- Will log all statements -->
  <logger name="org.jdbcdslog.ResultSetLogger"  level="OFF"  /> <!-- Won' log result sets -->
0
FrancisA On

I had success using jdbcdslog. As @Saeed Zarinfam mentioned here, Play 2.5 includes this by default.

Unlike this answer, this solution shows the parameter values instead of question marks.

Here are the steps I followed to get it working for Play 2.4 and MySQL:

Add to build.sbt:

"com.googlecode.usc" % "jdbcdslog" % "1.0.6.2"

Add to logback.xml:

<logger name="org.jdbcdslog.StatementLogger"  level="INFO" /> <!-- Will log all statements -->

Create conf/jdbcdslog.properties file containing:

jdbcdslog.driverName=mysql
jdbcdslog.showTime=true

Change db.default.url (example):

jdbc:mysql://127.0.0.1:3306/mydb

changes to jdbc:jdbcdslog:mysql://127.0.0.1:3306/mydb;targetDriver=com.mysql.jdbc.Driver

Change db.default.driver:

org.jdbcdslog.DriverLoggingProxy