zerolog

package module
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 7 Imported by: 2

README

zerolog

Go Reference codecov

Zerolog adapter for darvaza.org/slog. Wraps a *zerolog.Logger to implement the slog.Logger interface, providing blazing-fast, zero-allocation JSON logging.

Installation

go get darvaza.org/slog/handlers/zerolog

Quick Start

import (
    "os"
    "github.com/rs/zerolog"
    slogzerolog "darvaza.org/slog/handlers/zerolog"
)

// Create zerolog logger
zlogger := zerolog.New(os.Stdout).With().Timestamp().Logger()

// For pretty console output during development
// console := zerolog.ConsoleWriter{Out: os.Stdout}
// zlogger := zerolog.New(console).With().Timestamp().Logger()

// Create slog adapter
slogLogger := slogzerolog.New(&zlogger)

// Use with slog interface
slogLogger.Info().
    WithField("service", "api").
    WithField("version", "1.0.0").
    WithField("request_id", "abc-123").
    Print("Service started")

Features

  • Zero-allocation JSON encoding for maximum performance
  • One of the fastest structured loggers for Go
  • Clean, minimal JSON output
  • Console writer available for development
  • Excellent for high-throughput applications
  • Immutable logger instances ensure thread-safe field management

Output Example

{
  "level":"info",
  "service":"api",
  "version":"1.0.0",
  "request_id":"abc-123",
  "time":"2023-01-01T12:00:00Z",
  "message":"Service started"
}

Performance Notes

  • Use JSON output (not ConsoleWriter) in production
  • Zerolog is optimized for JSON encoding speed
  • Fields are encoded directly without intermediate allocations
  • Ideal for services with high log volumes

Documentation

Documentation

Overview

Package zerolog provides a slog.Logger adaptor using a github.com/rs/zerolog Logger as backend.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(logger *zerolog.Logger) slog.Logger

New creates a slog.Logger adaptor using a zerolog as backend, if one was passed.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is an adaptor for using github.com/rs/zerolog as slog.Logger.

func (*Logger) Debug

func (zl *Logger) Debug() slog.Logger

Debug returns a new Event Context set to add entries as level Debug.

func (*Logger) Enabled

func (zl *Logger) Enabled() bool

Enabled tells if the underlying logger is enabled or not.

func (*Logger) Error

func (zl *Logger) Error() slog.Logger

Error returns a new Event Context set to add entries as level Error.

func (*Logger) Fatal

func (zl *Logger) Fatal() slog.Logger

Fatal returns a new Event Context set to add entries as level Fatal.

func (*Logger) Info

func (zl *Logger) Info() slog.Logger

Info returns a new Event Context set to add entries as level Info.

func (*Logger) Level added in v0.6.2

func (zl *Logger) Level() slog.LogLevel

Level returns the current log level. Exposed for testing only.

func (*Logger) Panic

func (zl *Logger) Panic() slog.Logger

Panic returns a new Event Context set to add entries as level Panic.

func (*Logger) Print

func (zl *Logger) Print(args ...any)

Print adds a log entry with arguments handled in the manner of fmt.Print.

func (*Logger) Printf

func (zl *Logger) Printf(format string, args ...any)

Printf adds a log entry with arguments handled in the manner of fmt.Printf.

func (*Logger) Println

func (zl *Logger) Println(args ...any)

Println adds a log entry with arguments handled in the manner of fmt.Println.

func (*Logger) Warn

func (zl *Logger) Warn() slog.Logger

Warn returns a new Event Context set to add entries as level Warn.

func (*Logger) WithEnabled

func (zl *Logger) WithEnabled() (slog.Logger, bool)

WithEnabled tells if the logger would log or not

func (*Logger) WithField

func (zl *Logger) WithField(label string, value any) slog.Logger

WithField adds a field to the Event Context

func (*Logger) WithFields

func (zl *Logger) WithFields(fields map[string]any) slog.Logger

WithFields adds fields to the Event Context

func (*Logger) WithLevel

func (zl *Logger) WithLevel(level slog.LogLevel) slog.Logger

WithLevel returns a new Event Context set to add entries to the specified level.

func (*Logger) WithStack

func (zl *Logger) WithStack(skip int) slog.Logger

WithStack attaches a call stack to the Event Context

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL