logging

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: AGPL-3.0 Imports: 10 Imported by: 0

README

logging

Package logging provides structured logging with zerolog for consistent application-wide logging.

logging

import "github.com/agentstation/starmap/pkg/logging"

Package logging provides structured logging for the starmap system using zerolog. It offers high-performance, zero-allocation logging with support for both human-readable console output during development and structured JSON output for production environments.

Example usage:

// Get the default logger
log := logging.Default()
log.Info().Str("provider", "openai").Msg("Fetching models")

// Create a logger with context
ctx := logging.WithLogger(context.Background(), log)
ctxLog := logging.FromContext(ctx)
ctxLog.Debug().Msg("Using logger from context")

// Add structured fields
log.Error().
    Err(err).
    Str("provider_id", "anthropic").
    Int("retry_count", 3).
    Msg("Failed to fetch models")

Index

Variables

var (

    // Nop logger for discarding output.
    Nop = zerolog.Nop()
)

func Configure

func Configure(cfg *Config)

Configure updates the default logger with the given configuration.

func ConfigureFromEnv

func ConfigureFromEnv()

ConfigureFromEnv configures the logger from environment variables.

func Ctx

func Ctx(ctx context.Context) *zerolog.Logger

Ctx returns a logger from the context or the default logger This is a shorter alias for FromContext.

func Debug

func Debug() *zerolog.Event

Debug starts a new debug level log event.

func Default

func Default() *zerolog.Logger

Default returns the default global logger.

func DisableLoggingForTest

func DisableLoggingForTest(t testing.TB)

DisableLoggingForTest disables logging for the duration of a test.

func Err

func Err(err error) *zerolog.Event

Err creates a new error log event with the given error.

func Error

func Error() *zerolog.Event

Error starts a new error level log event.

func Fatal

func Fatal() *zerolog.Event

Fatal starts a new fatal level log event (will exit after logging).

func FromContext

func FromContext(ctx context.Context) *zerolog.Logger

FromContext extracts the logger from context, or returns the default logger.

func Info

func Info() *zerolog.Event

Info starts a new info level log event.

func Level

func Level(level zerolog.Level) zerolog.Logger

Level creates a child logger with the specified log level.

func New

func New(w io.Writer) zerolog.Logger

New creates a new logger with the given writer.

func NewConsole

func NewConsole() zerolog.Logger

NewConsole creates a new console logger for human-readable output.

func NewJSON

func NewJSON(w io.Writer) zerolog.Logger

NewJSON creates a new JSON logger for structured output.

func NewLoggerFromConfig

func NewLoggerFromConfig(cfg *Config) zerolog.Logger

NewLoggerFromConfig creates a new logger from configuration.

func NewNopLogger

func NewNopLogger() *zerolog.Logger

NewNopLogger creates a logger that discards all output (useful for tests).

func Panic

func Panic() *zerolog.Event

Panic starts a new panic level log event (will panic after logging).

func RequestID

func RequestID(ctx context.Context) string

RequestID extracts the request ID from context.

func SetDefault

func SetDefault(logger zerolog.Logger)

SetDefault sets the default global logger.

func Warn

func Warn() *zerolog.Event

Warn starts a new warning level log event.

func With

func With() zerolog.Context

With creates a child logger with additional context fields.

func WithError

func WithError(ctx context.Context, err error) context.Context

WithError adds an error to the context logger.

func WithField

func WithField(ctx context.Context, key string, value any) context.Context

WithField adds a single field to the logger in the context.

func WithFields

func WithFields(ctx context.Context, fields map[string]any) context.Context

WithFields adds structured fields to the logger in the context.

func WithLevel

func WithLevel(level zerolog.Level) *zerolog.Event

WithLevel starts a new log event with the given level.

func WithLogger

func WithLogger(ctx context.Context, logger *zerolog.Logger) context.Context

WithLogger adds a logger to the context.

func WithModel

func WithModel(ctx context.Context, modelID string) context.Context

WithModel adds model context to the logger.

func WithOperation

func WithOperation(ctx context.Context, operation string) context.Context

WithOperation adds operation context to the logger.

func WithProvider

func WithProvider(ctx context.Context, providerID string) context.Context

WithProvider adds provider context to the logger.

func WithRequestID

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

WithRequestID adds a request ID to the context for tracing.

func WithSource

func WithSource(ctx context.Context, source string) context.Context

WithSource adds source context to the logger.

type Config

Config holds logger configuration options.

