golog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 7 Imported by: 0

README

golog

This logging package provides a standardized way to initialize zerolog based loggers. It also integrates with the ctxerr package to propagate additional error contextual information up the call stack.

Usage

log.SetTimeFormat("2006-01-02 15:04:05")
log.SetLogLevel("info")
logger := log.New(true) // true for console writer
logger.Info().Str("some_key", "some_value").Msg("hello world")
func funcA(ctx context.Context, ...) {
    logger := log.Default.With().Str("some_base_id", id).Logger() // this info will be always displayed in the logs of child functions (propagated down the call stack)
    ctx = logger.WithContext(ctx) // put the logger into the context
    if err := funcB(ctx); err != nil {
        logger.Error().Ctx(ctxerr.Ctx(ctx, err)).Str("document", largeDocument).Msg("an error occurred") // stuff the context with the context error we got from down the call stack
    }
    ...
}

func funcB(ctx context.Context) error {
    ...
    if err := funcC(ctx); err != nil {
        return ctxerr.With(err, map[string]any{"manifest": largeManifest}) // additional contextual information to include only when there's an error and propagate it up the call stack
    }
    return nil
}

Note:

  • It's important to use the ctxerr.Ctx(ctx, err) function to add context to the logger and ctxerr.With(err, map[string]any{...}) to propagate additional contextual information up the call stack.
  • SetTimeFormat must be called before New to take effect.

Documentation

Overview

Package golog provides a standardized way to initialize github.com/rs/zerolog based loggers.

Index

Constants

This section is empty.

Variables

View Source
var Default zerolog.Logger

Default is the default global logger.

Functions

func New

func New(consoleWriter bool) zerolog.Logger

New creates a new logger instance.

func SetLogLevel

func SetLogLevel(l string)

SetLogLevel sets the log level for all logger instances. NOTE: Can be called at any time to change the log level.

func SetTimeFormat

func SetTimeFormat(format string)

SetTimeFormat sets the time format for all logger instances. NOTE: Must be called before `New` to take effect.

Types

This section is empty.

Jump to

Keyboard shortcuts

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