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 ( "context" "flag" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } 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(context.Background()).Msg("This message appears only when log level set to Debug") log.Info(context.Background()).Msg("This message appears when log level set to Debug or Info") if e := log.Debug(context.Background()); 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 ¶
- func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler
- func Ctx(ctx context.Context) *zerolog.Logger
- func Debug(ctx context.Context) *zerolog.Event
- func DisableDebug()
- func EnableDebug()
- func Error(ctx context.Context) *zerolog.Event
- func Fatal() *zerolog.Event
- func FromRequest(r *http.Request) *zerolog.Logger
- func HeadersHandler(headers []string) func(next http.Handler) http.Handler
- func Info(ctx context.Context) *zerolog.Event
- func Level(ctx context.Context, level zerolog.Level) *zerolog.Logger
- func Log(ctx context.Context) *zerolog.Event
- func Logger() *zerolog.Logger
- func MethodHandler(fieldKey string) func(next http.Handler) http.Handler
- func NewHandler(getLogger func() *zerolog.Logger) func(http.Handler) http.Handler
- func Panic() *zerolog.Event
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func RefererHandler(fieldKey string) func(next http.Handler) http.Handler
- func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler
- func RequestHandler(fieldKey string) func(next http.Handler) http.Handler
- func RequestIDHandler(fieldKey string) func(next http.Handler) http.Handler
- func SetLevel(level string)
- func SetLogger(l *zerolog.Logger)
- func URLHandler(fieldKey string) func(next http.Handler) http.Handler
- func UserAgentHandler(fieldKey string) func(next http.Handler) http.Handler
- func Warn(ctx context.Context) *zerolog.Event
- func With() zerolog.Context
- func WithContext(ctx context.Context, update func(c zerolog.Context) zerolog.Context) context.Context
- func ZapLogger() *zap.Logger
- type StdLogWrapper
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessHandler ¶ added in v0.0.5
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler
AccessHandler returns a handler that call f after each request.
func Ctx ¶
Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.
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 ( "context" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Debug(context.Background()).Msg("hello world") }
Output: {"level":"debug","time":1199811905,"message":"hello world"}
func DisableDebug ¶ added in v0.11.0
func DisableDebug()
DisableDebug tells the logger to use stdout and json output.
func EnableDebug ¶ added in v0.11.0
func EnableDebug()
EnableDebug tells the logger to use stdout and pretty print output.
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 ( "context" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Error(context.Background()).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" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } 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). Msg("Cannot start") // Outputs: {"level":"fatal","time":1199811905,"error":"a repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"} }
Output:
func FromRequest ¶ added in v0.0.2
FromRequest gets the logger in the request's context. This is a shortcut for log.Ctx(r.Context())
func HeadersHandler ¶ added in v0.5.1
HeadersHandler adds the provided set of header keys to the log context.
https://tools.ietf.org/html/rfc7239 https://en.wikipedia.org/wiki/X-Forwarded-For https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
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 ( "context" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Info(context.Background()).Msg("hello world") }
Output: {"level":"info","time":1199811905,"message":"hello world"}
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 ( "context" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Log(context.Background()).Msg("hello world") }
Output: {"time":1199811905,"message":"hello world"}
func MethodHandler ¶ added in v0.0.5
MethodHandler adds the request method as a field to the context's logger using fieldKey as field key.
func NewHandler ¶ added in v0.0.5
NewHandler injects log into requests context.
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.
func 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
package main import ( "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Print("hello world") }
Output: {"level":"debug","time":1199811905,"message":"hello world"}
func 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
package main import ( "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Printf("hello %s", "world") }
Output: {"level":"debug","time":1199811905,"message":"hello world"}
func RefererHandler ¶ added in v0.0.5
RefererHandler adds the request's referer as a field to the context's logger using fieldKey as field key.
func RemoteAddrHandler ¶ added in v0.0.5
RemoteAddrHandler adds the request's remote address as a field to the context's logger using fieldKey as field key.
func RequestHandler ¶ added in v0.0.5
RequestHandler adds the request method and URL as a field to the context's logger using fieldKey as field key.
func RequestIDHandler ¶ added in v0.0.5
RequestIDHandler adds the request's id as a field to the context's logger using fieldKey as field key.
func SetLevel ¶ added in v0.0.3
func SetLevel(level string)
SetLevel sets the minimum global log level. Options are 'debug' 'info' 'warn' and 'error'. Defaults to 'debug'
Example ¶
package main import ( "context" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.SetLevel("info") log.Debug(context.Background()).Msg("Debug") log.Info(context.Background()).Msg("Debug or Info") log.SetLevel("warn") log.Debug(context.Background()).Msg("Debug") log.Info(context.Background()).Msg("Debug or Info") log.Warn(context.Background()).Msg("Debug or Info or Warn") log.SetLevel("error") log.Debug(context.Background()).Msg("Debug") log.Info(context.Background()).Msg("Debug or Info") log.Warn(context.Background()).Msg("Debug or Info or Warn") log.Error(context.Background()).Msg("Debug or Info or Warn or Error") log.SetLevel("default-fall-through") log.Debug(context.Background()).Msg("Debug") }
Output: {"level":"info","time":1199811905,"message":"Debug or Info"} {"level":"warn","time":1199811905,"message":"Debug or Info or Warn"} {"level":"error","time":1199811905,"message":"Debug or Info or Warn or Error"} {"level":"debug","time":1199811905,"message":"Debug"}
func URLHandler ¶ added in v0.0.5
URLHandler adds the requested URL as a field to the context's logger using fieldKey as field key.
func UserAgentHandler ¶ added in v0.0.5
UserAgentHandler adds the request's user-agent as a field to the context's logger using fieldKey as field key.
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 ( "context" "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() log.Warn(context.Background()).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 ¶
package main import ( "time" "github.com/rs/zerolog" "github.com/pomerium/pomerium/internal/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, 5, 0, time.UTC) } log.DisableDebug() } func main() { setup() sublog := log.With().Str("foo", "bar").Logger() sublog.Debug().Msg("hello world") }
Output: {"level":"debug","foo":"bar","time":1199811905,"message":"hello world"}
Types ¶
type StdLogWrapper ¶ added in v0.0.3
StdLogWrapper can be used to wrap logs originating from the from std library's ErrorFunction argument in http.Serve and httputil.ReverseProxy.