Documentation
¶
Overview ¶
Package log defines the Logger interface used across craftgo together with a default zap adapter. Projects that already standardised on slog or zerolog can satisfy the same interface with their own adapter.
Index ¶
- func SetDefault(l Logger)
- func SetLevel(l Level)
- func WithRequestID(ctx context.Context, id string) context.Context
- type Field
- func Any(k string, v any) Field
- func Bool(k string, v bool) Field
- func Duration(k string, v time.Duration) Field
- func Err(err error) Field
- func Float64(k string, v float64) Field
- func Group(k string, fs ...Field) Field
- func Int(k string, v int) Field
- func Int64(k string, v int64) Field
- func String(k, v string) Field
- func Time(k string, v time.Time) Field
- type Level
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefault ¶
func SetDefault(l Logger)
SetDefault swaps the package-level Logger. Server.SetLogger calls this so codegen-emitted logic files can read the same instance via Default without a constructor parameter or context lookup. Passing nil is a no-op.
func SetLevel ¶ added in v1.4.1
func SetLevel(l Level)
SetLevel retunes the process-wide level for every Logger created by New and NewConsole, including the instance mirrored into Default and the one held by the server. The swap is atomic and takes effect on the next log call without replacing any logger. Loggers wrapped via NewZap manage their own level and ignore this.
func WithRequestID ¶
WithRequestID returns ctx with the supplied request id stashed under the package's canonical key. pkg/server.RequestID calls this so `log.WithContext(ctx)` can pick the value up without taking a hard dependency on pkg/server.
Types ¶
type Field ¶
Field is a typed key/value pair. Use the constructors below; building a literal works but skips the typing benefit.
type Level ¶
type Level int8
Level is a coarse level enum that maps onto zap's atomic level. Values align with the standard slog levels so external adapters translate without surprises.
Level constants. The numeric gaps mirror slog (-4/0/4/8) so adapter authors can copy the conversion table verbatim.
func GetLevel ¶ added in v1.4.1
func GetLevel() Level
GetLevel reports the current process-wide level.
func ParseLevel ¶ added in v1.4.1
ParseLevel maps a config string ("debug" / "info" / "warn" / "error", case- and space-insensitive) onto a Level. The bool reports whether the string was recognised so callers can keep their current level on a blank or misspelled value instead of silently snapping to one.
type Logger ¶
type Logger interface {
Debug(msg string, fields ...Field)
Info(msg string, fields ...Field)
Warn(msg string, fields ...Field)
Error(msg string, fields ...Field)
With(fields ...Field) Logger
WithContext(ctx context.Context) Logger
Enabled(level Level) bool
}
Logger is the structured-logging surface every craftgo middleware depends on. Callers with a request context chain `logger.WithContext(ctx).Info(...)` to fan trace_id / span_id / request_id into the line; callers without one call `Info(...)` directly.
func Default ¶
func Default() Logger
Default returns the current package-level Logger. Generated logic constructors read it (typically chained with `.WithContext(ctx)`) so user code can call `l.Info(...)` directly without juggling context plumbing.
func Discard ¶
func Discard() Logger
Discard returns a Logger that drops every call silently. Useful for tests, batch tools, and any runtime where log output is undesirable. Wire it via `srv.SetLogger(log.Discard())`.
func New ¶
func New() Logger
New returns the default Logger: a production-configured zap logger writing JSON to stderr. Its threshold tracks the process-wide level (info by default); call SetLevel to retune it.
func NewConsole ¶
func NewConsole() Logger
NewConsole returns a development-configured Logger that emits human-readable, colour-tagged lines to stderr. Same Logger contract as New - drop into `srv.SetLogger(log.NewConsole())` for local `go run` sessions and switch back to New for production. It shares the process-wide level with New; call `log.SetLevel(log.LevelDebug)` for verbose local runs.