I'm not really confortable with context, but here's the thing...
I've created a middleware that is used by a gin server and that sets a correlation ID in the logger context.
package log
var l *logger
type logger struct {
logger zerolog.Logger
}
...
const correlationHeader = "Correlation-ID"
const correlationContextKey = "correlation_id"
...
func Middleware() gin.HandlerFunc {
return func(c *gin.Context) {
// creates a correlation_id in the request header if not present
correlationID := c.Request.Header.Get(correlationHeader)
if correlationID == "" {
correlationID = xid.New().String()
c.Request.Header.Add(CorrelationHeader, correlationID)
}
// add correlationID to the logger context
l.logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(correlationContextKey, correlationID)
})
}
}
if I want to get the correlation ID from the request, I can do something like c.Request.Header.Get(CorrelationHeader) (c being *gin.Context).
But if I don't have access to c.Request but just the logger instance, how can I access the correlation ID value from the logger context?
package log
var l *logger
type logger struct {
logger zerolog.Logger
}
func Error(err error, msg string) {
l.logger.Error().
Err(err).
Str(correlationContextKey, l.logger.XXXXXXXXXX ). // ??????
Msg(msg)
}
Hi,
I'm not really confortable with context, but here's the thing... I've created a middleware that is used by a gin server and that sets a correlation ID in the logger context.
if I want to get the correlation ID from the request, I can do something like
c.Request.Header.Get(CorrelationHeader)
(c being*gin.Context
).But if I don't have access to
c.Request
but just the logger instance, how can I access the correlation ID value from the logger context?Thank you so much.