31 lines
629 B
Go
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
|
|
}
|