journal icon indicating copy to clipboard operation
journal copied to clipboard

Fatal exceptions are not logged

Open mtomko opened this issue 8 years ago • 0 comments

This is perhaps due to the threaded/asynchronous nature of the journal library, but fatal exceptions are not logged unless they are handled specially. This issue may be more one of documentation, as a workaround exists, but it may be worth investigating whether it would be possible to deal with the problem so the workaround was not necessary.

To reproduce this issue with 2.2.1, run:

import journal._

object RapidDeath extends App {

  val log = Logger[this.type]

  try {
    throw new Exception("Oh no!")
  } catch {
    case t: Throwable => log.error("Died", t)
  }

}

No log file is written, perhaps because the application terminates (along with the logger threads) before any buffers are flushed and a message is written.

Conversely, you can ensure that the exception will be logged with:

import journal._

object RapidDeath extends App {

  val log = Logger[this.type]

  try {
    throw new Exception("Oh no!")
  } catch {
    case t: Throwable => log.backend.error("Died", t)
  }

}

Note the direct call to log.backend in the catch block. This appears to bypass the threading, ensuring the message reaches the log before the application terminates. I think this is mostly a reasonable workaround, so long as your application is sufficiently simple to have just one or two places where this specialized try/catch/log behavior is necessary, but it might merit a note in the documentation.

mtomko avatar Mar 15 '16 17:03 mtomko