Michał Jurczuk
Michał Jurczuk

Reputation: 3828

Logback no ExceptionInInitializerError in file only on console

I'm using logback 1.7.5 with play framework 2.2.4, during working I'm getting such error:

Exception in thread "Thread-5" java.lang.ExceptionInInitializerError
    at controllers.db.SyncDBManager.createDeviceCollectionsIfNotExists(SyncDBManager.scala)
    at server.impl.logic.controller.DeviceInitializer.checkAuthenticationResponse(DeviceInitializer.java:226)
    at server.impl.logic.controller.DeviceInitializer.processReceivedFrames(DeviceInitializer.java:117)
    at server.impl.logic.controller.DeviceController.onReceivedPackets(DeviceController.java:77)
    at server.impl.logic.io.DeviceReader.run(DeviceReader.java:130)
Caused by: java.util.NoSuchElementException: None.get
    at scala.None$.get(Option.scala:313)
    at scala.None$.get(Option.scala:311)
    at controllers.base.MongoSyncHelper$class.$init$(MongoSyncHelper.scala:16)
    at controllers.db.SyncDBManager$.<init>(SyncDBManager.scala:32)
    at controllers.db.SyncDBManager$.<clinit>(SyncDBManager.scala)
    ... 5 more

But only on console, how can I catch such exception and write it to file?

Upvotes: 0

Views: 220

Answers (1)

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

Seems like an ordinary stack trace in a console printed with the sysout. Depending on your design if you want to log an exception with the Play logger you should do it in a catch block with one of logger's methods by passing an exception as one of parameters. For example:

try {
  // code
} catch {
  case e: Exception => play.api.Logger.error("An error occurred", e)
}

The logger level is up to you. If you don't have a catch block and want to log an unexpected exception that occurs in any place of an application you should log it in the Global object by overriding onError method.

override def onError(request: RequestHeader, e: Throwable): Future[SimpleResult] = {
  play.api.Logger.error("An error occurred", e)
  super.onError(request, e)
}

Remember that onError method is called only in the production mode.

Upvotes: 1

Related Questions