log

package
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 6 Imported by: 0

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

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

func WithRequestID(ctx context.Context, id string) context.Context

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

type Field struct {
	Key   string
	Value any
}

Field is a typed key/value pair. Use the constructors below; building a literal works but skips the typing benefit.

func Any

func Any(k string, v any) Field

func Bool

func Bool(k string, v bool) Field

func Duration

func Duration(k string, v time.Duration) Field

func Err

func Err(err error) Field

func Float64

func Float64(k string, v float64) Field

func Group

func Group(k string, fs ...Field) Field

func Int

func Int(k string, v int) Field

func Int64

func Int64(k string, v int64) Field

func String

func String(k, v string) Field

String / Int / Int64 / Float64 / Bool / Time / Duration / Err / Any / Group are the canonical Field constructors documented in the README.

func Time

func Time(k string, v time.Time) Field

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.

const (
	LevelDebug Level = -4
	LevelInfo  Level = 0
	LevelWarn  Level = 4
	LevelError Level = 8
)

Level constants. The numeric gaps mirror slog (-4/0/4/8) so adapter authors can copy the conversion table verbatim.

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.

func NewZap

func NewZap(z *zap.Logger) Logger

NewZap wraps an existing `*zap.Logger` so projects that already configured zap can reuse it.

Jump to

Keyboard shortcuts

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