Documentation
¶
Overview ¶
Package golog provides a simple, structured logging library for Go applications. It supports JSON output, context-based logging, and field enrichment.
Features:
- Structured JSON logging
- Context support
- Field enrichment
- Multiple log levels (Debug, Info, Error)
- Flushable output
Example usage:
writer := golog.NewJSONWriter(os.Stdout)
golog.SetWriter(writer)
golog.Info("Hello, world!")
golog.With("user_id", 123).Info("User logged in")
golog.WithError(err).Error("Operation failed")
golog.WithContext(ctx).WithFields(map[string]any{"request_id": "abc"}).Info("Request processed")
golog.WithPairs("user_id", 123, "action", "login").Info("User logged in")
golog.SetLevel("debug")
golog.SetSkipFrames(2)
Index ¶
- Constants
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any) error
- func Flush()
- func Info(msg string, args ...any)
- func LevelString(level int) string
- func NewDefaultWriter(output io.Writer) *defaultWriter
- func NewJSONWriter(output io.Writer) *jsonWriter
- func ParseLevel(level string) int
- func RegisterEnricher(enricher Enricher)
- func SetLevel(level int)
- func SetSkipFrames(skip int)
- func SetWriter(logger LogWriter)
- type Enricher
- type EnricherFunc
- type LogScope
- func (l *LogScope) Context() context.Context
- func (l *LogScope) Debug(msg string, args ...any)
- func (l *LogScope) Error(msg string, args ...any) error
- func (l *LogScope) Flush()
- func (l *LogScope) Info(msg string, args ...any)
- func (l *LogScope) With(key string, value any) *LogScope
- func (l *LogScope) WithContext(ctx context.Context) *LogScope
- func (l *LogScope) WithError(err error) *LogScope
- func (l *LogScope) WithFields(fields map[string]any) *LogScope
- type LogWriter
Constants ¶
const ( LevelDebug = iota // 0 LevelInfo // 1 LevelError // 2 )
Log levels
const ( // FieldTime is the key for time of log FieldTime = "time" // FieldLevel is the key for level of log FieldLevel = "level" // FieldMessage is the key for message of log FieldMessage = "msg" // FieldCaller is the key for caller of log FieldCaller = "caller" )
Standard JSON log field names used in log output
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
Debug logs a message at the debug level. It creates a new scope and writes the message with the DEBUG level.
func Error ¶
Error logs a message at the error level. It creates a new scope and writes the message with the ERROR level.
func Flush ¶
func Flush()
Flush ensures all buffered log entries are written. It calls Flush on the global log writer instance.
func Info ¶
Info logs a message at the info level. It creates a new scope and writes the message with the INFO level.
func LevelString ¶
LevelString converts an integer level to its string representation. Returns "UNKNOWN" if the level is invalid.
func NewDefaultWriter ¶
NewDefaultWriter creates a new defaultWriter instance with the given io.Writer. The writer is wrapped in a buffer for better performance. Example:
writer := NewDefaultWriter(os.Stdout)
func NewJSONWriter ¶
NewJSONWriter creates a new JSON logger that writes to the specified io.Writer
func ParseLevel ¶
ParseLevel converts a string level name to its integer value. The parsing is case-insensitive. Returns -1 if the level name is invalid.
func RegisterEnricher ¶
func RegisterEnricher(enricher Enricher)
RegisterEnricher adds a new enricher to the global enrichers list. Enrichers are called in the order they are registered.
func SetLevel ¶
func SetLevel(level int)
SetLevel sets the minimum log level that should be logged. Only messages with severity >= minLevel will be logged. Valid levels are: DEBUG (0), INFO (1), ERROR (2)
func SetSkipFrames ¶
func SetSkipFrames(skip int)
Types ¶
type Enricher ¶
type Enricher interface {
// Enrich adds additional fields to a log entry based on the context.
// The fields map is modified in place to add the enriched fields.
Enrich(ctx context.Context, level string, msg string, fields map[string]any)
}
Enricher defines the interface for log entry enrichment. Enrichers can add additional fields to log entries based on context.
type EnricherFunc ¶
EnricherFunc is a function type that implements the Enricher interface. It allows functions to be used as enrichers without creating a new type.
type LogScope ¶
type LogScope struct {
// contains filtered or unexported fields
}
LogScope represents a logging context with associated fields and enrichers. It provides methods for adding fields and writing log entries.
func With ¶
With creates a new LogScope with a single key-value field. It is a convenience function for creating a scope with a single field.
func WithContext ¶
WithContext creates a new LogScope with the given context. It is a convenience function for creating a scope with a context.
func WithError ¶
WithError creates a new LogScope with an error field. It is a convenience function for creating a scope with an error.
func WithFields ¶
WithFields creates a new LogScope with multiple fields. It is a convenience function for creating a scope with multiple fields at once.
func WithPairs ¶
WithPairs creates a new LogScope with multiple fields. It is a convenience function for creating a scope with multiple fields at once.
func (*LogScope) Debug ¶
Debug writes a log entry at the debug level. The message and any additional arguments are formatted using fmt.Sprintf.
func (*LogScope) Error ¶
Error writes a log entry at the error level. The message and any additional arguments are formatted using fmt.Sprintf.
func (*LogScope) Flush ¶
func (l *LogScope) Flush()
Flush ensures all buffered log entries are written. It calls Flush on the underlying log writer.
func (*LogScope) Info ¶
Info writes a log entry at the info level. The message and any additional arguments are formatted using fmt.Sprintf.
func (*LogScope) With ¶
With adds a key-value field to this LogScope. It returns the LogScope for method chaining.
func (*LogScope) WithContext ¶
WithContext sets the context for this LogScope. It returns the LogScope for method chaining.
type LogWriter ¶
type LogWriter interface {
// Write writes a log entry with the given level, message, and fields
Write(level int, msg string, fields map[string]any)
// Flush ensures all buffered log entries are written
Flush()
}
LogWriter defines the interface for log output writers. Implementations should handle the actual writing of log entries.