logger

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logger provides logging implementations for the chroma-go library. This file implements the Logger interface using the standard library's slog package.

Panic Prevention: This implementation follows the codebase panic prevention guidelines: - No use of Must* functions that could panic - All type conversions are safe and explicit - No risky operations like unchecked array access or nil pointer dereferences - Type switches include default cases to handle unexpected types safely

As a library component, this code is designed to never panic in production use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field represents a key-value pair for structured logging

func Any

func Any(key string, value interface{}) Field

Any creates a field with any value

func Bool

func Bool(key string, value bool) Field

Bool creates a bool field

func ErrorField

func ErrorField(key string, err error) Field

ErrorField creates an error field. If key is empty, the field will use "_error" as the default key when logged.

func Int

func Int(key string, value int) Field

Int creates an int field

func String

func String(key string, value string) Field

String creates a string field

type Logger

type Logger interface {
	// Standard logging methods
	Debug(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)

	// Context-aware logging methods
	DebugWithContext(ctx context.Context, msg string, fields ...Field)
	InfoWithContext(ctx context.Context, msg string, fields ...Field)
	WarnWithContext(ctx context.Context, msg string, fields ...Field)
	ErrorWithContext(ctx context.Context, msg string, fields ...Field)

	// With returns a new logger with the given fields
	With(fields ...Field) Logger

	// Enabled returns true if the given level is enabled
	IsDebugEnabled() bool

	// Sync flushes any buffered log entries. Should be called before program exit.
	Sync() error
}

Logger is the interface that wraps basic logging methods. It provides a common interface for different logging implementations.

type NoopLogger

type NoopLogger struct{}

NoopLogger is a logger that doesn't log anything

func NewNoopLogger

func NewNoopLogger() *NoopLogger

NewNoopLogger creates a new NoopLogger

func (*NoopLogger) Debug

func (n *NoopLogger) Debug(msg string, fields ...Field)

Debug does nothing

func (*NoopLogger) DebugWithContext

func (n *NoopLogger) DebugWithContext(ctx context.Context, msg string, fields ...Field)

DebugWithContext does nothing

func (*NoopLogger) Error

func (n *NoopLogger) Error(msg string, fields ...Field)

Error does nothing

func (*NoopLogger) ErrorWithContext

func (n *NoopLogger) ErrorWithContext(ctx context.Context, msg string, fields ...Field)

ErrorWithContext does nothing

func (*NoopLogger) Info

func (n *NoopLogger) Info(msg string, fields ...Field)

Info does nothing

func (*NoopLogger) InfoWithContext

func (n *NoopLogger) InfoWithContext(ctx context.Context, msg string, fields ...Field)

InfoWithContext does nothing

func (*NoopLogger) IsDebugEnabled

func (n *NoopLogger) IsDebugEnabled() bool

IsDebugEnabled always returns false

func (*NoopLogger) Sync

func (n *NoopLogger) Sync() error

Sync does nothing for NoopLogger

func (*NoopLogger) Warn

func (n *NoopLogger) Warn(msg string, fields ...Field)

Warn does nothing

func (*NoopLogger) WarnWithContext

func (n *NoopLogger) WarnWithContext(ctx context.Context, msg string, fields ...Field)

WarnWithContext does nothing

func (*NoopLogger) With

func (n *NoopLogger) With(fields ...Field) Logger

With returns the same NoopLogger

type SlogLogger

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

SlogLogger is a Logger implementation using the standard library's slog package

func NewDefaultSlogLogger

func NewDefaultSlogLogger() (*SlogLogger, error)

NewDefaultSlogLogger creates a new SlogLogger with JSON handler and production configuration

func NewInfoSlogLogger added in v0.3.3

func NewInfoSlogLogger() (*SlogLogger, error)

NewInfoSlogLogger creates a new SlogLogger with info level (no debug output)

func NewSlogLogger

func NewSlogLogger(logger *slog.Logger) *SlogLogger

NewSlogLogger creates a new SlogLogger with the provided slog.Logger

func NewSlogLoggerWithHandler

func NewSlogLoggerWithHandler(handler slog.Handler) *SlogLogger

NewSlogLoggerWithHandler creates a new SlogLogger with the provided handler

