Play 2.4 - Display Ebeans SQL statement in logs
Asked Answered
P

4

17

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…

Poltroon answered 14/6, 2015 at 10:28 Comment(0)
J
30

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" />
Jahdai answered 28/6, 2015 at 12:57 Comment(3)
It's possible do it in code? Only activate when Play.isDev()!Expectorant
To avoid logging SQL statements in production, you could use two different files: the default one for development (logback.xml) and another one for production (logback-production.xml). Then, to use the production one, add -Dlogger.resource=conf/logback-production.xml.Jahdai
Question is for Play 2.4, but as of Play 2.6, org.avaje.ebean is now io.ebeanFiliform
E
11

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 -->
Enjambment answered 18/7, 2016 at 7:2 Comment(0)
E
5

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();
Embarkation answered 22/10, 2015 at 11:51 Comment(1)
This isn't working for me. Play 2.4.2 and "com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0", "org.avaje.ebeanorm" % "avaje-ebeanorm-mocker" % "6.15.1" I am getting empty set. Am I missing anything obvious ?Murrelet
B
0

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
Binder answered 9/11, 2017 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.