golog

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 8 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

View Source
const CensoredFieldPlaceholder = "***"

CensoredFieldPlaceholder is the default value used to replace censored fields.

Variables

View Source
var Default zerolog.Logger

Default is the default global logger.

Functions

func New

func New(consoleWriter bool, out io.Writer) zerolog.Logger

New creates a new logger instance. If out is nil, os.Stdout will be used.

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.

func WithCensoredSecretFields added in v1.1.0

func WithCensoredSecretFields(ctx zerolog.Context, rootName string, v any) zerolog.Context

WithCensoredSecretFields returns a logger context with all fields of struct v that have a `secret:"true"` tag censored and replaced with CensoredFieldPlaceholder. Nested structs are supported. The rootName is used as a prefix for all field names.

Example:

type Config struct {
    Username string `json:"username"`
    Password string `json:"password" secret:"true"`
}
cfg := Config{Username: "admin", Password: "s3cr3t"}
ctx := WithCensoredSecretFields(logger.With(), "config", cfg)
logger := ctx.Logger()

This will log:

{"config.Username":"admin","config.Password":"***"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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