zlog/log_example_test.go

204 lines
3.7 KiB
Go
Raw Permalink Normal View History

2017-05-12 05:24:39 +00:00
package zerolog_test
import (
"errors"
stdlog "log"
2017-05-12 05:24:39 +00:00
"os"
2017-05-20 05:43:10 +00:00
"time"
2017-05-12 05:24:39 +00:00
"github.com/rs/zerolog"
)
func ExampleNew() {
log := zerolog.New(os.Stdout)
log.Info().Msg("hello world")
// Output: {"level":"info","message":"hello world"}
}
func ExampleLogger_With() {
log := zerolog.New(os.Stdout).
With().
Str("foo", "bar").
Logger()
log.Info().Msg("hello world")
// Output: {"level":"info","foo":"bar","message":"hello world"}
}
func ExampleLogger_Level() {
log := zerolog.New(os.Stdout).Level(zerolog.WarnLevel)
log.Info().Msg("filtered out message")
log.Error().Msg("kept message")
// Output: {"level":"error","message":"kept message"}
}
func ExampleLogger_Sample() {
log := zerolog.New(os.Stdout).Sample(2)
log.Info().Msg("message 1")
log.Info().Msg("message 2")
log.Info().Msg("message 3")
log.Info().Msg("message 4")
// Output: {"level":"info","sample":2,"message":"message 2"}
// {"level":"info","sample":2,"message":"message 4"}
}
func ExampleLogger_Debug() {
log := zerolog.New(os.Stdout)
log.Debug().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
// Output: {"level":"debug","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Info() {
log := zerolog.New(os.Stdout)
log.Info().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
// Output: {"level":"info","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Warn() {
log := zerolog.New(os.Stdout)
log.Warn().
Str("foo", "bar").
Msg("a warning message")
// Output: {"level":"warn","foo":"bar","message":"a warning message"}
2017-05-12 05:24:39 +00:00
}
func ExampleLogger_Error() {
log := zerolog.New(os.Stdout)
log.Error().
Err(errors.New("some error")).
Msg("error doing something")
// Output: {"level":"error","error":"some error","message":"error doing something"}
}
func ExampleLogger_Write() {
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Logger()
stdlog.SetFlags(0)
stdlog.SetOutput(log)
stdlog.Print("hello world")
// Output: {"foo":"bar","message":"hello world"}
}
2017-05-12 05:24:39 +00:00
func ExampleLogger_Log() {
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Str("bar", "baz").
Msg("")
// Output: {"foo":"bar","bar":"baz"}
}
2017-05-18 07:10:45 +00:00
func ExampleEvent_Dict() {
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
).
Msg("hello world")
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
2017-05-18 07:10:45 +00:00
}
2017-05-20 05:25:37 +00:00
func ExampleEvent_Interface() {
log := zerolog.New(os.Stdout)
obj := struct {
Name string `json:"name"`
}{
Name: "john",
}
log.Log().
Str("foo", "bar").
2017-05-20 05:25:37 +00:00
Interface("obj", obj).
Msg("hello world")
// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
}
2017-05-20 05:43:10 +00:00
func ExampleEvent_Dur() {
2017-05-21 04:08:42 +00:00
d := time.Duration(10 * time.Second)
2017-05-20 05:43:10 +00:00
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
2017-05-21 04:08:42 +00:00
Dur("dur", d).
2017-05-20 05:43:10 +00:00
Msg("hello world")
2017-05-21 04:08:42 +00:00
// Output: {"foo":"bar","dur":10000,"message":"hello world"}
2017-05-20 05:43:10 +00:00
}
2017-05-18 07:10:45 +00:00
func ExampleContext_Dict() {
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
).Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
}
2017-05-20 05:25:37 +00:00
func ExampleContext_Interface() {
obj := struct {
Name string `json:"name"`
}{
Name: "john",
}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
2017-05-20 05:25:37 +00:00
Interface("obj", obj).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
}
2017-05-20 05:43:10 +00:00
func ExampleContext_Dur() {
2017-05-21 04:08:42 +00:00
d := time.Duration(10 * time.Second)
2017-05-20 05:43:10 +00:00
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
2017-05-21 04:08:42 +00:00
Dur("dur", d).
2017-05-20 05:43:10 +00:00
Logger()
log.Log().Msg("hello world")
2017-05-21 04:08:42 +00:00
// Output: {"foo":"bar","dur":10000,"message":"hello world"}
2017-05-20 05:43:10 +00:00
}