Logging is a crucial aspect of microservices. We can write middleware to capture all requests and responses going into and out of a service. Even for a client, we can capture logs while making RPC calls to a service.
Go Micro is a lean framework and doesn't enforce logging by default. We can easily wrap a service handler with our own custom logger. For example, in the encryptService example, we have a file called handlers.go.
In order to activate logging for each request in a custom format, we have to define a wrapper, and then link it to the service. As an example, if we have to log every incoming encryption request, follow these steps:
- Create a new wrapper function. It takes Context, Request, and Response as arguments. Here, we just print the time of the request arrival, like this:
func logWrapper(fn server.HandlerFunc) server.HandlerFunc...