You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
buildx/vendor/github.com/moby/buildkit/util/bklog/log.go

64 lines
1.2 KiB
Go

package bklog
import (
"context"
"github.com/containerd/containerd/log"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
)
func init() {
// overwrites containerd/log
log.G = GetLogger
log.L = L
}
var (
G = GetLogger
L = logrus.NewEntry(logrus.StandardLogger())
)
var (
logWithTraceID = false
)
func EnableLogWithTraceID(b bool) {
logWithTraceID = b
}
type (
loggerKey struct{}
)
// WithLogger returns a new context with the provided logger. Use in
// combination with logger.WithField(s) for great effect.
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
return context.WithValue(ctx, loggerKey{}, logger)
}
// GetLogger retrieves the current logger from the context. If no logger is
// available, the default logger is returned.
func GetLogger(ctx context.Context) (l *logrus.Entry) {
logger := ctx.Value(loggerKey{})
if logger != nil {
l = logger.(*logrus.Entry)
} else if logger := log.GetLogger(ctx); logger != nil {
l = logger
} else {
l = L
}
if logWithTraceID {
if spanContext := trace.SpanFromContext(ctx).SpanContext(); spanContext.IsValid() {
return l.WithFields(logrus.Fields{
"traceID": spanContext.TraceID(),
"spanID": spanContext.SpanID(),
})
}
}
return l
}