Service Fundamentals: Logging
In order to support services running in production the first fundamental requirement of a service implementation is knowing exactly what is going inside of your service. Through production logging you can get the needed insights into the service. At a minimum it is ideal to log the parameters being passed into the service and the result of the service operation.
The format of the log is largely dependent on the systems being implemented. JSON makes for a natural and easy choice when logging. An example of a log entry could be.
Request
{
"name": "John Doe",
"email": "[email protected]",
"account": "standard",
"correlationId": "01234567-1234-1234-1234-012345678901"
}
Response
{
"data": {
"accountId": 96453201,
},
"meta": {
"result": "success",
"correlationId": "01234567-1234-1234-1234-012345678901"
}
}
Correlation Identifier
A couple of approaches to logging could be:
- Delay the logging until the completion of the service execution. The risk here is that the service fails to execute to completion due to some exception or system error and the log record is never written for the request or the response.
- Log the request as soon as you see it. Then log the response once it has been created. On the plus side the request is logged regardless of any errors or issues during the service execution. The downside is that the request log and the response log exist at two different points in the log file (assuming the service is processing other requests concurrently). To combat this we need to incorporate a correlation ID. A unique value that we can use while searching the logs to identify all log entries associated with a specific service execution. For a call that cross multiple services a common correlation ID can help identify the entire cross-service call stack.
Sensitive Data
A legitimate concern of logging is logging out sensitive data. Ideally the service implementation will provide a safe approach to resolving this concern so that the developer does not have to think too hard about solving this issue. We'll discuss an approach to logging sensitive data in a future post.
Pipeline
To ensure logs are written for each request and response without the developer having to intentionally perform this step we will introduce logging into the pipeline so that each request and response is logged automatically.