type Config struct {
    // Level is the minimum log level to output
    Level string

    // Format is the output format (json, console, pretty)
    Format string

    // Output is where to write logs (stderr, stdout, or file path)
    Output string

    // TimeFormat for timestamps (kitchen, rfc3339, unix, etc.)
    TimeFormat string

    // NoColor disables color output in console mode
    NoColor bool

    // AddCaller includes file:line in log output
    AddCaller bool

    // Fields are default fields to include in all logs
    Fields map[string]any
}

func DefaultConfig
func DefaultConfig() *Config

DefaultConfig returns a configuration with sensible defaults.

type TestLogger

TestLogger creates a test logger that captures output.

type TestLogger struct {
    *zerolog.Logger
    Buffer *bytes.Buffer
}

func CaptureLoggingForTest
func CaptureLoggingForTest(t testing.TB) *TestLogger

CaptureLoggingForTest captures logging output for the duration of a test.

func NewTestLogger
func NewTestLogger(t testing.TB) *TestLogger

NewTestLogger creates a new test logger that captures output.

func (*TestLogger) AssertContains
func (tl *TestLogger) AssertContains(t testing.TB, substr string)

AssertContains asserts that the log contains the given string.

func (*TestLogger) AssertCount
func (tl *TestLogger) AssertCount(t testing.TB, expected int)

AssertCount asserts that the log has the expected number of entries.

func (*TestLogger) AssertNotContains
func (tl *TestLogger) AssertNotContains(t testing.TB, substr string)

AssertNotContains asserts that the log does not contain the given string.

func (*TestLogger) Clear
func (tl *TestLogger) Clear()

Clear clears the captured log output.

func (*TestLogger) Contains
func (tl *TestLogger) Contains(substr string) bool

Contains checks if the log output contains the given string.

func (*TestLogger) ContainsAll
func (tl *TestLogger) ContainsAll(substrs ...string) bool

ContainsAll checks if the log output contains all given strings.

func (*TestLogger) ContainsAny
func (tl *TestLogger) ContainsAny(substrs ...string) bool

ContainsAny checks if the log output contains any of the given strings.

func (*TestLogger) Count
func (tl *TestLogger) Count() int

Count returns the number of log entries.

func (*TestLogger) Lines
func (tl *TestLogger) Lines() []string

Lines returns the captured log output as individual lines.

func (*TestLogger) Output
func (tl *TestLogger) Output() string

Output returns the captured log output as a string.

Generated by gomarkdoc

Documentation

Overview

Package logging provides structured logging for the starmap system using zerolog. It offers high-performance, zero-allocation logging with support for both human-readable console output during development and structured JSON output for production environments.

Example usage:

// Get the default logger
log := logging.Default()
log.Info().Str("provider", "openai").Msg("Fetching models")

// Create a logger with context
ctx := logging.WithLogger(context.Background(), log)
ctxLog := logging.FromContext(ctx)
ctxLog.Debug().Msg("Using logger from context")

// Add structured fields
log.Error().
    Err(err).
    Str("provider_id", "anthropic").
    Int("retry_count", 3).
    Msg("Failed to fetch models")

Index

Constants

This section is empty.

Variables

View Source
var (

	// Nop logger for discarding output.
	Nop = zerolog.Nop()
)

Functions

func Configure

func Configure(cfg *Config)

Configure updates the default logger with the given configuration.

func ConfigureFromEnv

func ConfigureFromEnv()

ConfigureFromEnv configures the logger from environment variables.

func Ctx

func Ctx(ctx context.Context) *zerolog.Logger

Ctx returns a logger from the context or the default logger This is a shorter alias for FromContext.

func Debug

func Debug() *zerolog.Event

Debug starts a new debug level log event.

func Default

func Default() *zerolog.Logger

Default returns the default global logger.

func DisableLoggingForTest

func DisableLoggingForTest(t testing.TB)

DisableLoggingForTest disables logging for the duration of a test.

func Err

func Err(err error) *zerolog.Event

Err creates a new error log event with the given error.

func Error

func Error() *zerolog.Event

Error starts a new error level log event.

func Fatal

func Fatal() *zerolog.Event

Fatal starts a new fatal level log event (will exit after logging).

func FromContext

func FromContext(ctx context.Context) *zerolog.Logger

FromContext extracts the logger from context, or returns the default logger.

func Info

func Info() *zerolog.Event

Info starts a new info level log event.

func Level

func Level(level zerolog.Level) zerolog.Logger

Level creates a child logger with the specified log level.

func New

func New(w io.Writer) zerolog.Logger

New creates a new logger with the given writer.

func NewConsole

