log

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var TraceIDKey = ctxKey{}

TraceIDKey is the context key for trace ID.

Functions

func Debug

func Debug(msg string, args ...any)

func DebugContext

func DebugContext(ctx context.Context, msg string, args ...any)

func Error

func Error(msg string, args ...any)

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID extracts the trace ID from context. Returns "" if not set.

func Info

func Info(msg string, args ...any)

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

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)

func SetLogger

func SetLogger(l Logger)

SetLogger replaces the global logger with a custom implementation.

func Warn

func Warn(msg string, args ...any)

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID returns a new context carrying the trace ID.

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.

func L

func L() Logger

L returns the global logger instance.

func With

func With(args ...any) Logger

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL