grpc/logger/context.go
Adphi 3a3d77169c
interceptors: migrate to otel and add logging interceptor
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2024-10-17 17:15:05 +02:00

47 lines
713 B
Go

package logger
import (
"context"
"sync"
"github.com/sirupsen/logrus"
)
var (
defaultLogger Logger
mu sync.RWMutex
)
type log struct{}
func init() {
defaultLogger = &logger{fl: logrus.New()}
}
var C = From
func SetDefault(logger Logger) {
mu.Lock()
defer mu.Unlock()
defaultLogger = logger
}
func Set(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, log{}, logger)
}
func From(ctx context.Context) Logger {
log, ok := ctx.Value(log{}).(Logger)
if ok {
return log.WithContext(ctx)
}
if defaultLogger != nil {
return defaultLogger
}
logr := New()
mu.Lock()
defer mu.Unlock()
defaultLogger = logr
return defaultLogger.WithContext(ctx)
}