Documentation
¶
Overview ¶
Package log provides a structured logging system for Rune services.
Index ¶
- Constants
- Variables
- func ContextInjector(ctx context.Context, fields Fields) context.Context
- func Debug(msg string, fields ...Field)
- func Debugf(msg string, args ...interface{})
- func Error(msg string, fields ...Field)
- func Errorf(msg string, args ...interface{})
- func Fatal(msg string, fields ...Field)
- func Fatalf(msg string, args ...interface{})
- func Info(msg string, fields ...Field)
- func Infof(msg string, args ...interface{})
- func RedirectStdLog(logger Logger) func()
- func SetDefaultLogger(logger Logger)
- func StdLogWriter(logger Logger, level Level) io.Writer
- func ToStdLogger(logger Logger, prefix string, flag int) *stdlog.Logger
- func Warn(msg string, fields ...Field)
- func Warnf(msg string, args ...interface{})
- func WithLogger(ctx context.Context, logger Logger) context.Context
- type BaseLogger
- func (l *BaseLogger) Debug(msg string, fields ...Field)
- func (l *BaseLogger) Debugf(msg string, args ...interface{})
- func (l *BaseLogger) Error(msg string, fields ...Field)
- func (l *BaseLogger) Errorf(msg string, args ...interface{})
- func (l *BaseLogger) Fatal(msg string, fields ...Field)
- func (l *BaseLogger) Fatalf(msg string, args ...interface{})
- func (l *BaseLogger) GetLevel() Level
- func (l *BaseLogger) Info(msg string, fields ...Field)
- func (l *BaseLogger) Infof(msg string, args ...interface{})
- func (l *BaseLogger) Outputs() []Output
- func (l *BaseLogger) SetLevel(level Level)
- func (l *BaseLogger) Warn(msg string, fields ...Field)
- func (l *BaseLogger) Warnf(msg string, args ...interface{})
- func (l *BaseLogger) With(fields ...Field) Logger
- func (l *BaseLogger) WithComponent(component string) Logger
- func (l *BaseLogger) WithContext(ctx context.Context) Logger
- func (l *BaseLogger) WithError(err error) Logger
- func (l *BaseLogger) WithField(key string, value interface{}) Logger
- func (l *BaseLogger) WithFields(fields Fields) Logger
- type Config
- type ConsoleOutput
- type ConsoleOutputOption
- type Entry
- type Field
- func Any(key string, value interface{}) Field
- func Bool(key string, value bool) Field
- func Component(value string) Field
- func Duration(key string, value time.Duration) Field
- func Err(err error) Field
- func F(key string, value interface{}) Field
- func Float64(key string, value float64) Field
- func Int(key string, value int) Field
- func Int64(key string, value int64) Field
- func Json(key string, value interface{}) Field
- func RequestID(value string) Field
- func SpanID(value string) Field
- func Str(key, value string) Field
- func Time(key string, value time.Time) Field
- func TraceID(value string) Field
- type Fields
- type FileOutput
- type FileOutputOption
- type Formatter
- type Hook
- type JSONFormatter
- type Level
- type Logger
- func ApplyConfig(config *Config) (Logger, error)
- func FromContext(ctx context.Context) Logger
- func GetDefaultLogger() Logger
- func NewLogger(options ...LoggerOption) Logger
- func With(fields ...Field) Logger
- func WithComponent(component string) Logger
- func WithContext(ctx context.Context) Logger
- func WithError(err error) Logger
- func WithField(key string, value interface{}) Logger
- func WithFields(fields Fields) Logger
- type LoggerOption
- type NullOutput
- type OTELHook
- type Output
- type OutputConfig
- type RedactionHook
- type SamplingConfig
- type SamplingHook
- type TestEntry
- type TestLogger
- func (l *TestLogger) AssertLogged(level Level, containsMessage string) bool
- func (l *TestLogger) AssertLoggedWithField(level Level, containsMessage string, key string, value interface{}) bool
- func (l *TestLogger) ClearEntries()
- func (l *TestLogger) Debug(msg string, fields ...Field)
- func (l *TestLogger) Debugf(format string, args ...interface{})
- func (l *TestLogger) Error(msg string, fields ...Field)
- func (l *TestLogger) Errorf(format string, args ...interface{})
- func (l *TestLogger) Fatal(msg string, fields ...Field)
- func (l *TestLogger) Fatalf(format string, args ...interface{})
- func (l *TestLogger) GetEntries() []TestEntry
- func (l *TestLogger) GetLevel() Level
- func (l *TestLogger) Info(msg string, fields ...Field)
- func (l *TestLogger) Infof(format string, args ...interface{})
- func (l *TestLogger) SetLevel(level Level)
- func (l *TestLogger) Warn(msg string, fields ...Field)
- func (l *TestLogger) Warnf(format string, args ...interface{})
- func (l *TestLogger) With(fields ...Field) Logger
- func (l *TestLogger) WithComponent(component string) Logger
- func (l *TestLogger) WithContext(ctx context.Context) Logger
- func (l *TestLogger) WithError(err error) Logger
- func (l *TestLogger) WithField(key string, value interface{}) Logger
- func (l *TestLogger) WithFields(fields Fields) Logger
- type TextFormatter
Constants ¶
const ( RequestIDKey = "request_id" TraceIDKey = "trace_id" SpanIDKey = "span_id" ComponentKey = "component" OperationKey = "operation" )
Context keys for propagating logging context
Variables ¶
var ErrEntrySampled = &entrySampledError{}
ErrEntrySampled is returned when an entry is sampled out.
Functions ¶
func ContextInjector ¶
ContextInjector injects logging fields into a context.Context.
func Debugf ¶
func Debugf(msg string, args ...interface{})
Standard global logging methods with key-value pairs (for backward compatibility)
func RedirectStdLog ¶
func RedirectStdLog(logger Logger) func()
RedirectStdLog redirects the standard library's default logger to our logger This is useful for capturing logs from third-party libraries that use the standard log package directly
func SetDefaultLogger ¶
func SetDefaultLogger(logger Logger)
SetDefaultLogger sets the global default logger.
func StdLogWriter ¶
StdLogWriter creates an io.Writer that logs lines at the specified level Useful for redirecting output from third-party libraries
func ToStdLogger ¶
ToStdLogger converts our structured Logger to a standard library *log.Logger This is helpful for integrating with libraries or components that expect a standard logger
Types ¶
type BaseLogger ¶
type BaseLogger struct {
// contains filtered or unexported fields
}
BaseLogger implements the Logger interface.
func (*BaseLogger) Debug ¶
func (l *BaseLogger) Debug(msg string, fields ...Field)
Debug logs a message at the debug level with fields.
func (*BaseLogger) Debugf ¶
func (l *BaseLogger) Debugf(msg string, args ...interface{})
Debugf logs a message at the debug level with key-value args.
func (*BaseLogger) Error ¶
func (l *BaseLogger) Error(msg string, fields ...Field)
Error logs a message at the error level with fields.
func (*BaseLogger) Errorf ¶
func (l *BaseLogger) Errorf(msg string, args ...interface{})
Errorf logs a message at the error level with key-value args.
func (*BaseLogger) Fatal ¶
func (l *BaseLogger) Fatal(msg string, fields ...Field)
Fatal logs a message at the fatal level with fields and then exits.
func (*BaseLogger) Fatalf ¶
func (l *BaseLogger) Fatalf(msg string, args ...interface{})
Fatalf logs a message at the fatal level with key-value args and then exits.
func (*BaseLogger) GetLevel ¶
func (l *BaseLogger) GetLevel() Level
GetLevel returns the current minimum log level.
func (*BaseLogger) Info ¶
func (l *BaseLogger) Info(msg string, fields ...Field)
Info logs a message at the info level with fields.
func (*BaseLogger) Infof ¶
func (l *BaseLogger) Infof(msg string, args ...interface{})
Infof logs a message at the info level with key-value args.
func (*BaseLogger) Outputs ¶
func (l *BaseLogger) Outputs() []Output
Outputs returns the configured log outputs.
func (*BaseLogger) SetLevel ¶
func (l *BaseLogger) SetLevel(level Level)
SetLevel sets the minimum log level.
func (*BaseLogger) Warn ¶
func (l *BaseLogger) Warn(msg string, fields ...Field)
Warn logs a message at the warn level with fields.
func (*BaseLogger) Warnf ¶
func (l *BaseLogger) Warnf(msg string, args ...interface{})
Warnf logs a message at the warn level with key-value args.
func (*BaseLogger) With ¶
func (l *BaseLogger) With(fields ...Field) Logger
With adds fields to the logger (new Field-based API)
func (*BaseLogger) WithComponent ¶
func (l *BaseLogger) WithComponent(component string) Logger
WithComponent returns a new logger with the component field added.
func (*BaseLogger) WithContext ¶
func (l *BaseLogger) WithContext(ctx context.Context) Logger
WithContext returns a new logger with fields from the context.
func (*BaseLogger) WithError ¶
func (l *BaseLogger) WithError(err error) Logger
WithError returns a new logger with the error added as a field.
func (*BaseLogger) WithField ¶
func (l *BaseLogger) WithField(key string, value interface{}) Logger
WithField returns a new logger with the field added to it.
func (*BaseLogger) WithFields ¶
func (l *BaseLogger) WithFields(fields Fields) Logger
WithFields returns a new logger with the fields added to it.
type Config ¶
type Config struct {
// Level sets the minimum log level
Level string `json:"level" yaml:"level"`
// Format sets the output format (json, text)
Format string `json:"format" yaml:"format"`
// Outputs defines where logs should be written
Outputs []OutputConfig `json:"outputs" yaml:"outputs"`
// Sampling defines sampling behavior for high-volume logs
Sampling *SamplingConfig `json:"sampling" yaml:"sampling"`
// EnableCaller enables adding caller information to logs
EnableCaller bool `json:"enable_caller" yaml:"enable_caller"`
// RedactedFields lists fields that should be redacted (e.g. passwords)
RedactedFields []string `json:"redacted_fields" yaml:"redacted_fields"`
}
Config defines logging configuration.
type ConsoleOutput ¶
type ConsoleOutput struct {
// contains filtered or unexported fields
}
ConsoleOutput writes log entries to the console (stdout/stderr).
func NewConsoleOutput ¶
func NewConsoleOutput(options ...ConsoleOutputOption) *ConsoleOutput
NewConsoleOutput creates a new ConsoleOutput with the given options.
func (*ConsoleOutput) Close ¶
func (o *ConsoleOutput) Close() error
Close implements the Output interface but does nothing for console output.
type ConsoleOutputOption ¶
type ConsoleOutputOption func(*ConsoleOutput)
ConsoleOutputOption is a function that configures a ConsoleOutput.
func WithCustomErrorWriter ¶
func WithCustomErrorWriter(writer io.Writer) ConsoleOutputOption
WithCustomErrorWriter configures the ConsoleOutput to use a custom error writer.
func WithCustomWriter ¶
func WithCustomWriter(writer io.Writer) ConsoleOutputOption
WithCustomWriter configures the ConsoleOutput to use a custom writer.
func WithErrorToStderr ¶
func WithErrorToStderr() ConsoleOutputOption
WithErrorToStderr configures the ConsoleOutput to send error and fatal logs to stderr.
func WithStderr ¶
func WithStderr() ConsoleOutputOption
WithStderr configures the ConsoleOutput to use stderr.
type Entry ¶
type Entry struct {
Level Level
Message string
Fields Fields
Timestamp time.Time
Caller string
Error error
}
Entry represents a single log entry.
type Field ¶
type Field struct {
Key string
Value interface{}
}
Field represents a structured log field with a key and value
type Fields ¶
type Fields map[string]interface{}
Fields is a map of field names to values.
func ContextExtractor ¶
ContextExtractor extracts logging context from a context.Context.
type FileOutput ¶
type FileOutput struct {
// contains filtered or unexported fields
}
FileOutput writes log entries to a file.
func NewFileOutput ¶
func NewFileOutput(filename string, options ...FileOutputOption) *FileOutput
NewFileOutput creates a new FileOutput with the given options.
type FileOutputOption ¶
type FileOutputOption func(*FileOutput)
FileOutputOption is a function that configures a FileOutput.
func WithBackupFormat ¶
func WithBackupFormat(format string) FileOutputOption
WithBackupFormat sets the format for backup filenames.
func WithMaxAge ¶
func WithMaxAge(maxAge time.Duration) FileOutputOption
WithMaxAge sets the maximum file age.
func WithMaxBackups ¶
func WithMaxBackups(maxBackups int) FileOutputOption
WithMaxBackups sets the maximum number of backup files.
func WithMaxSize ¶
func WithMaxSize(maxBytes int64) FileOutputOption
WithMaxSize sets the maximum file size.
type JSONFormatter ¶
type JSONFormatter struct {
TimestampFormat string // Format for timestamps
EnableCaller bool // Enable caller information (default: false)
}
JSONFormatter formats log entries as JSON.
type Level ¶
type Level int
Level represents the severity level of a log message.
func ParseLevel ¶
ParseLevel parses a level string into a Level.
type Logger ¶
type Logger interface {
// Standard logging methods with structured context (Field-based API)
Debug(msg string, fields ...Field)
Info(msg string, fields ...Field)
Warn(msg string, fields ...Field)
Error(msg string, fields ...Field)
Fatal(msg string, fields ...Field)
// Standard logging methods with key-value pairs (for backward compatibility)
Debugf(msg string, args ...interface{})
Infof(msg string, args ...interface{})
Warnf(msg string, args ...interface{})
Errorf(msg string, args ...interface{})
Fatalf(msg string, args ...interface{})
// Field creation methods (for backward compatibility)
WithField(key string, value interface{}) Logger
WithFields(fields Fields) Logger
WithError(err error) Logger
// With adds multiple fields to the logger (for new Field-based API)
With(fields ...Field) Logger
// WithContext adds request context to the Logger
WithContext(ctx context.Context) Logger
// WithComponent tags logs with a component name
WithComponent(component string) Logger
// SetLevel sets the minimum log level
SetLevel(level Level)
// GetLevel returns the current minimum log level
GetLevel() Level
}
Logger defines the core logging interface for Rune components.
func ApplyConfig ¶
ApplyConfig creates a logger from a configuration.
func FromContext ¶
FromContext extracts a logger from a context.Context. If no logger is found, it returns the default logger.
func GetDefaultLogger ¶
func GetDefaultLogger() Logger
GetDefaultLogger returns the global default logger.
func NewLogger ¶
func NewLogger(options ...LoggerOption) Logger
NewLogger creates a new logger with the given options.
func WithComponent ¶
func WithContext ¶
func WithFields ¶
type LoggerOption ¶
type LoggerOption func(*BaseLogger)
LoggerOption is a function that configures a logger.
func WithFormatter ¶
func WithFormatter(formatter Formatter) LoggerOption
WithFormatter sets the log formatter.
func WithOutput ¶
func WithOutput(output Output) LoggerOption
WithOutput adds an output to the logger.
type NullOutput ¶
type NullOutput struct{}
NullOutput discards all log entries.
func (*NullOutput) Close ¶
func (o *NullOutput) Close() error
Close implements the Output interface but does nothing.
type OTELHook ¶
type OTELHook struct{}
OTELHook integrates with OpenTelemetry to add trace information to logs.
type OutputConfig ¶
type OutputConfig struct {
// Type is the output type (console, file, syslog, remote)
Type string `json:"type" yaml:"type"`
// Options are type-specific configuration options
Options map[string]interface{} `json:"options" yaml:"options"`
}
OutputConfig defines a single log output.
type RedactionHook ¶
type RedactionHook struct {
// contains filtered or unexported fields
}
RedactionHook redacts sensitive values from log entries.
func NewRedactionHook ¶
func NewRedactionHook(fields []string) *RedactionHook
NewRedactionHook creates a new redaction hook.
func (*RedactionHook) Fire ¶
func (h *RedactionHook) Fire(entry *Entry) error
Fire executes the hook's logic for a log entry.
func (*RedactionHook) Levels ¶
func (h *RedactionHook) Levels() []Level
Levels returns the levels this hook should be called for.
type SamplingConfig ¶
type SamplingConfig struct {
// Initial is the initial number of entries to process without sampling
Initial int `json:"initial" yaml:"initial"`
// Thereafter is how often to sample after the initial entries
Thereafter int `json:"thereafter" yaml:"thereafter"`
}
SamplingConfig defines sampling behavior for high-volume logs.
type SamplingHook ¶
type SamplingHook struct {
// contains filtered or unexported fields
}
SamplingHook implements sampling for high-volume logs.
func NewSamplingHook ¶
func NewSamplingHook(initial, thereafter int) *SamplingHook
NewSamplingHook creates a new sampling hook.
func (*SamplingHook) Fire ¶
func (h *SamplingHook) Fire(entry *Entry) error
Fire executes the hook's logic for a log entry.
func (*SamplingHook) Levels ¶
func (h *SamplingHook) Levels() []Level
Levels returns the levels this hook should be called for.
type TestLogger ¶
type TestLogger struct {
// contains filtered or unexported fields
}
TestLogger is a Logger implementation for testing that captures logs without producing output and provides methods to verify logging behavior.
func NewTestLogger ¶
func NewTestLogger() *TestLogger
NewTestLogger creates a new TestLogger for use in unit tests
func (*TestLogger) AssertLogged ¶
func (l *TestLogger) AssertLogged(level Level, containsMessage string) bool
AssertLogged returns true if a log entry with the given level and message was captured
func (*TestLogger) AssertLoggedWithField ¶
func (l *TestLogger) AssertLoggedWithField(level Level, containsMessage string, key string, value interface{}) bool
AssertLoggedWithField returns true if a log entry with the given level, message, and field key/value was captured
func (*TestLogger) ClearEntries ¶
func (l *TestLogger) ClearEntries()
ClearEntries clears all captured log entries
func (*TestLogger) Debug ¶
func (l *TestLogger) Debug(msg string, fields ...Field)
Debug logs a debug message
func (*TestLogger) Debugf ¶
func (l *TestLogger) Debugf(format string, args ...interface{})
Debugf logs a formatted debug message
func (*TestLogger) Error ¶
func (l *TestLogger) Error(msg string, fields ...Field)
Error logs an error message
func (*TestLogger) Errorf ¶
func (l *TestLogger) Errorf(format string, args ...interface{})
Errorf logs a formatted error message
func (*TestLogger) Fatal ¶
func (l *TestLogger) Fatal(msg string, fields ...Field)
Fatal logs a fatal message
func (*TestLogger) Fatalf ¶
func (l *TestLogger) Fatalf(format string, args ...interface{})
Fatalf logs a formatted fatal message
func (*TestLogger) GetEntries ¶
func (l *TestLogger) GetEntries() []TestEntry
GetEntries returns all captured log entries
func (*TestLogger) GetLevel ¶
func (l *TestLogger) GetLevel() Level
GetLevel returns the current minimum log level
func (*TestLogger) Info ¶
func (l *TestLogger) Info(msg string, fields ...Field)
Info logs an info message
func (*TestLogger) Infof ¶
func (l *TestLogger) Infof(format string, args ...interface{})
Infof logs a formatted info message
func (*TestLogger) SetLevel ¶
func (l *TestLogger) SetLevel(level Level)
SetLevel sets the minimum log level
func (*TestLogger) Warn ¶
func (l *TestLogger) Warn(msg string, fields ...Field)
Warn logs a warning message
func (*TestLogger) Warnf ¶
func (l *TestLogger) Warnf(format string, args ...interface{})
Warnf logs a formatted warning message
func (*TestLogger) With ¶
func (l *TestLogger) With(fields ...Field) Logger
With returns a new logger with the provided fields added to the context
func (*TestLogger) WithComponent ¶
func (l *TestLogger) WithComponent(component string) Logger
WithComponent returns a new logger with a component field
func (*TestLogger) WithContext ¶
func (l *TestLogger) WithContext(ctx context.Context) Logger
WithContext returns a new logger with context values extracted as fields
func (*TestLogger) WithError ¶
func (l *TestLogger) WithError(err error) Logger
WithError returns a new logger with an error field
func (*TestLogger) WithField ¶
func (l *TestLogger) WithField(key string, value interface{}) Logger
WithField returns a new logger with a field added to the context
func (*TestLogger) WithFields ¶
func (l *TestLogger) WithFields(fields Fields) Logger
WithFields returns a new logger with fields added to the context
type TextFormatter ¶
type TextFormatter struct {
TimestampFormat string // Format for timestamps
EnableCaller bool // Enable caller information (default: false)
DisableColors bool // Disable color output
ShortTimestamp bool // Use short timestamp without date (default: false)
DisableTimestamp bool // Disable timestamp output
}
TextFormatter formats log entries as human-readable text.
func NewTextFormatter ¶
func NewTextFormatter() *TextFormatter
NewTextFormatter creates a new TextFormatter with sensible defaults.