Documentation
¶
Overview ¶
Package log provides a unified logging interface with traceId support.
All services should use this package instead of slog/zerolog directly. The underlying implementation can be swapped without changing call sites.
Usage:
log.Info("server started", "port", 8080)
log.InfoContext(ctx, "created env", "env_id", 123) // auto injects trace_id
log.With("service", "mf-user").Info("ready")
Index ¶
- Variables
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func GetTraceID(ctx context.Context) string
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func Init(mode, serviceName string)
- func SetLogger(l Logger)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type Logger
Constants ¶
This section is empty.
Variables ¶
View Source
var TraceIDKey = ctxKey{}
TraceIDKey is the context key for trace ID.
Functions ¶
func GetTraceID ¶
GetTraceID extracts the trace ID from context. Returns "" if not set.
func Init ¶
func Init(mode, serviceName string)
Init initializes the global logger. Call once at startup.
log.Init("debug", "mf-user") // mode: "debug" (text) or "release" (json)
Types ¶
type Logger ¶
type Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
// Context-aware: automatically extracts trace_id from ctx
DebugContext(ctx context.Context, msg string, args ...any)
InfoContext(ctx context.Context, msg string, args ...any)
WarnContext(ctx context.Context, msg string, args ...any)
ErrorContext(ctx context.Context, msg string, args ...any)
// With returns a child logger with additional key-value pairs
With(args ...any) Logger
}
Logger is the unified logging interface. All services depend on this, never on a specific logging library directly.
Click to show internal directories.
Click to hide internal directories.