zerologhook

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 License: MIT Imports: 5 Imported by: 0

README

zerolog-hook

Go Reference Go Report Card Test

A lightweight and performant caller info hook for zerolog that enriches log events with function name, package name, file name, and line number — with minimal runtime overhead.

⚡ Up to 3x faster than the built-in .Caller() method in zerolog. See Benchmarks.


Features

✅ Adds structured caller information:
    • Function name
    • Package name
    • File name
    • Line number

✅ Individually configurable fields

✅ Zero external dependencies

✅ Thread-safe caching for runtime function resolution


Installation

go get github.com/dings-things/zerolog-hook

Quick Start

import (
    "os"

    "github.com/rs/zerolog"
    zerohook "github.com/dings-things/zerolog-hook"
)

func main() {
    logger := zerolog.New(os.Stdout).
        With().
        Timestamp().
        Logger().
        Hook(zerohook.NewCallerHook(
            true,  // WithFunc
            true,  // WithFile
            true,  // WithLine
            true,  // WithPkg
        ))

    logger.Info().Msg("Hello from logger")
}

Sample output:

{
  "level": "info",
  "time": "2025-04-18T12:00:00Z",
  "func": "main",
  "file": "main.go",
  "line": 14,
  "pkg": "main",
  "message": "Hello from logger"
}

Use Cases

  • When you need structured log context for tracing and debugging
  • To replace zerolog’s .Caller() with a faster and more flexible alternative
  • In performance-sensitive environments (high-frequency log generation)

Benchmarks

Tested on:

OS:     Windows 10
CPU:    12th Gen Intel(R) Core(TM) i5-12600
Go:     go1.21+
go test -bench=. -benchmem
Method Ops/sec ns/op B/op allocs/op
zerolog.Caller() ~1.12 M 1014 880 7
zerolog-hook ~3.74 M 308.7 580 5

~3x faster and ~34% less memory usage than zerolog's built-in .Caller()


API

func NewCallerHook(
    withFunc bool,
    withFile bool,
    withLine bool,
    withPkg bool,
) zerolog.Hook
  • withFunc: include "func" field
  • withFile: include "file" field
  • withLine: include "line" field
  • withPkg: include "pkg" field

Testing

go test -v ./...

Unit and benchmark tests are included to validate correctness and performance.


License

MIT © 2025 dings-things


Contributing

Pull requests are welcome!
Please open an issue first for major changes or discussions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallerHook

type CallerHook struct {
	WithFunc bool // Include function name
	WithFile bool // Include filename
	WithLine bool // Include line number
	WithPkg  bool // Include package name
}

CallerHook is a Zerolog hook that automatically adds caller information (function name, package name, filename, and line number) to the log entry. Each field can be configured individually at the time of creation.

func NewCallerHook

func NewCallerHook(withFunc, withFile, withLine, withPkg bool) CallerHook

NewCallerHook creates a new instance of CallerHook

  • withFunc: whether to include the function name
  • withFile: whether to include the filename
  • withLine: whether to include the line number
  • withPkg: whether to include the package name

example:

logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
logger = logger.Hook(NewCallerHook(true, true, true, true))
logger.Info().Msg("test log")
// Output: {"level":"info","time":...,"func":"captureLog","file":"caller_hook_test.go","line":...,"pkg":"loggerhook_test","message":"test log"}

func (CallerHook) Run

func (h CallerHook) Run(e *zerolog.Event, level zerolog.Level, msg string)

Run is called when the hook is executed (implements zerolog.Hook interface)

Jump to

Keyboard shortcuts

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