zlog/log_example_test.go

190 lines
3.5 KiB
Go
Raw Normal View History

2017-05-12 05:24:39 +00:00
package zerolog_test
import (
"errors"
"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":"warning","foo":"bar","message":"a warning message"}
}
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_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() {
d := time.Duration(10 * time.Millisecond)
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Dur("dur", d, time.Second).
Msg("hello world")
// Output: {"foo":"bar","dur":0.01,"message":"hello world"}
}
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() {
d := time.Duration(10 * time.Millisecond)
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Dur("dur", d, time.Second).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","dur":0.01,"message":"hello world"}
}