Play Framework 2.0 and Ebean SQL logging
Asked Answered
H

3

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).

Heiskell answered 15/3, 2012 at 12:22 Comment(0)
S
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.

Sole answered 14/8, 2012 at 15:53 Comment(0)
H
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
Hairpin answered 15/3, 2012 at 12:47 Comment(6)
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.Heiskell
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 ?Hairpin
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.Heiskell
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.Hairpin
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.Heiskell
it's weird, i can't help more.Hairpin
A
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());
Apophasis answered 31/8, 2016 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.