Documentation ¶
Overview ¶
Example ¶
This example uses command-line flags to demonstrate various outputs depending on the chosen log level.
package main import ( "flag" "time" "github.com/rs/zerolog" "github.com/dgoujard/rfc2136_web_api/util/logger" ) // 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() *logger.Logger { zerolog.TimeFieldFormat = "" zerolog.TimestampFunc = func() time.Time { return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC) } return logger.NewConsole(true) } func main() { l := 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) } l.Debug().Msg("This message appears only when log level set to Debug") l.Info().Msg("This message appears when log level set to Debug or Info") if e := l.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 ¶
- type Logger
- func (l *Logger) Ctx(ctx context.Context) *Logger
- func (l *Logger) Debug() *zerolog.Event
- func (l *Logger) Error() *zerolog.Event
- func (l *Logger) Fatal() *zerolog.Event
- func (l *Logger) Hook(h zerolog.Hook) zerolog.Logger
- func (l *Logger) Info() *zerolog.Event
- func (l *Logger) Level(level zerolog.Level) zerolog.Logger
- func (l *Logger) Log() *zerolog.Event
- func (l *Logger) Output(w io.Writer) zerolog.Logger
- func (l *Logger) Panic() *zerolog.Event
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Sample(s zerolog.Sampler) zerolog.Logger
- func (l *Logger) Warn() *zerolog.Event
- func (l *Logger) With() zerolog.Context
- func (l *Logger) WithLevel(level zerolog.Level) *zerolog.Event
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func NewConsole ¶
func (*Logger) Ctx ¶
Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.
func (*Logger) Debug ¶
Debug starts a new message with debug level.
You must call Msg on the returned event in order to send the event.
func (*Logger) Error ¶
Error starts a new message with error level.
You must call Msg on the returned event in order to send the event.
func (*Logger) 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.
func (*Logger) Info ¶
Info starts a new message with info level.
You must call Msg on the returned event in order to send the event.
func (*Logger) 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.
func (*Logger) 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 (*Logger) Print ¶
func (l *Logger) Print(v ...interface{})
Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.
func (*Logger) Printf ¶
Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Warn ¶
Warn starts a new message with warn level.
You must call Msg on the returned event in order to send the event.