24

I want to examine what SQL statements are generated by Ebean to find out why certain exceptions (related to SQL syntax) are occurring in my Play 2.0 application. Is there a way to log the SQL statements generated by Ebean in Play Framework 2.0?

In Play 1.x, there is a jpa.debugSQL config option, which if set to true, will do exactly this. Does a similar setting for Ebean exist for Play 2.0? The documentation page about Ebean of Play 2.0 is still a bit scarce.


What I have tried so far:

I have added these method calls in my controllers and the onStart / onRequest methods of the Global object, but it doesn't have any effect:

Ebean.getServer(null).getAdminLogging().setLogLevel(LogLevel.SQL);
Ebean.getServer(null).getAdminLogging().setDebugGeneratedSql(Play.isDev());

I have modified the log levels from application.conf, but it didn't help either (even with log level TRACE).

Tommi
  • 8,550
  • 5
  • 32
  • 51

3 Answers3

53

Sorry to be late to the party, but I use this in development:

db.default.logStatements=true

logger.com.jolbox=DEBUG

Add those two lines to the application.conf and you are good to go.

It outputs all the sql statements. Hope it helps.

mbseid
  • 1,044
  • 10
  • 18
4

You can enable SQL logging by using the following statement

Ebean.getServer(null).getAdminLogging().setDebugGeneratedSql(true);

Use this command in the onRequest interceptor for example

In a next release, you will certainly be able to configure this in the file ebean.properties.

// Tips : use Play.isDev() to log only in dev mode
Julien Lafont
  • 7,869
  • 2
  • 32
  • 52
  • Thanks. I tried your suggestion (I added the above statement to both `onStart` and `onRequest` methods in my Global object), but I still do not see any SQL statements in my application's logs. – Tommi Mar 15 '12 at 13:04
  • I haven't try in Play2.0 final,i will retry soon (RC3 last test). Try to change Log level in the application.conf or in the AdminLogging instance. Do you use "default" as database source ? – Julien Lafont Mar 15 '12 at 13:46
  • Yes, "default" datasource. Tried setting the log level in application.conf and AdminLogging instance. A lot of other stuff appear in the logs, but alas, no SQL statements, even with log level `TRACE`. – Tommi Mar 16 '12 at 06:10
  • 2 questions : try by adding this line directry in your controller, and if it works, are you sure your Global object is well defined ? (there is a config to set if the global object is not in the root package, symply add a Sysout to see whether it works). I tried with Play 2.0 final yesterday and it works. – Julien Lafont Mar 16 '12 at 09:14
  • The Global object is ok. I have `Logger.info()` calls in both `onStart` and `onRequest` methods and I can see their output in the console. Even so, I still added it in the controller as well - no effect. So, the Ebean methods must get called, but for some reason, do not work. – Tommi Mar 16 '12 at 10:17
  • it's weird, i can't help more. – Julien Lafont Mar 16 '12 at 12:51
0

Also you can get SQL on the spot by using method getGeneratedSQL. Code sample below

        Query<PreventionActivity> where = find.where(
            and(
                    and(
                    ge("Age_FROM", age)
                    , or(le("Age_TO", age), eq("Age_TO", 0))
                    )
                    , or(eq("Gender_CD", genderCd), eq("Gender_CD", "*"))
            )
    );

    List<PreventionActivity> list = where.findList();
    Logger.info("sql ");
    Logger.info(where.getGeneratedSql());
Ev.Rei.
  • 251
  • 2
  • 8