Documentation
¶
Index ¶
- Constants
- func AddOutputs(writers ...io.Writer)
- func ClearHandler()
- func Close() error
- func CtxDebug(ctx context.Context, format string, args ...any)
- func CtxError(ctx context.Context, format string, args ...any)
- func CtxFatal(ctx context.Context, format string, args ...any)
- func CtxInfo(ctx context.Context, format string, args ...any)
- func CtxPanic(ctx context.Context, format string, args ...any)
- func CtxTrace(ctx context.Context, format string, args ...any)
- func CtxWarn(ctx context.Context, format string, args ...any)
- func Debug(format string, args ...any)
- func Error(format string, args ...any)
- func Fatal(format string, args ...any)
- func Flush()
- func Info(format string, args ...any)
- func NewStructuredLogHandler(level Level) *structuredLogHandler
- func Panic(format string, args ...any)
- func RegisterHandler(handlers ...Handler)
- func SetLevel(level Level)
- func SetOutput(w io.Writer)
- func SetStructuredOutput(out io.Writer, json bool)
- func Trace(format string, args ...any)
- func Warn(format string, args ...any)
- func WithLogID(ctx context.Context, logID string) context.Context
- type ConsoleHandler
- func (h *ConsoleHandler) AddOutputs(writers ...io.Writer)
- func (h *ConsoleHandler) Close() error
- func (h *ConsoleHandler) Flush()
- func (h *ConsoleHandler) Output(level Level, ctx context.Context, format string, args ...any)
- func (h *ConsoleHandler) SetLevel(level Level)
- func (h *ConsoleHandler) SetOutput(w io.Writer)
- func (h *ConsoleHandler) With(args ...any) Handler
- func (h *ConsoleHandler) WithGroup(name string) Handler
- func (h *ConsoleHandler) Write(p []byte) (int, error)
- type FileHandler
- func (h *FileHandler) AddOutputs(writers ...io.Writer)
- func (h *FileHandler) Close() error
- func (h *FileHandler) Flush()
- func (h *FileHandler) GetCurrentFilePath() string
- func (h *FileHandler) Output(level Level, ctx context.Context, format string, args ...any)
- func (h *FileHandler) SetLevel(level Level)
- func (h *FileHandler) SetOutput(w io.Writer)
- func (h *FileHandler) With(args ...any) Handler
- func (h *FileHandler) WithGroup(name string) Handler
- func (h *FileHandler) Write(p []byte) (int, error)
- type FileHandlerConfig
- type Formatter
- type Handler
- type Level
- type Logger
- type MultiError
- type RotationInterval
- type SyncConsoleHandler
- func (h *SyncConsoleHandler) AddOutputs(writers ...io.Writer)
- func (h *SyncConsoleHandler) Close() error
- func (h *SyncConsoleHandler) Flush()
- func (h *SyncConsoleHandler) Output(level Level, ctx context.Context, format string, args ...any)
- func (h *SyncConsoleHandler) SetLevel(level Level)
- func (h *SyncConsoleHandler) SetOutput(w io.Writer)
- func (h *SyncConsoleHandler) With(args ...any) Handler
- func (h *SyncConsoleHandler) WithGroup(name string) Handler
- func (h *SyncConsoleHandler) Write(p []byte) (int, error)
- type TextFormatter
Constants ¶
const LogIDKey contextKey = "log_id"
LogIDKey is the context key for log ID
Variables ¶
This section is empty.
Functions ¶
func AddOutputs ¶
AddOutputs adds multiple output writers to the default logger
func ClearHandler ¶
func ClearHandler()
ClearHandler removes all handlers from the default logger This maintains backward compatibility with the original API
func NewStructuredLogHandler ¶
func NewStructuredLogHandler(level Level) *structuredLogHandler
NewStructuredLogHandler creates a new structured log handler
func RegisterHandler ¶
func RegisterHandler(handlers ...Handler)
RegisterHandler adds handlers to the default logger This maintains backward compatibility with the original API
func SetLevel ¶
func SetLevel(level Level)
SetLevel sets the minimum log level for the default logger
func SetStructuredOutput ¶
SetStructuredOutput configures structured logging output This sets up the underlying slog logger for JSON or text output
Types ¶
type ConsoleHandler ¶
type ConsoleHandler struct {
// contains filtered or unexported fields
}
ConsoleHandler writes log messages to the console with async buffering
func NewConsoleHandler ¶
func NewConsoleHandler(level Level) *ConsoleHandler
NewConsoleHandler creates a new console handler
func (*ConsoleHandler) AddOutputs ¶
func (h *ConsoleHandler) AddOutputs(writers ...io.Writer)
AddOutputs adds multiple output writers to this handler
func (*ConsoleHandler) Close ¶
func (h *ConsoleHandler) Close() error
Close shuts down the handler and releases resources
func (*ConsoleHandler) Flush ¶
func (h *ConsoleHandler) Flush()
Flush ensures all buffered messages are written
func (*ConsoleHandler) SetLevel ¶
func (h *ConsoleHandler) SetLevel(level Level)
SetLevel sets the minimum log level for this handler
func (*ConsoleHandler) SetOutput ¶
func (h *ConsoleHandler) SetOutput(w io.Writer)
SetOutput sets the output writer for this handler
func (*ConsoleHandler) With ¶
func (h *ConsoleHandler) With(args ...any) Handler
With returns a new handler with the given structured fields
func (*ConsoleHandler) WithGroup ¶
func (h *ConsoleHandler) WithGroup(name string) Handler
WithGroup returns a new handler that starts a group
type FileHandler ¶
type FileHandler struct {
// contains filtered or unexported fields
}
FileHandler writes log messages to files with rotation support
func NewFileHandler ¶
func NewFileHandler(config FileHandlerConfig) (*FileHandler, error)
NewFileHandler creates a new file handler with the specified configuration
func (*FileHandler) AddOutputs ¶
func (h *FileHandler) AddOutputs(writers ...io.Writer)
AddOutputs adds multiple output writers to this handler
func (*FileHandler) Close ¶
func (h *FileHandler) Close() error
Close shuts down the handler and releases resources
func (*FileHandler) Flush ¶
func (h *FileHandler) Flush()
Flush ensures all buffered messages are written
func (*FileHandler) GetCurrentFilePath ¶
func (h *FileHandler) GetCurrentFilePath() string
GetCurrentFilePath returns the current log file path
func (*FileHandler) SetLevel ¶
func (h *FileHandler) SetLevel(level Level)
SetLevel sets the minimum log level for this handler
func (*FileHandler) SetOutput ¶
func (h *FileHandler) SetOutput(w io.Writer)
SetOutput sets the output writer for this handler
func (*FileHandler) With ¶
func (h *FileHandler) With(args ...any) Handler
With returns a new handler with the given structured fields
func (*FileHandler) WithGroup ¶
func (h *FileHandler) WithGroup(name string) Handler
WithGroup returns a new handler that starts a group
type FileHandlerConfig ¶
type FileHandlerConfig struct {
// LogDir is the directory where log files will be stored
LogDir string
// Filename is the base filename pattern (can include strftime-like placeholders)
Filename string
// Rotation defines when to rotate log files
Rotation RotationInterval
// Level is the minimum log level to output
Level Level
}
FileHandlerConfig holds configuration for FileHandler
type Formatter ¶
type Formatter interface {
Format(level Level, ctx context.Context, format string, args ...any) string
}
Formatter converts log data into string representations
type Handler ¶
type Handler interface {
io.Writer
Output(level Level, ctx context.Context, format string, args ...any)
With(args ...any) Handler
WithGroup(name string) Handler
SetLevel(level Level)
SetOutput(w io.Writer)
AddOutputs(writers ...io.Writer)
Flush()
Close() error
}
Handler formats and writes log messages
type Level ¶
type Level uint32
const ( // TraceLevel is the most verbose level, typically used for detailed debugging TraceLevel Level = iota // DebugLevel is used for debugging information that's useful during development DebugLevel // InfoLevel is used for general operational information InfoLevel // WarnLevel is used for warning conditions that don't require immediate action WarnLevel // ErrorLevel is used for error conditions that require attention ErrorLevel // FatalLevel is used for fatal errors that cause the program to exit FatalLevel // PanicLevel is the most severe level, causing the program to panic PanicLevel )
func ParseLevel ¶
ParseLevel parses a string into a Level Returns InfoLevel for unknown strings
type Logger ¶
type Logger interface {
Trace(format string, args ...any)
Debug(format string, args ...any)
Info(format string, args ...any)
Warn(format string, args ...any)
Error(format string, args ...any)
Fatal(format string, args ...any)
Panic(format string, args ...any)
CtxTrace(ctx context.Context, format string, args ...any)
CtxDebug(ctx context.Context, format string, args ...any)
CtxInfo(ctx context.Context, format string, args ...any)
CtxWarn(ctx context.Context, format string, args ...any)
CtxError(ctx context.Context, format string, args ...any)
CtxFatal(ctx context.Context, format string, args ...any)
CtxPanic(ctx context.Context, format string, args ...any)
With(args ...any) Logger
WithGroup(name string) Logger
SetLevel(level Level)
SetOutput(w io.Writer)
AddOutputs(writers ...io.Writer)
Flush()
Close() error
}
Logger provides methods for all log levels with support for both traditional and structured logging
func NewLogger ¶
NewLogger creates a new logger with the specified handlers If no handlers are provided, a default console handler is used
type MultiError ¶
type MultiError struct {
Errors []error
}
MultiError represents multiple errors from closing handlers
func (*MultiError) Error ¶
func (m *MultiError) Error() string
type RotationInterval ¶
type RotationInterval int
RotationInterval defines when to rotate log files
const ( // RotationNone means no rotation (single file) RotationNone RotationInterval = iota // RotationHourly rotates logs every hour RotationHourly // RotationDaily rotates logs every day RotationDaily // RotationWeekly rotates logs every week RotationWeekly // RotationMonthly rotates logs every month RotationMonthly )
type SyncConsoleHandler ¶
type SyncConsoleHandler struct {
// contains filtered or unexported fields
}
SyncConsoleHandler is a synchronous version of ConsoleHandler for testing It writes directly to the output without async buffering
func NewSyncConsoleHandler ¶
func NewSyncConsoleHandler(level Level) *SyncConsoleHandler
NewSyncConsoleHandler creates a new synchronous console handler
func (*SyncConsoleHandler) AddOutputs ¶
func (h *SyncConsoleHandler) AddOutputs(writers ...io.Writer)
AddOutputs adds multiple output writers to this handler
func (*SyncConsoleHandler) Close ¶
func (h *SyncConsoleHandler) Close() error
Close is a no-op for synchronous handler
func (*SyncConsoleHandler) Flush ¶
func (h *SyncConsoleHandler) Flush()
Flush is a no-op for synchronous handler
func (*SyncConsoleHandler) SetLevel ¶
func (h *SyncConsoleHandler) SetLevel(level Level)
SetLevel sets the minimum log level for this handler
func (*SyncConsoleHandler) SetOutput ¶
func (h *SyncConsoleHandler) SetOutput(w io.Writer)
SetOutput sets the output writer for this handler
func (*SyncConsoleHandler) With ¶
func (h *SyncConsoleHandler) With(args ...any) Handler
With returns a new handler with the given structured fields
func (*SyncConsoleHandler) WithGroup ¶
func (h *SyncConsoleHandler) WithGroup(name string) Handler
WithGroup returns a new handler that starts a group
type TextFormatter ¶
type TextFormatter struct {
// contains filtered or unexported fields
}
TextFormatter formats log messages as human-readable text
func NewTextFormatter ¶
func NewTextFormatter(color bool) *TextFormatter
NewTextFormatter creates a new text formatter