Documentation
¶
Overview ¶
Package log provides a structured logger built on top of zerolog with colored console output, JSON mode, API request logging, and field helpers.
Creating a logger ¶
zl, err := log.New(&log.Config{
Level: "info",
DateTimeLayout: time.RFC3339,
Colored: true,
JSONFormat: false,
})
if err != nil {
log.Fatal(err)
}
Basic logging ¶
zl.Info("server started")
zl.Warnf("retrying in %s", delay)
zl.WithField("user", userID).Error("authentication failed")
Wrapping in Logger ¶
Logger embeds Zerolog and adds convenience methods compatible with common logging interfaces.
logger := &log.Logger{Zerolog: zl}
logger.Infof("listening on :%d", port)
API request logging ¶
zl.API(r.Method, r.URL.Path, r.RemoteAddr, status, duration)
Index ¶
- Variables
- type Config
- type Logger
- func (s *Logger) Debug(args ...any)
- func (s *Logger) Debugf(format string, args ...any)
- func (s *Logger) Error(args ...any)
- func (s *Logger) Errorf(format string, args ...any)
- func (s *Logger) Fatal(args ...any)
- func (s *Logger) Fatalf(format string, args ...any)
- func (s *Logger) Info(args ...any)
- func (s *Logger) Infof(format string, args ...any)
- func (s *Logger) Panic(args ...any)
- func (s *Logger) Panicf(format string, args ...any)
- func (s *Logger) Print(args ...any)
- func (s *Logger) Printf(format string, args ...any)
- func (s *Logger) Warn(args ...any)
- func (s *Logger) Warnf(format string, args ...any)
- func (s *Logger) WithError(err error) *Logger
- func (s *Logger) WithField(key string, value any) *Logger
- func (s *Logger) WithFields(fields map[string]any) *Logger
- type Zerolog
- func (zl *Zerolog) API(method, path, remoteAddr string, statusCode int, duration time.Duration, ...)
- func (zl *Zerolog) Benchmark(name string, duration time.Duration)
- func (zl *Zerolog) Failure(msg string)
- func (zl *Zerolog) Success(msg string)
- func (zl *Zerolog) WithContext(ctx map[string]any) *Zerolog
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrInvalidLogLevel = errors.New("invalid logger level")
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Level string
DateTimeLayout string
Colored bool
JSONFormat bool
UseEmoji bool
}
Config holds logger configuration
type Logger ¶
type Logger struct {
*Zerolog
}
Click to show internal directories.
Click to hide internal directories.