|
|
|
|
|
by zknill
1036 days ago
|
|
log/slog package essentially delegates writing log messages to some "handler" interface. The key method is: Handle(context.Context, Record) error
This method has access to the context, which means you can get the logging handler to extract values from the context. Instead of storing the logger on the context, you can extract the traceId, etc values from the context and log those.It's a little bit involved to write a whole logger from scratch, but you can 'wrap' the existing logger handlers and include values from the context relatively easily. There are examples in this project (which aims to help solve your usecase): https://github.com/zknill/slogmw |
|
https://github.com/indexsupply/x/blob/main/wslog/slog_test.g...