2 / 2
Jul 2024

Golang mongo driver uses an interface LogSink which has the following structure:

type LogSink interface { // Info logs a non-error message with the given key/value pairs. The // level argument is provided for optional logging. Info(level int, msg string, keysAndValues ...interface{}) // Error logs an error, with the given message and key/value pairs. Error(err error, msg string, keysAndValues ...interface{}) }

This interface does not tells anything about context.Context. How can we transfer some information to the logger as there is no context involved ? My use case: I want to send query logs to some external service along with some user information that will be available in http or grpc request.

8 days later

There’s currently no simple way to access Context values via the LogSink interface. I created GODRIVER-3265 to investigate ways to support passing user-defined values through the logger interface.

Depending on what information you’re trying to log, you may be able to use a CommandMonitor instead, which passes the operation Context to the callback.