zlog/log_example_test.go

555 lines
11 KiB
Go
Raw Normal View History

// +build !binary_log
2022-03-20 19:19:42 +00:00
package zlog_test
2017-05-12 05:24:39 +00:00
import (
"errors"
"fmt"
stdlog "log"
"net"
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
2022-11-03 15:18:09 +00:00
"tuxpa.in/a/zlog"
2017-05-12 05:24:39 +00:00
)
func ExampleNew() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-12 05:24:39 +00:00
log.Info().Msg("hello world")
// Output: {"level":"info","message":"hello world"}
}
func ExampleLogger_With() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).
2017-05-12 05:24:39 +00:00
With().
Str("foo", "bar").
Logger()
log.Info().Msg("hello world")
// Output: {"level":"info","foo":"bar","message":"hello world"}
}
func ExampleLogger_Level() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).Level(zlog.WarnLevel)
2017-05-12 05:24:39 +00:00
log.Info().Msg("filtered out message")
log.Error().Msg("kept message")
// Output: {"level":"error","message":"kept message"}
}
func ExampleLogger_Sample() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).Sample(&zlog.BasicSampler{N: 2})
2017-05-12 05:24:39 +00:00
log.Info().Msg("message 1")
log.Info().Msg("message 2")
log.Info().Msg("message 3")
log.Info().Msg("message 4")
// Output: {"level":"info","message":"message 1"}
// {"level":"info","message":"message 3"}
2017-05-12 05:24:39 +00:00
}
2017-12-01 17:52:37 +00:00
type LevelNameHook struct{}
2022-03-20 19:19:42 +00:00
func (h LevelNameHook) Run(e *zlog.Event, l zlog.Level, msg string) {
if l != zlog.NoLevel {
2017-12-01 17:52:37 +00:00
e.Str("level_name", l.String())
} else {
e.Str("level_name", "NoLevel")
}
}
type MessageHook string
2022-03-20 19:19:42 +00:00
func (h MessageHook) Run(e *zlog.Event, l zlog.Level, msg string) {
2017-12-01 17:52:37 +00:00
e.Str("the_message", msg)
}
func ExampleLogger_Hook() {
var levelNameHook LevelNameHook
var messageHook MessageHook = "The message"
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).Hook(levelNameHook).Hook(messageHook)
2017-12-01 17:52:37 +00:00
log.Info().Msg("hello world")
// Output: {"level":"info","level_name":"info","the_message":"hello world","message":"hello world"}
}
2017-09-02 02:56:35 +00:00
func ExampleLogger_Print() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-09-02 02:56:35 +00:00
log.Print("hello world")
// Output: {"level":"debug","message":"hello world"}
}
func ExampleLogger_Printf() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-09-02 02:56:35 +00:00
log.Printf("hello %s", "world")
// Output: {"level":"debug","message":"hello world"}
}
2019-11-04 19:39:22 +00:00
func ExampleLogger_Trace() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2019-11-04 19:39:22 +00:00
log.Trace().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
// Output: {"level":"trace","foo":"bar","n":123,"message":"hello world"}
}
2017-05-12 05:24:39 +00:00
func ExampleLogger_Debug() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-12 05:24:39 +00:00
log.Debug().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
// Output: {"level":"debug","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Info() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-12 05:24:39 +00:00
log.Info().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
// Output: {"level":"info","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Warn() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-12 05:24:39 +00:00
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() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-12 05:24:39 +00:00
log.Error().
Err(errors.New("some error")).
Msg("error doing something")
// Output: {"level":"error","error":"some error","message":"error doing something"}
}
func ExampleLogger_WithLevel() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2022-03-20 19:19:42 +00:00
log.WithLevel(zlog.InfoLevel).
Msg("hello world")
// Output: {"level":"info","message":"hello world"}
}
func ExampleLogger_Write() {
2022-03-20 19:19:42 +00:00
log := zlog.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() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-12 05:24:39 +00:00
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() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-18 07:10:45 +00:00
log.Log().
Str("foo", "bar").
2022-03-20 19:19:42 +00:00
Dict("dict", zlog.Dict().
2017-05-18 07:10:45 +00:00
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
}
type User struct {
Name string
Age int
Created time.Time
}
2022-03-20 19:19:42 +00:00
func (u User) MarshalZerologObject(e *zlog.Event) {
e.Str("name", u.Name).
Int("age", u.Age).
Time("created", u.Created)
}
type Price struct {
val uint64
prec int
unit string
}
2022-03-20 19:19:42 +00:00
func (p Price) MarshalZerologObject(e *zlog.Event) {
denom := uint64(1)
for i := 0; i < p.prec; i++ {
denom *= 10
}
result := []byte(p.unit)
result = append(result, fmt.Sprintf("%d.%d", p.val/denom, p.val%denom)...)
e.Str("price", string(result))
}
2017-07-26 07:14:43 +00:00
type Users []User
2022-03-20 19:19:42 +00:00
func (uu Users) MarshalZerologArray(a *zlog.Array) {
2017-07-26 07:14:43 +00:00
for _, u := range uu {
a.Object(u)
}
}
func ExampleEvent_Array() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-07-26 07:14:43 +00:00
log.Log().
Str("foo", "bar").
2022-03-20 19:19:42 +00:00
Array("array", zlog.Arr().
2017-07-26 07:14:43 +00:00
Str("baz").
Int(1).
2022-03-20 19:19:42 +00:00
Dict(zlog.Dict().
Str("bar", "baz").
Int("n", 1),
),
2017-07-26 07:14:43 +00:00
).
Msg("hello world")
// Output: {"foo":"bar","array":["baz",1,{"bar":"baz","n":1}],"message":"hello world"}
2017-07-26 07:14:43 +00:00
}
func ExampleEvent_Array_object() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-07-26 07:14:43 +00:00
2022-03-20 19:19:42 +00:00
// Users implements zlog.LogArrayMarshaler
2017-07-26 07:14:43 +00:00
u := Users{
User{"John", 35, time.Time{}},
User{"Bob", 55, time.Time{}},
}
log.Log().
Str("foo", "bar").
Array("users", u).
Msg("hello world")
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleEvent_Object() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2022-03-20 19:19:42 +00:00
// User implements zlog.LogObjectMarshaler
u := User{"John", 35, time.Time{}}
log.Log().
Str("foo", "bar").
Object("user", u).
Msg("hello world")
// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
}
func ExampleEvent_EmbedObject() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
price := Price{val: 6449, prec: 2, unit: "$"}
log.Log().
Str("foo", "bar").
EmbedObject(price).
Msg("hello world")
// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
}
2017-05-20 05:25:37 +00:00
func ExampleEvent_Interface() {
2022-03-20 19:19:42 +00:00
log := zlog.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 := 10 * time.Second
2017-05-20 05:43:10 +00:00
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-05-20 05:43:10 +00:00
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-07-25 19:50:35 +00:00
func ExampleEvent_Durs() {
d := []time.Duration{
10 * time.Second,
20 * time.Second,
2017-07-25 19:50:35 +00:00
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
2017-07-25 19:50:35 +00:00
log.Log().
Str("foo", "bar").
Durs("durs", d).
Msg("hello world")
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
}
func ExampleEvent_Fields_map() {
fields := map[string]interface{}{
"bar": "baz",
"n": 1,
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Fields(fields).
Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleEvent_Fields_slice() {
fields := []interface{}{
"bar", "baz",
"n", 1,
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Fields(fields).
Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
2017-05-18 07:10:45 +00:00
func ExampleContext_Dict() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
2017-05-18 07:10:45 +00:00
Str("foo", "bar").
2022-03-20 19:19:42 +00:00
Dict("dict", zlog.Dict().
2017-05-18 07:10:45 +00:00
Str("bar", "baz").
Int("n", 1),
).Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
}
2017-07-26 07:14:43 +00:00
func ExampleContext_Array() {
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
2017-07-26 07:14:43 +00:00
Str("foo", "bar").
2022-03-20 19:19:42 +00:00
Array("array", zlog.Arr().
2017-07-26 07:14:43 +00:00
Str("baz").
Int(1),
).Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
}
func ExampleContext_Array_object() {
2022-03-20 19:19:42 +00:00
// Users implements zlog.LogArrayMarshaler
2017-07-26 07:14:43 +00:00
u := Users{
User{"John", 35, time.Time{}},
User{"Bob", 55, time.Time{}},
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
2017-07-26 07:14:43 +00:00
Str("foo", "bar").
Array("users", u).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleContext_Object() {
2022-03-20 19:19:42 +00:00
// User implements zlog.LogObjectMarshaler
u := User{"John", 35, time.Time{}}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
Str("foo", "bar").
Object("user", u).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
}
func ExampleContext_EmbedObject() {
price := Price{val: 6449, prec: 2, unit: "$"}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
Str("foo", "bar").
EmbedObject(price).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
}
2017-05-20 05:25:37 +00:00
func ExampleContext_Interface() {
obj := struct {
Name string `json:"name"`
}{
Name: "john",
}
2022-03-20 19:19:42 +00:00
log := zlog.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 := 10 * time.Second
2017-05-20 05:43:10 +00:00
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
2017-05-20 05:43:10 +00:00
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
}
2017-07-25 19:50:35 +00:00
func ExampleContext_Durs() {
d := []time.Duration{
10 * time.Second,
20 * time.Second,
2017-07-25 19:50:35 +00:00
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
2017-07-25 19:50:35 +00:00
Str("foo", "bar").
Durs("durs", d).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
}
func ExampleContext_IPAddr() {
hostIP := net.IP{192, 168, 0, 100}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
IPAddr("HostIP", hostIP).
Logger()
log.Log().Msg("hello world")
// Output: {"HostIP":"192.168.0.100","message":"hello world"}
}
func ExampleContext_IPPrefix() {
route := net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
IPPrefix("Route", route).
Logger()
log.Log().Msg("hello world")
// Output: {"Route":"192.168.0.0/24","message":"hello world"}
}
func ExampleContext_MACAddr() {
mac := net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
MACAddr("hostMAC", mac).
Logger()
log.Log().Msg("hello world")
// Output: {"hostMAC":"00:14:22:01:23:45","message":"hello world"}
}
func ExampleContext_Fields_map() {
fields := map[string]interface{}{
"bar": "baz",
"n": 1,
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
Str("foo", "bar").
Fields(fields).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_Fields_slice() {
fields := []interface{}{
"bar", "baz",
"n", 1,
}
2022-03-20 19:19:42 +00:00
log := zlog.New(os.Stdout).With().
Str("foo", "bar").
Fields(fields).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}