Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateOperationID(component string) string
- func WithComponent(ctx context.Context, component string) context.Context
- func WithFields(ctx context.Context, fields Fields) context.Context
- func WithLogger(ctx context.Context, logger Logger) context.Context
- func WithOperationID(ctx context.Context, operationID string) context.Context
- type Config
- type Fields
- type Format
- type Formatter
- type HumanFormatter
- type JSONFormatter
- type LogContext
- type LogEntry
- type LogLevel
- type Logger
Constants ¶
const (
ContextKeyInstance contextKey = "logging_instance"
)
Variables ¶
var (
ErrValidationFailed = errors.New("validation failed")
)
Functions ¶
func GenerateOperationID ¶
GenerateOperationID generates a simple operation ID based on component and current time.
func WithComponent ¶
WithComponent adds a component name to the context.
func WithFields ¶
WithFields merges additional fields into context.
Types ¶
type Config ¶
type Config struct {
Level LogLevel `json:"level"` // Level is the minimum log level to output
Format Format `json:"format"` // Format is the output format ("human" or "json")
Output io.Writer `json:"-"` // Output is the output stream (empty for os.stdout)
EnableColors bool `json:"colors"` // EnableColors enables colored output for human format
TimeFormat string `json:"time_format"` // TimeFormat is the format for timestamps
}
Config holds the configuration for the logging system.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default logging configuration.
type Formatter ¶
type Formatter interface {
// Format formats a log entry for output
Format(entry *LogEntry, writer io.Writer) error
// Name returns the name of the formatter
Name() string
}
Formatter defines the interface for formatting log entries.
type HumanFormatter ¶
type HumanFormatter struct {
// contains filtered or unexported fields
}
HumanFormatter formats log entries in a human-readable format with optional colors.
func NewHumanFormatter ¶
func NewHumanFormatter(enableColors bool, timeFormat string) *HumanFormatter
NewHumanFormatter creates a new human formatter.
func (*HumanFormatter) Format ¶
func (f *HumanFormatter) Format(entry *LogEntry, writer io.Writer) error
Format formats a log entry in human-readable format.
func (*HumanFormatter) Name ¶
func (f *HumanFormatter) Name() string
Name returns the formatter name.
type JSONFormatter ¶
type JSONFormatter struct {
// contains filtered or unexported fields
}
JSONFormatter formats log entries as JSON objects.
func NewJSONFormatter ¶
func NewJSONFormatter(timeFormat string) *JSONFormatter
NewJSONFormatter creates a new JSON formatter.
type LogContext ¶
type LogContext struct {
// Component is the component or module name
Component string
// OperationID is an identifier for the current operation
OperationID string
// Fields contains additional context fields
Fields map[string]any
}
LogContext holds contextual information for logging operations.
type LogEntry ¶
type LogEntry struct {
Timestamp time.Time // Timestamp when the log entry was created
Level LogLevel // Level of the log entry
Message string // Message is the main log message
Component string // Component is the component or module that generated the log entry
OperationID string // OperationID is an optional identifier for grouping related log entries
Fields map[string]any // Fields contains additional structured data
Error error // Error contains error information if the log entry is error-related
}
LogEntry represents a structured log entry with all necessary information.
type LogLevel ¶
type LogLevel int
LogLevel represents the severity level of log entries.
const ( // LogLevelDebug is the lowest level, used for detailed debugging information. LogLevelDebug LogLevel = iota // LogLevelInfo is used for general information messages. LogLevelInfo // LogLevelWarn is used for warning messages. LogLevelWarn // LogLevelError is used for error messages. LogLevelError // LogLevelFatal is used for fatal error messages that terminate the application. LogLevelFatal )
func (LogLevel) MarshalText ¶
MarshalText makes LogLevel encode as its string (e.g., "INFO") in JSON and text encoders.
type Logger ¶
type Logger interface {
// Debug logs a message at debug level
Debug(ctx context.Context, message string, fields ...Fields)
// Info logs a message at info level
Info(ctx context.Context, message string, fields ...Fields)
// Warn logs a message at warn level
Warn(ctx context.Context, message string, fields ...Fields)
// Error logs a message at error level
Error(ctx context.Context, message string, err error, fields ...Fields)
// Fatal logs a message at fatal level and terminates the application
Fatal(ctx context.Context, message string, err error, fields ...Fields)
// WithContext creates a new logger with the specified context
WithContext(component string, operationID string, fields ...Fields) Logger
// SetLevel sets the minimum log level
SetLevel(level LogLevel)
// SetFormatter sets the log formatter
SetFormatter(formatter Formatter)
// Flush ensures all log entries are written
Flush() error
// Close closes the logger
Close() error
}
Logger defines the interface for logging operations.
func NewDefault ¶
func NewDefault() Logger
NewDefault creates a new logger with default configuration.