gocommon/commonctx/logger.go
Matthieu 'JP' DERASSE 2259c587e1
Some checks failed
continuous-integration/drone/push Build is failing
feat(middlewares): Add stacktrace in recovery log
2023-01-15 19:42:39 +00:00

31 lines
629 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 GetLoggerWithFields(ctx context.Context, fields logrus.Fields) *logrus.Entry {
if log := ctx.Value(LoggerKey); log != nil {
log := log.(*logrus.Entry)
log = log.WithFields(fields)
AddMainLogger(ctx, log)
return log
}
return nil
}