Play Framework 2.0 and Ebean SQL logging

白昼怎懂夜的黑 提交于 2019-11-26 14:29:33

问题


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


回答1:


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.




回答2:


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



回答3:


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());


来源:https://stackoverflow.com/questions/9719601/play-framework-2-0-and-ebean-sql-logging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!