gocommon/commonctx/logger.go
Matthieu 'JP' DERASSE b517779842
Some checks failed
continuous-integration/drone/push Build is failing
feat(middleware): Start request_id middleware
2023-01-15 22:39:41 +00:00

31 lines
674 B
Go

package commonctx
import (
"context"
"github.com/sirupsen/logrus"
)
const LoggerKey = "mainLogger"
func AddMainLogger(ctx context.Context, logger *logrus.Entry) context.Context {
return context.WithValue(ctx, LoggerKey, logger)
}
func GetLogger(ctx context.Context) *logrus.Entry {
if log := ctx.Value(LoggerKey); log != nil {
return log.(*logrus.Entry)
}
return nil
}
func GetContextAndLoggerWithFields(ctx context.Context, fields logrus.Fields) (context.Context, *logrus.Entry) {
if log := ctx.Value(LoggerKey); log != nil {
log := log.(*logrus.Entry)
log = log.WithFields(fields)
ctx = AddMainLogger(ctx, log)
return ctx, log
}
return ctx, nil
}