Documentation
¶
Index ¶
- func CorrelationIDFromContext(ctx context.Context) (context.Context, string)
- func Critical(ctx context.Context, fnName, errorPath, msg string, opts ...Option)
- func Error(ctx context.Context, fnName, errorPath, msg string, opts ...Option)
- func Info(ctx context.Context, fnName string, msg string, opts ...Option)
- func InitConfig(cfg Config)
- func Logger() zerolog.Logger
- func SpanIDFromContext(ctx context.Context) (context.Context, string)
- func TraceIDFromContext(ctx context.Context) (context.Context, string)
- func Warn(ctx context.Context, fnName, errorPath, msg string, opts ...Option)
- func WithCorrelationID(ctx context.Context, id string) context.Context
- func WithSpanID(ctx context.Context, spanID string) context.Context
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type Config
- type Event
- type Exception
- type Option
- func WithAction(action string) Option
- func WithActorIP(ip string) Option
- func WithCorrelationIDOption(id string) Option
- func WithDurationMs(ms int64) Option
- func WithEnvironment(env string) Option
- func WithEvent(evName string) Option
- func WithException(err error) Option
- func WithMetadata(metadata map[string]any) Option
- func WithOutcome(outcome string) Option
- func WithParentID(id string) Option
- func WithPublishSubject(subject string) Option
- func WithReceivedPayload(payload []byte) Option
- func WithResponsePayload(payload []byte) Option
- func WithRetryCount(count int) Option
- func WithSubscribeSubject(subject string) Option
- func WithTraceFromContext(ctx context.Context) Option
- func WithUserID(id string) Option
- func WithUserRole(t string) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CorrelationIDFromContext ¶ added in v0.1.0
CorrelationIDFromContext extracts the correlation ID from the context. Returns "" if not found.
func Critical ¶
Critical logs a critical event. REQUIRED FIELD: fnName and errorPath must both be provided and non-empty.
func Error ¶
Error logs an error event. REQUIRED FIELD: fnName and errorPath must both be provided and non-empty.
func InitConfig ¶
func InitConfig(cfg Config)
InitConfig initializes global logging configuration. This must be called once during application startup before using the logging package.
func SpanIDFromContext ¶ added in v0.0.9
SpanIDFromContext extracts the span ID from the context. Returns "" if not found.
func TraceIDFromContext ¶
TraceIDFromContext extracts the trace ID from the context. Returns "" if not found.
func Warn ¶
Warn logs a warning event. REQUIRED FIELD: fnName and errorPath must both be provided and non-empty.
func WithCorrelationID ¶ added in v0.1.0
WithCorrelationID attaches a business transaction correlation ID to the context.
func WithSpanID ¶ added in v0.0.9
WithSpanID attaches the given span ID to the context.
Types ¶
type Config ¶
type Config struct {
// Service is the logical service name for this application.
// REQUIRED FIELD: Must be provided and non-empty.
Service string
// Env represents the deployment environment (e.g. "development", "staging", "production").
// CONFIGURABLE
Env string
// RedactKeys are case-insensitive field names that should be redacted from metadata/properties.
// Example: ["password", "token", "authorization", "ssn"].
// CONFIGURABLE
RedactKeys []string
// LogDir is the directory where log files will be written.
// Only used when EnableFile is true.
// CONFIGURABLE
LogDir string
// EnableFile writes logs to a file at LogDir/app.log.
// Disabled by default; suitable for containerised deployments where stderr is collected.
EnableFile bool
// ConsoleJSON determines whether console output should be JSON (true)
// or a human-friendly pretty format (false).
// CONFIGURABLE
ConsoleJSON bool
// EnableSeq toggles sending logs to Seq over HTTP.
// Can also be enabled by setting SEQ_ENABLE=true in the environment.
// CONFIGURABLE
EnableSeq bool
// SeqURL is the GELF UDP address for Seq (e.g. "localhost:12201").
// Requires a GELF UDP input to be enabled in Seq.
// Can also be set via the SEQ_URL environment variable.
// CONFIGURABLE
SeqURL string
// SeqAPIKey is unused with GELF UDP transport and kept for backwards compatibility.
// Can also be set via the SEQ_API_KEY environment variable.
// CONFIGURABLE
SeqAPIKey string
// TimeFormat is the layout used for timestamps when pretty-printing.
// CONFIGURABLE
TimeFormat string
}
Config holds all logging configuration. CONFIGURABLE: All fields in this struct are intended to be set from application configuration.
func ConfigOrDefault ¶
func ConfigOrDefault() Config
ConfigOrDefault returns the current globalConfig. It panics if InitConfig has not been called.
type Event ¶
type Event struct {
// Required / baseline fields
Service string `json:"service"`
Environment string `json:"environment,omitempty"`
TraceID string `json:"trace_id"`
SpanID string `json:"span_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
// event is the canonical field name for business/saga event names.
Event string `json:"event,omitempty"`
// Location/context fields
Function string `json:"function,omitempty"`
ErrorPath string `json:"error_path,omitempty"`
RetryCount int `json:"retry_count,omitempty"`
DurationMs int64 `json:"duration_ms,omitempty"`
SubscribeSubject string `json:"subscribe_subject,omitempty"`
PublishSubject string `json:"publish_subject,omitempty"`
ReceivedPayload any `json:"received_payload,omitempty"`
ResponsePayload any `json:"response_payload,omitempty"`
// Who — actor identity (flat for direct filterability)
UserID string `json:"user_id,omitempty"` // user ID performing the action
UserRole string `json:"user_role,omitempty"` // "user" | "service" | "scheduler" | "system"
ActorIP string `json:"actor_ip,omitempty"` // originating IP
// What — action on a resource (flat for direct filterability)
Action string `json:"action,omitempty"` // "create" | "update" | "delete" | "read"
Outcome string `json:"outcome,omitempty"` // "success" | "failure" | "partial"
// Correlation — ties events across services/traces for one business transaction
CorrelationID string `json:"correlation_id,omitempty"`
// Additional structured context
Metadata map[string]any `json:"metadata,omitempty"`
Exception *Exception `json:"exception,omitempty"`
}
Event represents a structured log event with required and optional fields.
Note: This struct is primarily used as an internal carrier for field values. The logger writes fields using zerolog; not all struct fields are serialized directly.
type Exception ¶
type Exception struct {
Type string `json:"type,omitempty"`
Message string `json:"message,omitempty"`
Stack string `json:"stack,omitempty"`
}
Exception represents a structured error payload for logs.
type Option ¶
type Option func(*Event)
Option allows adding optional metadata to an Event. EXTENSION POINT: Add more option helpers for additional fields.
func WithAction ¶ added in v0.1.0
WithAction sets the action performed ("create" | "update" | "delete" | "read").
func WithActorIP ¶ added in v0.1.0
WithActorIP sets the originating IP address of the actor.
func WithCorrelationIDOption ¶ added in v0.1.0
WithCorrelationIDOption sets the correlation ID as an option, overriding any value from context.
func WithDurationMs ¶
WithDurationMs records the operation duration in milliseconds.
func WithEnvironment ¶
WithEnvironment sets the deployment environment, overriding the config default.
func WithException ¶
WithException populates the structured exception field. If err is nil, this option does nothing.
func WithMetadata ¶
WithMetadata attaches arbitrary business-context key/value pairs; sensitive keys are redacted.
func WithOutcome ¶ added in v0.1.0
WithOutcome sets the outcome of the action ("success" | "failure" | "partial").
func WithParentID ¶ added in v0.0.9
WithParentID sets the parent span ID for distributed tracing context.
func WithPublishSubject ¶ added in v0.0.4
WithPublishSubject sets the NATS subject the service is publishing this event to.
func WithReceivedPayload ¶ added in v0.0.6
WithReceivedPayload attaches the raw NATS inbound message body to the log entry. If payload is valid JSON it is stored as a structured value; otherwise as a string.
func WithResponsePayload ¶ added in v0.0.6
WithResponsePayload attaches the outbound NATS response body to the log entry. If payload is valid JSON it is stored as a structured value; otherwise as a string.
func WithRetryCount ¶
WithRetryCount records the number of retry attempts made.
func WithSubscribeSubject ¶ added in v0.0.4
WithSubscribeSubject sets the NATS subject the service consumed this event from.
func WithTraceFromContext ¶
WithTraceFromContext is a no-op unless built with -tags=otel.
func WithUserID ¶ added in v0.1.1
WithUserID sets the user ID performing the action.
func WithUserRole ¶ added in v0.1.1
WithUserRole sets the user role ("user" | "service" | "scheduler" | "system").