logging

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package logging provides structured JSON-line logging with correlation and tenant identifiers carried through context. It is intended for the ChronosOS control plane: the HTTP middleware stamps each request with a correlation id and execution code logs through the same context so all lines for one request can be joined by correlation_id.

Index

Constants

View Source
const (
	HeaderCorrelationID = "X-Correlation-ID"
	HeaderRequestID     = "X-Request-ID"
	HeaderTenant        = "X-Tenant-ID"
)

Header names used to propagate correlation and tenant identifiers across service boundaries.

Variables

This section is empty.

Functions

func CorrelationIDFromContext

func CorrelationIDFromContext(ctx context.Context) string

CorrelationIDFromContext returns the correlation id, or "" if none is set.

func Middleware

func Middleware(logger *Logger) func(http.Handler) http.Handler

Middleware returns an http middleware that ensures every request carries a correlation id (reused from the X-Correlation-ID / X-Request-ID header when present, otherwise generated), stamps it on the request context and the response header, propagates any tenant header into context, and logs the completed request as a structured line.

The integrator can attach this to the ChronosOS server; downstream handlers and execution code then log through the same context via CorrelationIDFromContext / logger methods so all lines for a request share a correlation_id.

func NewCorrelationID

func NewCorrelationID() string

NewCorrelationID generates a random hex correlation id.

func TenantFromContext

func TenantFromContext(ctx context.Context) string

TenantFromContext returns the tenant id, or "" if none is set.

func WithCorrelationID

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

WithCorrelationID returns a context carrying the given correlation id.

func WithTenant

func WithTenant(ctx context.Context, tenant string) context.Context

WithTenant returns a context carrying the given tenant id. It delegates to engine/hooks so the tenant set by the HTTP middleware and the tenant read by the metrics PrometheusHook share one context key (single source of truth for per-tenant attribution across the os and engine layers).

Types

type Level

type Level string

Level is a log severity.

const (
	LevelDebug Level = "debug"
	LevelInfo  Level = "info"
	LevelWarn  Level = "warn"
	LevelError Level = "error"
)

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger emits structured JSON log lines to an io.Writer. It is safe for concurrent use.

func New

func New(w io.Writer, opts ...Option) *Logger

New creates a Logger writing JSON lines to w.

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, fields ...any)

Debug logs at debug level.

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string, fields ...any)

Error logs at error level.

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, fields ...any)

Info logs at info level.

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, lvl Level, msg string, fields ...any)

Log emits a single structured line at the given level. fields are optional key/value pairs; an odd trailing key is recorded under "_MISSING_VALUE".

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, fields ...any)

Warn logs at warn level.

type Option

type Option func(*Logger)

Option configures a Logger.

func WithField

func WithField(k string, v any) Option

WithField attaches a static field to every emitted line.

func WithMinLevel

func WithMinLevel(l Level) Option

WithMinLevel sets the minimum level that will be emitted.

Jump to

Keyboard shortcuts

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