Where to see the logged sql statements in play2?

牧云@^-^@ 提交于 2019-11-27 17:50:25

1. application.conf

make sure:

db.default.logStatements=true

This config is actually a setting of bonecp which is connection pool used in play2

2. custom logger

Add a custom logger configuration to conf/logger.xml.

The content may be:

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.jolbox.bonecp" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="play" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="application" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

</configuration>

The com.jlbox.bonecp is for bonecp, and play and application are for play2.

3. disable logger settings in application.conf

Comment the logger settings in application.conf:

# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .

# Root logger:
# logger.root=ERROR

# Logger used by the framework:
# logger.play=INFO

# Logger provided to your application:
# logger.application=DEBUG

Restart play, and you will see all executed SQLs(including parameter values).

This no longer works in Play 2.4.2 from what I can tell. The default connection pool engine was changed over to HikariCP.

Add this to your application.conf and follow the directions below. Things should work:

Application.conf

db.default.pool = "bonecp"
db.default.bonecp.logStatements=true

conf/logger.xml Add a custom logger configuration to conf/logger.xml.

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.jolbox.bonecp" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="play" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="application" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

</configuration>

Just add the following to application.conf (works for me in play 2.2.1)

db.default.logStatements=true

logger.com.jolbox.bonecp=DEBUG

For HikariCP (i.e. starting with Play 2.4), see https://github.com/brettwooldridge/HikariCP/wiki/JDBC-Logging:

HikariCP does not inherently include JDBC logging at this time. This is a conscious decision, not an oversight or undeveloped future roadmap item. Nearly all major database have a JDBC driver capable of logging on its own. For those that do not, log4jdbc-log4j2 is a good option.

This wiki page documents how to enable logging for common databases, as well as log4jdbc-log4j2.

For log4jdbc-log4j2: add "org.bgee.log4jdbc-log4j2" % "log4jdbc-log4j2-jdbc4.1" % "1.16" to libraryDependencies; configuration is described at https://code.google.com/archive/p/log4jdbc-log4j2/.

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