Files
ctrld/log.go
Cuong Manh Le fc527dbdfb all: eliminate usage of global ProxyLogger
So setting up logging for ctrld binary and ctrld packages could be done
more easily, decouple the required setup for interactive vs daemon
running.

This is the first step toward replacing rs/zerolog libary with a
different logging library.
2025-10-09 17:45:59 +07:00

53 lines
1.3 KiB
Go

package ctrld
import (
"context"
"fmt"
"github.com/rs/zerolog"
)
// LoggerCtxKey is the context.Context key for a logger.
type LoggerCtxKey struct{}
// LoggerCtx returns a context.Context with LoggerCtxKey set.
func LoggerCtx(ctx context.Context, l *Logger) context.Context {
return context.WithValue(ctx, LoggerCtxKey{}, l)
}
// A Logger provides fast, leveled, structured logging.
type Logger struct {
*zerolog.Logger
}
var noOpZeroLogger = zerolog.Nop()
// NopLogger returns a logger which all operation are no-op.
var NopLogger = &Logger{&noOpZeroLogger}
// LoggerFromCtx returns the logger associated with given ctx.
//
// If there's no logger, a no-op logger will be returned.
func LoggerFromCtx(ctx context.Context) *Logger {
if logger, ok := ctx.Value(LoggerCtxKey{}).(*Logger); ok && logger != nil {
return logger
}
return NopLogger
}
// ReqIdCtxKey is the context.Context key for a request id.
type ReqIdCtxKey struct{}
// Log emits the logs for a particular zerolog event.
// The request id associated with the context will be included if presents.
func Log(ctx context.Context, e *zerolog.Event, format string, v ...any) {
id, ok := ctx.Value(ReqIdCtxKey{}).(string)
if !ok {
e.Msgf(format, v...)
return
}
e.MsgFunc(func() string {
return fmt.Sprintf("[%s] %s", id, fmt.Sprintf(format, v...))
})
}