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 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 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.
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.
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.