-
Notifications
You must be signed in to change notification settings - Fork 1
/
options_example_test.go
98 lines (83 loc) · 2.74 KB
/
options_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package logger_test
import (
"errors"
"fmt"
"os"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
"github.com/secureworks/logger/log"
_ "github.com/secureworks/logger/logrus"
_ "github.com/secureworks/logger/zerolog"
)
func ExampleOption() {
// Options can be used to specificy behavior for the logger
// implementation beyond the values in the Config. For more examples
// see CustomOption.
optionFn := func(val interface{}) error {
ul, _ := val.(log.UnderlyingLogger)
logger, _ := ul.GetLogger().(*logrus.Logger)
logger.Formatter = &logrus.JSONFormatter{
PrettyPrint: true,
DisableTimestamp: true,
}
return nil
}
logger, _ := log.Open("logrus", &log.Config{Output: os.Stdout}, optionFn)
logger.Info().WithStr("test").Msg("test message here")
// Output: {
// "level": "info",
// "msg": "test message here"
// }
}
type SingleHook struct{}
func (h SingleHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
e.Bool("test_hook", true)
}
func ExampleCustomOption_singleValue() {
// CustomOption can be used to specificy behavior for the logger
// implementation beyond the values in the Config. For more examples
// see Option.
logger, _ := log.Open(
"zerolog",
&log.Config{Output: os.Stdout},
// Zerolog's Logger.Hook method is chainable (returns a new loger with
// the given hook attached), so CustomOption will reset the underlying
// logger to the result.
//
// See: https://pkg.go.dev/github.com/rs/zerolog#Logger.Hook
//
log.CustomOption("Hook", SingleHook{}),
)
logger.Info().Msg("test message here")
// Output: {"level":"info","test_hook":true,"message":"test message here"}
}
func ExampleCustomOption_withPossibleError() {
// If a CustomOption returns an error value that is not nil, that
// error bubbles up through log.Open.
loggerFailed, err := log.Open(
"zerolog",
&log.Config{Output: os.Stdout},
log.CustomOption("Sample", func() (zerolog.Sampler, error) {
return &zerolog.BasicSampler{N: 2}, errors.New("custom option failed")
}),
)
if loggerFailed == nil {
fmt.Printf("ERR: %q\n", err)
}
// If a CustomOption returns an error value that is nil, that value is
// ignored and the named method receives the given value(s).
loggerSuccess, _ := log.Open(
"zerolog",
&log.Config{Output: os.Stdout},
log.CustomOption("Sample", func() (zerolog.Sampler, error) {
return &zerolog.BasicSampler{N: 2}, nil
}),
)
loggerSuccess.Info().Msg("test success message 1")
loggerSuccess.Info().Msg("test success message 2")
loggerSuccess.Info().Msg("test success message 3")
loggerSuccess.Info().Msg("test success message 4")
// Output: ERR: "custom option failed"
// {"level":"info","message":"test success message 1"}
// {"level":"info","message":"test success message 3"}
}