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 ¶
- Variables
- func Configure(cfg *Config)
- func ConfigureFromEnv()
- func Ctx(ctx context.Context) *zerolog.Logger
- func Debug() *zerolog.Event
- func Default() *zerolog.Logger
- func DisableLoggingForTest(t testing.TB)
- func Err(err error) *zerolog.Event
- func Error() *zerolog.Event
- func Fatal() *zerolog.Event
- func FromContext(ctx context.Context) *zerolog.Logger
- func Info() *zerolog.Event
- func Level(level zerolog.Level) zerolog.Logger
- func New(w io.Writer) zerolog.Logger
- func NewConsole() zerolog.Logger
- func NewJSON(w io.Writer) zerolog.Logger
- func NewLoggerFromConfig(cfg *Config) zerolog.Logger
- func NewNopLogger() *zerolog.Logger
- func Panic() *zerolog.Event
- func RequestID(ctx context.Context) string
- func SetDefault(logger zerolog.Logger)
- func Warn() *zerolog.Event
- func With() zerolog.Context
- func WithError(ctx context.Context, err error) context.Context
- func WithField(ctx context.Context, key string, value any) context.Context
- func WithFields(ctx context.Context, fields map[string]any) context.Context
- func WithLevel(level zerolog.Level) *zerolog.Event
- func WithLogger(ctx context.Context, logger *zerolog.Logger) context.Context
- func WithModel(ctx context.Context, modelID string) context.Context
- func WithOperation(ctx context.Context, operation string) context.Context
- func WithProvider(ctx context.Context, providerID string) context.Context
- func WithRequestID(ctx context.Context, requestID string) context.Context
- func WithSource(ctx context.Context, source string) context.Context
- type Config
- type TestLogger
- func (tl *TestLogger) AssertContains(t testing.TB, substr string)
- func (tl *TestLogger) AssertCount(t testing.TB, expected int)
- func (tl *TestLogger) AssertNotContains(t testing.TB, substr string)
- func (tl *TestLogger) Clear()
- func (tl *TestLogger) Contains(substr string) bool
- func (tl *TestLogger) ContainsAll(substrs ...string) bool
- func (tl *TestLogger) ContainsAny(substrs ...string) bool
- func (tl *TestLogger) Count() int
- func (tl *TestLogger) Lines() []string
- func (tl *TestLogger) Output() string
Constants ¶
This section is empty.
Variables ¶
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 ¶
Ctx returns a logger from the context or the default logger This is a shorter alias for FromContext.
func DisableLoggingForTest ¶
DisableLoggingForTest disables logging for the duration of a test.
func FromContext ¶
FromContext extracts the logger from context, or returns the default logger.
func NewConsole ¶
NewConsole creates a new console logger for human-readable output.
func NewLoggerFromConfig ¶
NewLoggerFromConfig creates a new logger from configuration.
func NewNopLogger ¶
NewNopLogger creates a logger that discards all output (useful for tests).
func WithFields ¶
WithFields adds structured fields to the logger in the context.
func WithLogger ¶
WithLogger adds a logger to the context.
func WithOperation ¶
WithOperation adds operation context to the logger.
func WithProvider ¶
WithProvider adds provider context to the logger.
func WithRequestID ¶
WithRequestID adds a request ID to the context for tracing.
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 ¶
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) 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.