Documentation
¶
Overview ¶
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.
package main
import (
"flag"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
// Default level for this example is info, unless debug flag is present
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() {
// Compute log output only if 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"}
Index ¶
- Variables
- func Ctx(ctx context.Context) *zerolog.Logger
- func Debug() *zerolog.Event
- func Err(err error) *zerolog.Event
- func Error() *zerolog.Event
- func Fatal() *zerolog.Event
- func Hook(h zerolog.Hook) zerolog.Logger
- func Info() *zerolog.Event
- func Level(level zerolog.Level) zerolog.Logger
- func Log() *zerolog.Event
- func Output(w io.Writer) zerolog.Logger
- func Panic() *zerolog.Event
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func Sample(s zerolog.Sampler) zerolog.Logger
- func Trace() *zerolog.Event
- func Warn() *zerolog.Event
- func With() zerolog.Context
- func WithLevel(level zerolog.Level) *zerolog.Event
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
Logger is the global logger.
Functions ¶
func Ctx ¶
Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.
Example ¶
Example of using the Ctx function in the log package to log with context
package main
import (
"context"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
type valueKeyType int
var valueKey valueKeyType = 42
var captainHook = zerolog.HookFunc(func(e *zerolog.Event, l zerolog.Level, msg string) {
e.Interface("key", e.GetCtx().Value(valueKey))
e.Bool("is_error", l > zerolog.ErrorLevel)
e.Int("msg_len", len(msg))
})
func main() {
setup()
hooked := log.Hook(captainHook)
ctx := context.WithValue(context.Background(), valueKey, "12345")
logger := hooked.With().Ctx(ctx).Logger()
log.Ctx(logger.WithContext(ctx)).Info().Msg("hello world")
}
Output: {"level":"info","time":1199811905,"key":"12345","is_error":false,"msg_len":11,"message":"hello world"}
func Debug ¶
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")
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Debug().Msg("hello world")
}
Output: {"level":"debug","time":1199811905,"message":"hello world"}
func Err ¶ added in v1.17.0
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.
package main
import (
"errors"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
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"}
func Error ¶
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")
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Error().Msg("hello world")
}
Output: {"level":"error","time":1199811905,"message":"hello world"}
func Fatal ¶
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")
package main
import (
"errors"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
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)
// Outputs: {"level":"fatal","time":1199811905,"error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
}
Output:
func Info ¶
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")
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Info().Msg("hello world")
}
Output: {"level":"info","time":1199811905,"message":"hello world"}
func Level ¶
Level creates a child logger with the minimum accepted level set to level.
Example ¶
Example of using the Level function to set the log level
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
// you have to assign the result of Level() to a new Logger and can't inline the level calls
// because they need a *Logger receiver
leveled := log.Level(zerolog.ErrorLevel)
leveled.Info().Msg("hello world")
leveled.Error().Msg("I said HELLO")
}
Output: {"level":"error","time":1199811905,"message":"I said HELLO"}
func Log ¶
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"
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Log().Msg("hello world")
}
Output: {"time":1199811905,"message":"hello world"}
func Output ¶ added in v1.2.0
Output duplicates the global logger and sets w as its output.
Example ¶
Example of using the Output function in the log package to change the output destination
package main
import (
"bytes"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
out := &bytes.Buffer{}
tee := log.Output(out)
tee.Info().Msg("hello world")
written := out.Len()
log.Info().Int("bytes", written).Msg("wrote")
}
Output: {"level":"info","bytes":59,"time":1199811905,"message":"wrote"}
func Panic ¶
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.
Example ¶
Example of a log at a particular "level" (in this case, "panic")
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Panic().Msg("Cannot start")
// Outputs: {"level":"panic","time":1199811905,"message":"Cannot start"} then panics
}
Output:
func Print ¶ added in v1.3.0
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
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Print("hello world")
}
Output: {"level":"debug","time":1199811905,"message":"hello world"}
func Printf ¶ added in v1.3.0
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
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Printf("hello %s", "world")
}
Output: {"level":"debug","time":1199811905,"message":"hello world"}
func Sample ¶
Sample returns a logger with the s sampler.
Example ¶
Example of using the Sample function in the log package to set a sampler
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
sampled := log.Sample(&zerolog.BasicSampler{N: 2})
sampled.Info().Msg("hello world")
sampled.Info().Msg("I said, hello world")
sampled.Info().Msg("Can you here me now world")
}
Output: {"level":"info","time":1199811905,"message":"hello world"} {"level":"info","time":1199811905,"message":"Can you here me now world"}
func Trace ¶ added in v1.17.0
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")
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Trace().Msg("hello world")
}
Output: {"level":"trace","time":1199811905,"message":"hello world"}
func Warn ¶
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")
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
log.Warn().Msg("hello world")
}
Output: {"level":"warn","time":1199811905,"message":"hello world"}
func With ¶
With creates a child logger with the field added to its context.
Example ¶
Example of using the With function to add context fields
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
// you have to assign the result of With() to a new Logger and can't inline the level calls
// because they need a *Logger receiver
augmented := log.With().Str("service", "myservice").Logger()
augmented.Info().Msg("hello world")
}
Output: {"level":"info","service":"myservice","time":1199811905,"message":"hello world"}
func WithLevel ¶ added in v1.4.0
WithLevel starts a new message with level.
You must call Msg on the returned event in order to send the event.
Example ¶
Example of using the WithLevel function to set the log level
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
zerolog.TimeFieldFormat = ""
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
func main() {
setup()
// you have to assign the result of Level() to a new Logger and can't inline the level calls
// because they need a *Logger receiver
event := log.WithLevel(zerolog.ErrorLevel)
event.Msg("taxes are due")
}
Output: {"level":"error","time":1199811905,"message":"taxes are due"}
Types ¶
This section is empty.