akisaarinen
akisaarinen

Reputation: 111

Guaranteeing log output flush in Akka upon system shutdown?

I'm using Akka FSM with Scala and logging using the log member of the FSM trait, i.e. an instance of akka.event.Logging.

When calling context.system.shutdown() to shutdown the actor system when the application wants to terminate normally, it seems that latest log messages are sometimes lost (and not printed out at all).

Is there way to guarantee that all log messages are always printed out before the system is shutdown?

Upvotes: 4

Views: 660

Answers (1)

fbl
fbl

Reputation: 2910

Based on Dr. Kuhn's suggestion, here is a sample TerminatingLogger. I put mine in the error() channel so that it won't fall into the string comparison often, but you can obviously put it wherever you want.

class TerminatingLogger extends Actor with StdOutLogger {
    override def receive: Receive = {
      case InitializeLogger(_) ⇒ sender() ! LoggerInitialized
      case error : akka.event.Logging.Error if error.message.toString().equals("TERMINATE_SYSTEM") =>
      {
        context.system.shutdown()
      }
      case event: LogEvent     ⇒ 
      {
       print(event) 
      }
    }
  }

Then just configure it in your application.conf file

akka {
  loggers = [com.namespace.TerminatingLogger]
}

Upvotes: 1

Related Questions