Documentation
¶
Overview ¶
Package lol (lots of logs) provides a unified logging interface with multiple backends.
This package offers a simple, structured logging interface that can be backed by different logging implementations. Currently it supports zerolog as the primary backend.
Key features: - Structured logging with fields - Multiple log levels (trace, debug, info, warn, error, fatal, panic) - APM trace context integration - Environment-aware configuration - Testing utilities
Basic usage:
logger := lol.NewZerolog(
lol.WithFields(lol.Fields{"service": "myapp"}),
lol.WithEnv("production"),
lol.WithLevel("info"),
lol.WithWriter(os.Stdout),
lol.WithAPM(lol.APMConfig{Enabled: true}),
)
logger.Info("Application started")
logger.WithField("user_id", 123).Warn("User action")
For testing:
testLogger := lol.NewTest()
testLogger.Error("This won't be printed")
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ZeroLogger = NewZerolog( WithFields(Fields{"type": "default"}), WithEnv(EnvLocal), WithLevel(LevelInfo), WithWriter(os.Stderr), ) ZeroTestLogger = NewZerolog( WithEnv(EnvTest), WithLevel(LevelError), WithWriter(os.Stderr), ) ZeroDiscardLogger = NewZerolog( WithWriter(io.Discard), ) )
Functions ¶
This section is empty.
Types ¶
type Level ¶
type Level uint32
Logrus internal Levels exposed (abstracted from logrus)
const ( // LevelPanic level, highest level of severity. Logs and then calls panic with the // message passed to Debug, Info, ... LevelPanic Level = iota // LevelFatal level. Logs and then calls `logger.Exit(1)`. It will exit even if the // logging level is set to Panic. LevelFatal // LevelError level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. LevelError // LevelWarn level. Non-critical entries that deserve eyes. LevelWarn // LevelInfo level. General operational entries about what's going on inside the // application. LevelInfo // LevelDebug level. Usually only enabled when debugging. Very verbose logging. LevelDebug // LevelTrace level. Designates finer-grained informational events than the Debug. LevelTrace )
type Logger ¶
type Logger interface {
Trace(args ...any)
Debug(args ...any)
Print(args ...any)
Info(args ...any)
Warn(args ...any)
Warning(args ...any)
Error(args ...any)
Fatal(args ...any)
Panic(args ...any)
// printf
Tracef(format string, args ...any)
Debugf(format string, args ...any)
Printf(format string, args ...any)
Infof(format string, args ...any)
Warnf(format string, args ...any)
Warningf(format string, args ...any)
Errorf(format string, args ...any)
Fatalf(format string, args ...any)
Panicf(format string, args ...any)
// ln
Traceln(args ...any)
Debugln(args ...any)
Println(args ...any)
Infoln(args ...any)
Warnln(args ...any)
Warningln(args ...any)
Errorln(args ...any)
Fatalln(args ...any)
Panicln(args ...any)
WithField(key string, value any) Logger
WithFields(fields Fields) Logger
WithTrace(ctx context.Context) Logger
}
func NewZerolog ¶
NewZerolog creates a new Logger using zerolog as the backend
Example ¶
ExampleNewZerolog demonstrates creating a structured logger with zerolog backend
// Create a logger with custom fields and configuration
var buf bytes.Buffer
logger := NewZerolog(
WithFields(Fields{"service": "example-app", "version": "1.0.0"}),
WithEnv(EnvProd),
WithLevel(LevelInfo),
WithWriter(&buf),
)
// Log some messages
logger.Info("Application started successfully")
logger.WithField("user_id", 123).WithField("action", "login").Info("User logged in")
logger.Warn("This is a warning message")
fmt.Printf(
"Logged output contains 'Application started': %t\n",
bytes.Contains(buf.Bytes(), []byte("Application started")),
)
fmt.Printf(
"Logged output contains 'user_id': %t\n",
bytes.Contains(buf.Bytes(), []byte("user_id")),
)
fmt.Printf(
"Logged output contains 'service': %t\n",
bytes.Contains(buf.Bytes(), []byte("service")),
)
fmt.Printf(
"Logged output contains 'version': %t\n",
bytes.Contains(buf.Bytes(), []byte("version")),
)
Output: Logged output contains 'Application started': true Logged output contains 'user_id': true Logged output contains 'service': true Logged output contains 'version': true
type Opt ¶
type Opt func(*config)
func WithTimeFieldFormat ¶
WithTimeFieldFormat sets the format for time fields
func WithWriter ¶
WithWriter sets the output writer for the logger. By default it uses os.Stderr