We can now add logging to our microservice. In the following example, we will print information about the socket address, the incoming request, and a generated random value:
fn main() {
logger::init();
info!("Rand Microservice - v0.1.0");
trace!("Starting...");
let addr = ([127, 0, 0, 1], 8080).into();
debug!("Trying to bind server to address: {}", addr);
let builder = Server::bind(&addr);
trace!("Creating service handler...");
let server = builder.serve(|| {
service_fn_ok(|req| {
trace!("Incoming request is: {:?}", req);
let random_byte = rand::random::<u8>();
debug!("Generated value is: {}", random_byte);
Response::new(Body::from(random_byte.to_string()))
})
});
info!("Used address: {}", server.local_addr());
let server = server.map_err(drop);
debug!("Run!");
hyper::rt::run(server);
}
Using logging is quite simple. We can use macros to print the address of the socket and information about the request and response.