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