mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
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.
53 lines
1.3 KiB
Go
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...))
|
|
})
|
|
}
|