Actor

The main type is the LogActor struct, which contains a Logger instance in the writer field:

pub struct LogActor {
writer: Logger<LoggerBackend, String, Formatter3164>,
}

We'll use this logger in the Handler trait implementation to write messages, but now we need a constructor for our struct, because we have to configure Logger on start:

impl LogActor {
pub fn new() -> Self {
let formatter = Formatter3164 {
facility: Facility::LOG_USER,
hostname: None,
process: "rust-microservice".into(),
pid: 0,
};
let writer = syslog::unix(formatter).unwrap();
Self {
writer,
}
}
}

We added new method that fills the Formatter3164 struct with the Facility value and process name. Other fields are set to blank values. We create a Logger instance by calling the syslog::unix method and providing a formatter to it. We store the Logger in the writer field and return an instance of the LogActor struct.

To add the actor's behavior, we'll implement the Actor trait for the LogActor struct:

impl Actor for LogActor {
type Context = Context<Self>;
}

Since this actor will work in the same thread with a server instance and a counting actor, we'll use the basic Context type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.27.251