log

package module
v0.0.0-...-8147c01 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 12 Imported by: 5

Documentation

Index

Constants

View Source
const LogIDKey contextKey = "log_id"

LogIDKey is the context key for log ID

Variables

This section is empty.

Functions

func AddOutputs

func AddOutputs(writers ...io.Writer)

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 Close

func Close() error

Close closes the default logger and releases resources

func CtxDebug

func CtxDebug(ctx context.Context, format string, args ...any)

CtxDebug logs a message at DebugLevel with context

func CtxError

func CtxError(ctx context.Context, format string, args ...any)

CtxError logs a message at ErrorLevel with context

func CtxFatal

func CtxFatal(ctx context.Context, format string, args ...any)

CtxFatal logs a message at FatalLevel with context and exits the program

func CtxInfo

func CtxInfo(ctx context.Context, format string, args ...any)

CtxInfo logs a message at InfoLevel with context

func CtxPanic

func CtxPanic(ctx context.Context, format string, args ...any)

CtxPanic logs a message at PanicLevel with context and panics

func CtxTrace

func CtxTrace(ctx context.Context, format string, args ...any)

CtxTrace logs a message at TraceLevel with context

func CtxWarn

func CtxWarn(ctx context.Context, format string, args ...any)

CtxWarn logs a message at WarnLevel with context

func Debug

func Debug(format string, args ...any)

Debug logs a message at DebugLevel

func Error

func Error(format string, args ...any)

Error logs a message at ErrorLevel

func Fatal

func Fatal(format string, args ...any)

Fatal logs a message at FatalLevel and exits the program

func Flush

func Flush()

Flush flushes all pending log messages

func Info

func Info(format string, args ...any)

Info logs a message at InfoLevel

func NewStructuredLogHandler

func NewStructuredLogHandler(level Level) *structuredLogHandler

NewStructuredLogHandler creates a new structured log handler

func Panic

func Panic(format string, args ...any)

Panic logs a message at PanicLevel and panics

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 SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output writer for the default logger

func SetStructuredOutput

func SetStructuredOutput(out io.Writer, json bool)

SetStructuredOutput configures structured logging output This sets up the underlying slog logger for JSON or text output

func Trace

func Trace(format string, args ...any)

Trace logs a message at TraceLevel

func Warn

func Warn(format string, args ...any)

Warn logs a message at WarnLevel

func WithLogID

func WithLogID(ctx context.Context, logID string) context.Context

WithLogID creates a new context with the specified log ID This is a convenience function for users to easily add log IDs to their contexts

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) Output

func (h *ConsoleHandler) Output(level Level, ctx context.Context, format string, args ...any)

Output writes a log message to the console

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

func (*ConsoleHandler) Write

func (h *ConsoleHandler) Write(p []byte) (int, error)

Write implements io.Writer for direct writes

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) Output

func (h *FileHandler) Output(level Level, ctx context.Context, format string, args ...any)

Output writes a log message to the file

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

func (*FileHandler) Write

func (h *FileHandler) Write(p []byte) (int, error)

Write implements io.Writer for direct writes

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

func ParseLevel(s string) Level

ParseLevel parses a string into a Level Returns InfoLevel for unknown strings

func (Level) Color

func (l Level) Color() int

Color returns the ANSI color code for the log level Used for colored console output

func (Level) String

func (l Level) String() string

String returns the string representation of the log level

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

func NewLogger(handlers ...Handler) Logger

NewLogger creates a new logger with the specified handlers If no handlers are provided, a default console handler is used

func With

func With(args ...any) Logger

With creates a new logger with the given structured fields

func WithGroup

func WithGroup(name string) Logger

WithGroup creates a new logger that starts a group

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) Output

func (h *SyncConsoleHandler) Output(level Level, ctx context.Context, format string, args ...any)

Output writes a log message to the console synchronously

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

func (*SyncConsoleHandler) Write

func (h *SyncConsoleHandler) Write(p []byte) (int, error)

Write implements io.Writer for direct writes

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

func (*TextFormatter) Format

func (f *TextFormatter) Format(level Level, ctx context.Context, format string, args ...any) string

Format converts log data into a formatted string

Jump to

Keyboard shortcuts

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