logger

package
v0.53.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package logger has helpers to setup a zerolog.Logger

https://github.com/rs/zerolog

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogErrorStackViaPkgErrors added in v0.51.0

func LogErrorStackViaPkgErrors(p bool)

LogErrorStackViaPkgErrors is a convenience function to set the zerolog ErrorStackMarshaler global variable. If true, writes error stacks for logs using "github.com/pkg/errors". If false, will use the internal errs.Op stack instead of "github.com/pkg/errors".

func New

func New(w io.Writer, lvl zerolog.Level, withTimestamp bool) zerolog.Logger

New is a convenience function to initialize a zerolog.Logger with an initial minimum accepted level and timestamp (if true) for a given io.Writer.

func NewWithGCPHook

func NewWithGCPHook(w io.Writer, lvl zerolog.Level, withTimestamp bool) zerolog.Logger

NewWithGCPHook is a convenience function to initialize a zerolog.Logger with an initial minimum accepted level and timestamp (if true) for a given io.Writer. In addition, it adds a Google Cloud Platform (GCP) Severity Hook. Zerolog by default outputs structured logs with "level":"error" as its leveling. Google Cloud, as an example, expects "severity","ERROR" for its leveling. This hook will add severity to each message.

Example
lgr := NewWithGCPHook(os.Stdout, zerolog.DebugLevel, false)
lgr.Trace().Msg("Trace is lower than Debug, this message is filtered out")
lgr.Debug().Msg("This is a log at the Debug level")

zerolog.SetGlobalLevel(zerolog.ErrorLevel)
lgr.Debug().Msg("Logging level raised to Error, Debug is lower than Error, this message is filtered out")
lgr.Error().Msg("This is a log at the Error level")

zerolog.SetGlobalLevel(zerolog.TraceLevel)
lgr.Trace().Msg("Setting Global level will not impact minimum set to logger, this trace message will still be filtered out")
lgr.Debug().Msg("Logging level raised all the way down to Trace level, Debug is higher than Trace, this will log")
Output:

{"level":"debug","severity":"DEBUG","message":"This is a log at the Debug level"}
{"level":"error","severity":"ERROR","message":"This is a log at the Error level"}
{"level":"debug","severity":"DEBUG","message":"Logging level raised all the way down to Trace level, Debug is higher than Trace, this will log"}

Types

type GCPSeverityHook

type GCPSeverityHook struct{}

The GCPSeverityHook struct satisfies the zerolog.Hook interface as it has the Run method defined with the appropriate parameters

func (GCPSeverityHook) Run

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

Run method satisfies zerolog.Hook interface and adds a severity level to all logs, given zerolog.Level passed in. Zerolog levels are mapped to log levels recognized by GCP

Jump to

Keyboard shortcuts

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