Skip to main content

zerolog.go

zerolog.go - Overview

This file defines a custom handler ZerologHandler that adapts the standard slog package to the zerolog package. It includes functions to handle log levels, attributes, and grouping for use with zerolog.

Detailed Documentation

ZerologHandler

  • Purpose: ZerologHandler is a custom handler that adapts slog to zerolog.
  • Structure:
    type ZerologHandler struct {
    logger *zerolog.Logger
    }

newZerologHandler

  • Purpose: Creates a new ZerologHandler.

  • Parameters:

    • logger (*zerolog.Logger): A pointer to a zerolog.Logger instance.
  • Returns:

    • *ZerologHandler: A pointer to the newly created ZerologHandler.
    func newZerologHandler(logger *zerolog.Logger) *ZerologHandler

Handle

  • Purpose: Implements the slog.Handler interface. This method handles the actual logging by converting the slog.Record to a zerolog event.
  • Parameters:
    • _ (context.Context): The context (not used in the implementation).
    • record (slog.Record): The log record containing the log level, attributes, and message.
  • Returns:
    • error: An error, if any occurred during the logging process.
  • Note:
    • The //nolint:gocritic comment indicates that a gocritic linter warning is being ignored for the slog.Record struct, possibly due to a hugeParam issue, but it's acknowledged that the interface is from the standard library and not under direct control.
    func (h *ZerologHandler) Handle(_ context.Context, record slog.Record) error

Enabled

  • Purpose: Implements the slog.Handler interface. Determines if a given log level is enabled for this handler.
  • Parameters:
    • _ (context.Context): The context (not used in the implementation).
    • level (slog.Level): The log level to check.
  • Returns:
    • bool: true if the log level is enabled; otherwise, false.
    func (h *ZerologHandler) Enabled(_ context.Context, level slog.Level) bool

WithAttrs

  • Purpose: Adds attributes to the log event. Implements the slog.Handler interface.
  • Parameters:
    • attrs ([]slog.Attr): A slice of slog.Attr attributes to add.
  • Returns:
    • slog.Handler: A new ZerologHandler with the added attributes.
    func (h *ZerologHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithGroup

  • Purpose: Returns a handler for a group of logs. Implements the slog.Handler interface.
  • Parameters:
    • name (string): The name of the group.
  • Returns:
    • slog.Handler: A new ZerologHandler for the specified group.
    func (h *ZerologHandler) WithGroup(name string) slog.Handler

addAttrToZerolog

  • Purpose: A generic function to add a slog.Attr to either a zerolog.Event or zerolog.Context.
  • Parameters:
    • attr (slog.Attr): The attribute to add.
    • target (T): The target object to add the attribute to (either zerolog.Event or zerolog.Context). T is constrained by an interface including methods like Str, Int64, Float64, etc.
  • Returns:
    • T: The modified target object with the attribute added.
    func addAttrToZerolog[T interface {
    Str(string, string) T
    Int64(string, int64) T
    Uint64(string, uint64) T
    Float64(string, float64) T
    Bool(string, bool) T
    Err(error) T
    Time(string, time.Time) T
    Dur(string, time.Duration) T
    Interface(string, any) T
    AnErr(key string, err error) T
    }](attr slog.Attr, target T) T

toZerologLevel

  • Purpose: Maps slog levels to zerolog levels.
  • Parameters:
    • level (slog.Level): The slog level to convert.
  • Returns:
    • zerolog.Level: The corresponding zerolog level.
    func toZerologLevel(level slog.Level) zerolog.Level

Code Examples

None.

Getting Started Relevance