如何记录播放框架启动错误

How to log play framework startup errors

数据库连接错误未在生产模式中记录

在以下堆栈跟踪中,只有第一行:[info] application - Creating Pool for datasource 'default' 转到 application.log。异常仅在 play.core.server.RealServerProcess#exit

中打印到 stderr

我可以使用 http://projects.lidalia.org.uk/sysout-over-slf4j/quickstart.html,但它不能在应用程序 onStart 中完成,因为数据库连接之前发生过,所以也许我需要编写一个模块?

[info] application - Creating Pool for datasource 'default'
Oops, cannot start the server.
Configuration error: Configuration error[Cannot connect to database [default]]
    at play.api.Configuration$.configError(Configuration.scala:178)
    at play.api.Configuration.reportError(Configuration.scala:829)
    at play.api.db.DefaultDBApi$$anonfun$connect.apply(DefaultDBApi.scala:48)
    at play.api.db.DefaultDBApi$$anonfun$connect.apply(DefaultDBApi.scala:42)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
    at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:72)
    at play.api.db.DBApiProvider.get(DBModule.scala:62)
    at play.api.db.DBApiProvider.get(DBModule.scala:58)
    at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
    at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:72)
    at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:61)
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:62)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:54)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
    at com.google.inject.internal.MembersInjectorImpl.call(MembersInjectorImpl.java:93)
    at com.google.inject.internal.MembersInjectorImpl.call(MembersInjectorImpl.java:80)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
    at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:62)
    at com.google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:984)
    at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
    at com.google.inject.util.Providers$GuicifiedProviderWithDependencies$$FastClassByGuice$a7177aa.invoke(<generated>)
    at com.google.inject.internal.cglib.reflect.$FastMethod.invoke(FastMethod.java:53)
    at com.google.inject.internal.SingleMethodInjector.invoke(SingleMethodInjector.java:57)
    at com.google.inject.internal.SingleMethodInjector.inject(SingleMethodInjector.java:91)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
    at com.google.inject.internal.MembersInjectorImpl.call(MembersInjectorImpl.java:93)
    at com.google.inject.internal.MembersInjectorImpl.call(MembersInjectorImpl.java:80)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
    at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
    at com.google.inject.internal.Initializer$InjectableReference.get(Initializer.java:174)
    at com.google.inject.internal.Initializer.injectAll(Initializer.java:108)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:174)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
    at com.google.inject.Guice.createInjector(Guice.java:96)
    at com.google.inject.Guice.createInjector(Guice.java:73)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:126)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:93)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
    at play.core.server.ProdServerStart$.start(ProdServerStart.scala:52)
    at play.core.server.ProdServerStart$.main(ProdServerStart.scala:27)
    at play.core.server.ProdServerStart.main(ProdServerStart.scala)
Caused by: Configuration error: Configuration error[Exception during pool initialization]
    at play.api.Configuration$.configError(Configuration.scala:178)
    at play.api.PlayConfig.reportError(Configuration.scala:1048)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:69)
    at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
    at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:124)
    at play.api.db.DefaultDatabase.dataSource(Databases.scala:122)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:143)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:139)
    at play.api.db.DefaultDBApi$$anonfun$connect.apply(DefaultDBApi.scala:44)
    ... 41 more
Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
    at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:542)
    at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:171)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:60)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:48)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:80)
    at play.api.db.HikariCPConnectionPool$$anonfun.apply(HikariCPModule.scala:58)
    at play.api.db.HikariCPConnectionPool$$anonfun.apply(HikariCPModule.scala:54)
    at scala.util.Try$.apply(Try.scala:192)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:54)
    ... 47 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
    at com.mysql.jdbc.Util.getInstance(Util.java:372)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:958)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
    at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2149)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2074)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:92)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:98)
    at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:438)
    at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:540)
    ... 55 more
Caused by: java.sql.SQLException: Access denied for user 'ilya-'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2239)
    at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2090)
    ... 69 more

https://www.playframework.com/documentation/2.4.x/JavaDependencyInjection

子类化 GuiceApplicationLoader 并且它有效

play.application.loader = "modules.LogSdtErrLoader"

LogSdtErrLoader.scala

package modules

import play.api.ApplicationLoader
import play.api.inject.guice.{GuiceApplicationBuilder, GuiceApplicationLoader}
import uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4J

class LogSdtErrLoader extends GuiceApplicationLoader {

  override final def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J()
    super.builder(context)
  }
}