Logging with additional fields in web services

Hello :slight_smile: I got a question about logging that I’ve been thinking on for some time, wondering if someone could help me out:

Let’s say I’m writing a new gRPC service with a bunch of endpoints. A pattern that I’ve become fond of is to isolate the core logic from the gRPC related code. So the gRPC handlers will be very simple, and only call other functions/methods that do the main work. I like this style because most of the code becomes independent of gRPC (or http, or kafka, or whatever is the input to the service).

However, there is a problem which I struggle with: logging. I think it is appropriate to log in the gRPC
handler, then we can log an error if an error occurred, or an info message if all is ok. However, the gRPC request usually contains some additional data which would be nice to add to the log statement, which sometimes needs some handling to be extracted. One way to deal with this is to partially handle the request in the gRPC handler before the core logic, but this results in handling parts of the message twice, or it results in sending in the partially handled message plus the full message to the core logic. None of which is ideal, I think.

Has anyone else here struggled with this? Is there a nice way to solve it? Thanks in advance :slight_smile: