Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.4 - Display Ebeans SQL statement in logs

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…

like image 719
Gonzague Avatar asked Jun 14 '15 10:06

Gonzague


3 Answers

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" />
like image 179
Nicolas B. Avatar answered Nov 14 '22 17:11

Nicolas B.


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 -->
like image 10
Saeed Zarinfam Avatar answered Nov 14 '22 16:11

Saeed Zarinfam


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();
like image 5
biesior Avatar answered Nov 14 '22 15:11

biesior