Logging is the best way to debug an application for errors and information. In this recipe, we will look at how to introduce logging with actors. There are third-party libraries that provide logging to an application, such as sl4jLogger
. In Akka, logging is not tied to a particular API, but we will go with ActorLogging
, provided by Akka.
To step through this recipe, we do not require any other API dependencies; just import the Hello-Akka
project in the IDE.
- Create a file, say,
Logging.scala
, in thecom.packt.chapter5
package. - Add the following import to the file:
import akka.actor.{Props, ActorSystem, ActorLogging, Actor}
- Create an actor, as follows, that uses logging:
class LoggingActor extends Actor with ActorLogging { def receive = { case (a: Int, b: Int) => { log.info(s"sum of $a and $b is ${a + b}") } case msg => log.warning(s"i don't know what are...