Documentation
¶
Index ¶
- Constants
- func Debug(msg string, args ...any)
- func Default() *slog.Logger
- func Error(msg string, args ...any)
- func Info(msg string, args ...any)
- func Init(cfg *Config) error
- func InitFromEnv(env string) error
- func New(cfg *Config) (*slog.Logger, error)
- func NewMockLogger() *slog.Logger
- func SetDefault(logger *slog.Logger)
- func Warn(msg string, args ...any)
- func With(key string, value any) *slog.Logger
- type ColoredHandler
- type Config
- type LegacyAdapter
- type LegacyLogger
- type LogEntry
- type MockLogger
- func (l *MockLogger) Clear()
- func (*MockLogger) Enabled(_ context.Context, _ slog.Level) bool
- func (l *MockLogger) GetLogs() []LogEntry
- func (l *MockLogger) GetLogsByLevel(level slog.Level) []LogEntry
- func (l *MockLogger) Handle(_ context.Context, r slog.Record) error
- func (l *MockLogger) String() string
- func (l *MockLogger) WithAttrs(attrs []slog.Attr) slog.Handler
- func (l *MockLogger) WithGroup(name string) slog.Handler
Constants ¶
const ( FormatJSON = "json" FormatConsole = "console" )
Format constants
Variables ¶
This section is empty.
Functions ¶
func Init ¶
Init initializes the logger with the given configuration and sets it as the default logger
func InitFromEnv ¶
InitFromEnv initializes the logger from environment variables
Types ¶
type ColoredHandler ¶
type ColoredHandler struct {
// contains filtered or unexported fields
}
ColoredHandler is a slog.Handler that writes colored logs to an io.Writer.
func NewColoredHandler ¶
func NewColoredHandler(w io.Writer, opts *slog.HandlerOptions, useJSON bool) *ColoredHandler
NewColoredHandler creates a new ColoredHandler that writes to w.
type Config ¶
type Config struct {
// Level is the minimum enabled logging level
Level string `envconfig:"LEVEL" default:"info"`
// Format is the log format ("json" or "console")
Format string `envconfig:"FORMAT" default:"json"`
// EnableCaller adds the file:line caller info to log output
EnableCaller bool `envconfig:"ENABLE_CALLER" default:"true"`
// EnableStacktrace enables automatic stacktrace capturing
EnableStacktrace bool `envconfig:"ENABLE_STACKTRACE" default:"true"`
// EnableColors enables colored output for console format
EnableColors bool `envconfig:"ENABLE_COLORS" default:"false"`
// Environment is the current environment (development, staging, production)
Environment string `envconfig:"ENVIRONMENT" default:"production"`
}
Config holds all the logger-related configuration
type LegacyAdapter ¶
type LegacyAdapter struct {
// contains filtered or unexported fields
}
LegacyAdapter adapts a *slog.Logger to the LegacyLogger interface
func (*LegacyAdapter) Error ¶
func (l *LegacyAdapter) Error(args ...any)
Error logs an error message
func (*LegacyAdapter) With ¶
func (l *LegacyAdapter) With(key string, value any) LegacyLogger
With adds structured context to the logger
type LegacyLogger ¶
type LegacyLogger interface {
// Debug logs a debug message
Debug(args ...any)
// Info logs an info message
Info(args ...any)
// Warn logs a warning message
Warn(args ...any)
// Error logs an error message
Error(args ...any)
// With adds structured context to the logger
With(key string, value any) LegacyLogger
}
LegacyLogger is the old logger interface, kept for backward compatibility
func NewLegacyAdapter ¶
func NewLegacyAdapter(logger *slog.Logger) LegacyLogger
NewLegacyAdapter creates a new LegacyAdapter
type MockLogger ¶
type MockLogger struct {
// contains filtered or unexported fields
}
MockLogger is a mock implementation of slog.Handler for testing
func (*MockLogger) GetLogs ¶
func (l *MockLogger) GetLogs() []LogEntry
GetLogs returns all log entries
func (*MockLogger) GetLogsByLevel ¶
func (l *MockLogger) GetLogsByLevel(level slog.Level) []LogEntry
GetLogsByLevel returns log entries filtered by level
func (*MockLogger) String ¶
func (l *MockLogger) String() string
String returns a string representation of the log entries