Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

log

v1.31.0
go.imperva.dev/zerolog/log

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

40 matches

Top ↑

Package log provides a global logger for zerolog.

Example

This example uses command-line flags to demonstrate various outputs depending on the chosen log level.
{
	setup()
	debug := flag.Bool("debug", false, "sets log level to debug")

	flag.Parse()

	zerolog.SetGlobalLevel(zerolog.InfoLevel)
	if *debug {
		zerolog.SetGlobalLevel(zerolog.DebugLevel)
	}

	log.Debug().Msg("This message appears only when log level set to Debug")
	log.Info().Msg("This message appears when log level set to Debug or Info")

	if e := log.Debug(); e.Enabled() {

		value := "bar"
		e.Str("foo", value).Msg("some debug message")
	}

}
Output:
{"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}

Variables

Logger

var (

	// Logger is the global logger.
	Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
)

Functions

Ctx

func Ctx(ctx context.Context) *zerolog.Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

Debug

func Debug() *zerolog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "debug")
{
	setup()
	log.Debug().Msg("hello world")

}
Output:
{"level":"debug","time":1199811905,"message":"hello world"}

Err

func Err(err error) *zerolog.Event

Err starts a new message with error level with err as a field if not nil or with info level if err is nil.

You must call Msg on the returned event in order to send the event.

Example

Example of a conditional level based on the presence of an error.
{
	setup()
	err := errors.New("some error")
	log.Err(err).Msg("hello world")
	log.Err(nil).Msg("hello world")

}
Output:
{"level":"error","error":"some error","time":1199811905,"message":"hello world"}
{"level":"info","time":1199811905,"message":"hello world"}

Error

func Error() *zerolog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "error")
{
	setup()
	log.Error().Msg("hello world")

}
Output:
{"level":"error","time":1199811905,"message":"hello world"}

Fatal

func Fatal() *zerolog.Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "fatal")
{
	setup()
	err := errors.New("A repo man spends his life getting into tense situations")
	service := "myservice"

	log.Fatal().
		Err(err).
		Str("service", service).
		Msgf("Cannot start %s", service)

}

GetLevel

func GetLevel() zerolog.Level

GetLevel returns the current log level

Hook

func Hook(h zerolog.Hook) zerolog.Logger

Hook returns a logger with the h Hook.

Info

func Info() *zerolog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "info")
{
	setup()
	log.Info().Msg("hello world")

}
Output:
{"level":"info","time":1199811905,"message":"hello world"}

InterceptLogrusMessages

func InterceptLogrusMessages(writer *io.PipeWriter)

InitLogrusInterceptor initializes the logrus global logger with an expected format so that they can be intercepted and parsed by ParseLogrusMessages.

IsDebugEnabled

func IsDebugEnabled() bool

IsDebugEnabled returns whether or not debug or trace logging is enabled.

Level

func Level(level zerolog.Level) zerolog.Logger

Level creates a child logger with the minimum accepted level set to level.

Log

func Log() *zerolog.Event

Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

Example

Example of a log with no particular "level"
{
	setup()
	log.Log().Msg("hello world")

}
Output:
{"time":1199811905,"message":"hello world"}

Output

func Output(w io.Writer) zerolog.Logger

Output duplicates the global logger and sets w as its output.

Panic

func Panic() *zerolog.Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

ParseLogrusMessages

func ParseLogrusMessages(reader *io.PipeReader, handler zerolog.ParseLogrusMessageErrorHandler)

ParseLogrusMessages uses a reader paired with the output writer from InitLogrusInterceptor to read messages from the pipe in a blocking manner until the writer is closed.

Print

func Print(v ...interface{})

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

Example

Simple logging example using the Print function in the log package Note that both Print and Printf are at the debug log level by default
{
	setup()

	log.Print("hello world")

}
Output:
{"level":"debug","time":1199811905,"message":"hello world"}

Printf

func Printf(format string, v ...interface{})

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

Example

Simple logging example using the Printf function in the log package
{
	setup()

	log.Printf("hello %s", "world")

}
Output:
{"level":"debug","time":1199811905,"message":"hello world"}

ReplaceGlobal

func ReplaceGlobal(logger zerolog.Logger) (func(), zerolog.Logger)

ReplaceGlobal replaces the global logger and returns a function to retrieve the original logger.

Sample

func Sample(s zerolog.Sampler) zerolog.Logger

Sample returns a logger with the s sampler.

SetLevel

func SetLevel(level zerolog.Level)

SetLevel updates the minimum logging level for the logger.

Trace

func Trace() *zerolog.Event

Trace starts a new message with trace level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "trace")
{
	setup()
	log.Trace().Msg("hello world")

}
Output:
{"level":"trace","time":1199811905,"message":"hello world"}

Warn

func Warn() *zerolog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "warn")
{
	setup()
	log.Warn().Msg("hello world")

}
Output:
{"level":"warn","time":1199811905,"message":"hello world"}

With

func With() zerolog.Context

With creates a child logger with the field added to its context.

WithContext

func WithContext(ctx context.Context) context.Context

WithContext returns a copy of ctx with l associated. If an instance of Logger is already in the context, the context is not updated.

WithLevel

func WithLevel(level zerolog.Level) *zerolog.Event

WithLevel starts a new message with level.

You must call Msg on the returned event in order to send the event.

WouldLog

func WouldLog(level zerolog.Level) bool

WouldLog returns whether or not a mesage with the given level would be logged.

Dependency graph