func NewTextSlogLogger

func NewTextSlogLogger() (*SlogLogger, error)

NewTextSlogLogger creates a new SlogLogger with text handler for human-readable output

func (*SlogLogger) Debug

func (s *SlogLogger) Debug(msg string, fields ...Field)

Debug logs a message at debug level

func (*SlogLogger) DebugWithContext

func (s *SlogLogger) DebugWithContext(ctx context.Context, msg string, fields ...Field)

DebugWithContext logs a message at debug level with context

func (*SlogLogger) Error

func (s *SlogLogger) Error(msg string, fields ...Field)

Error logs a message at error level

func (*SlogLogger) ErrorWithContext

func (s *SlogLogger) ErrorWithContext(ctx context.Context, msg string, fields ...Field)

ErrorWithContext logs a message at error level with context

func (*SlogLogger) Info

func (s *SlogLogger) Info(msg string, fields ...Field)

Info logs a message at info level

func (*SlogLogger) InfoWithContext

func (s *SlogLogger) InfoWithContext(ctx context.Context, msg string, fields ...Field)

InfoWithContext logs a message at info level with context

func (*SlogLogger) IsDebugEnabled

func (s *SlogLogger) IsDebugEnabled() bool

IsDebugEnabled returns true if debug level is enabled

func (*SlogLogger) Sync

func (s *SlogLogger) Sync() error

Sync flushes any buffered log entries slog doesn't require explicit sync, but we implement it for interface compatibility

func (*SlogLogger) Warn

func (s *SlogLogger) Warn(msg string, fields ...Field)

Warn logs a message at warn level

func (*SlogLogger) WarnWithContext

func (s *SlogLogger) WarnWithContext(ctx context.Context, msg string, fields ...Field)

WarnWithContext logs a message at warn level with context

func (*SlogLogger) With

func (s *SlogLogger) With(fields ...Field) Logger

With returns a new logger with the given fields

type ZapLogger

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

ZapLogger is a Logger implementation using uber-go/zap

func NewDefaultZapLogger

func NewDefaultZapLogger() (*ZapLogger, error)

NewDefaultZapLogger creates a new ZapLogger with default configuration

func NewDevelopmentZapLogger

func NewDevelopmentZapLogger() (*ZapLogger, error)

NewDevelopmentZapLogger creates a new ZapLogger with development configuration

func NewZapLogger

func NewZapLogger(logger *zap.Logger) *ZapLogger

NewZapLogger creates a new ZapLogger with the provided zap.Logger

func (*ZapLogger) Debug

func (z *ZapLogger) Debug(msg string, fields ...Field)

Debug logs a message at debug level

func (*ZapLogger) DebugWithContext

func (z *ZapLogger) DebugWithContext(ctx context.Context, msg string, fields ...Field)

DebugWithContext logs a message at debug level with context

func (*ZapLogger) Error

func (z *ZapLogger) Error(msg string, fields ...Field)

Error logs a message at error level

func (*ZapLogger) ErrorWithContext

func (z *ZapLogger) ErrorWithContext(ctx context.Context, msg string, fields ...Field)

ErrorWithContext logs a message at error level with context

func (*ZapLogger) Info

func (z *ZapLogger) Info(msg string, fields ...Field)

Info logs a message at info level

func (*ZapLogger) InfoWithContext

func (z *ZapLogger) InfoWithContext(ctx context.Context, msg string, fields ...Field)

InfoWithContext logs a message at info level with context

func (*ZapLogger) IsDebugEnabled

func (z *ZapLogger) IsDebugEnabled() bool

IsDebugEnabled returns true if debug level is enabled

func (*ZapLogger) Sync

func (z *ZapLogger) Sync() error

Sync flushes any buffered log entries

func (*ZapLogger) Warn

func (z *ZapLogger) Warn(msg string, fields ...Field)

Warn logs a message at warn level

func (*ZapLogger) WarnWithContext

func (z *ZapLogger) WarnWithContext(ctx context.Context, msg string, fields ...Field)

WarnWithContext logs a message at warn level with context

func (*ZapLogger) With

func (z *ZapLogger) With(fields ...Field) Logger

With returns a new logger with the given fields

Jump to

Keyboard shortcuts

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