func NewConsole() zerolog.Logger

NewConsole creates a new console logger for human-readable output.

func NewJSON

func NewJSON(w io.Writer) zerolog.Logger

NewJSON creates a new JSON logger for structured output.

func NewLoggerFromConfig

func NewLoggerFromConfig(cfg *Config) zerolog.Logger

NewLoggerFromConfig creates a new logger from configuration.

func NewNopLogger

func NewNopLogger() *zerolog.Logger

NewNopLogger creates a logger that discards all output (useful for tests).

func Panic

func Panic() *zerolog.Event

Panic starts a new panic level log event (will panic after logging).

func RequestID

func RequestID(ctx context.Context) string

RequestID extracts the request ID from context.

func SetDefault

func SetDefault(logger zerolog.Logger)

SetDefault sets the default global logger.

func Warn

func Warn() *zerolog.Event

Warn starts a new warning level log event.

func With

func With() zerolog.Context

With creates a child logger with additional context fields.

func WithError

func WithError(ctx context.Context, err error) context.Context

WithError adds an error to the context logger.

func WithField

func WithField(ctx context.Context, key string, value any) context.Context

WithField adds a single field to the logger in the context.

func WithFields

func WithFields(ctx context.Context, fields map[string]any) context.Context

WithFields adds structured fields to the logger in the context.

func WithLevel

func WithLevel(level zerolog.Level) *zerolog.Event

WithLevel starts a new log event with the given level.

func WithLogger

func WithLogger(ctx context.Context, logger *zerolog.Logger) context.Context

WithLogger adds a logger to the context.

func WithModel

func WithModel(ctx context.Context, modelID string) context.Context

WithModel adds model context to the logger.

func WithOperation

func WithOperation(ctx context.Context, operation string) context.Context

WithOperation adds operation context to the logger.

func WithProvider

func WithProvider(ctx context.Context, providerID string) context.Context

WithProvider adds provider context to the logger.

func WithRequestID

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

WithRequestID adds a request ID to the context for tracing.

func WithSource

func WithSource(ctx context.Context, source string) context.Context

WithSource adds source context to the logger.

Types

type Config

type Config struct {
	// Level is the minimum log level to output
	Level string

	// Format is the output format (json, console, pretty)
	Format string

	// Output is where to write logs (stderr, stdout, or file path)
	Output string

	// TimeFormat for timestamps (kitchen, rfc3339, unix, etc.)
	TimeFormat string

	// NoColor disables color output in console mode
	NoColor bool

	// AddCaller includes file:line in log output
	AddCaller bool

	// Fields are default fields to include in all logs
	Fields map[string]any
}

Config holds logger configuration options.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a configuration with sensible defaults.

type TestLogger

type TestLogger struct {
	*zerolog.Logger
	Buffer *bytes.Buffer
}

TestLogger creates a test logger that captures output.

func CaptureLoggingForTest

func CaptureLoggingForTest(t testing.TB) *TestLogger

CaptureLoggingForTest captures logging output for the duration of a test.

func NewTestLogger

func NewTestLogger(t testing.TB) *TestLogger

NewTestLogger creates a new test logger that captures output.

func (*TestLogger) AssertContains

func (tl *TestLogger) AssertContains(t testing.TB, substr string)

AssertContains asserts that the log contains the given string.

func (*TestLogger) AssertCount

func (tl *TestLogger) AssertCount(t testing.TB, expected int)

AssertCount asserts that the log has the expected number of entries.

func (*TestLogger) AssertNotContains

func (tl *TestLogger) AssertNotContains(t testing.TB, substr string)

AssertNotContains asserts that the log does not contain the given string.

func (*TestLogger) Clear

func (tl *TestLogger) Clear()

Clear clears the captured log output.

func (*TestLogger) Contains

func (tl *TestLogger) Contains(substr string) bool

Contains checks if the log output contains the given string.

func (*TestLogger) ContainsAll

func (tl *TestLogger) ContainsAll(substrs ...string) bool

ContainsAll checks if the log output contains all given strings.

func (*TestLogger) ContainsAny

func (tl *TestLogger) ContainsAny(substrs ...string) bool

ContainsAny checks if the log output contains any of the given strings.

func (*TestLogger) Count

func (tl *TestLogger) Count() int

Count returns the number of log entries.

func (*TestLogger) Lines

func (tl *TestLogger) Lines() []string

Lines returns the captured log output as individual lines.

func (*TestLogger) Output

func (tl *TestLogger) Output() string

Output returns the captured log output as a string.

Jump to

Keyboard shortcuts

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