Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka Logging outside Actor

I have an Akka Actor that makes a call to MyObject.foo(). MyObject is not an Actor. How do I setup Logging in it? With an Actor it's simple, because I can just mixin ActorLogging. In MyObject, I don't have access to context.system. Do I create an akka.event.Logging with AkkaSystem() and then what for the LogSource implicit?

like image 263
Bradford Avatar asked Apr 14 '12 21:04

Bradford


People also ask

What is Akka logging?

Logging in Akka is not tied to a specific logging backend. By default log messages are printed to STDOUT, but you can plug-in a SLF4J logger or your own logger. Logging is performed asynchronously to ensure that logging has minimal performance impact.

How do you stop Akka Actor?

In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor.

What is Actor system in Akka?

What is an Actor in Akka? An actor is essentially nothing more than an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognize the type of message it has received and take action accordingly.

What does the life cycle postStop do?

2) postStop() This method is used to release resources after stopping the Actor. It may be used for deregistering this Actor. Messages sent to a stopped actor will be redirected to the deadLetters of the ActorSystem.


2 Answers

Actually I would redirect Akka logging to slf4j and use this API directly in all unrelated classes. First add this to your configuration:

akka {     event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]     loglevel = "DEBUG" } 

Then choose some SLF4J implementation, I suggest logback. In your actors continue using ActorLogging trait. In other classes simply rely on SLF4J API - or even better - try out slf4s facade around SLF4J.

Tip: try out the following logging pattern in Logback:

<pattern>%d{HH:mm:ss.SSS} | %-5level | %thread | %X{akkaSource} | %logger{1} | %m%n%rEx</pattern> 

The %X{akkaSource} will print actor path when available (just like standard logging).

like image 198
Tomasz Nurkiewicz Avatar answered Sep 22 '22 03:09

Tomasz Nurkiewicz


Using Akka 2.2.1, I was able to put this into my App to get logging outside of an actor:

import akka.event.Logging val system = ActorSystem("HelloSystem", ConfigFactory.load.getConfig("akka")) val log = Logging.getLogger(system, this) log.info("Hi!") 

This seems like a simpler solution for unifying an application's logging.

like image 34
bnsmith Avatar answered Sep 21 '22 03:09

bnsmith