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?
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.
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 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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With