@@ -42,28 +42,36 @@ type zeroLogWrapper struct {
4242 * zerolog.Logger
4343}
4444
45- // NewNopLogger returns a new logger that does nothing.
46- func NewNopLogger () Logger {
47- // The custom nopLogger is about 3x faster than a zeroLogWrapper with zerolog.Nop().
48- return nopLogger {}
49- }
50-
5145// NewLogger returns a new logger that writes to the given destination.
5246//
5347// Typical usage from a main function is:
54- // logger := log.NewLogger(os.Stderr)
48+ //
49+ // logger := log.NewLogger(os.Stderr)
5550//
5651// Stderr is the typical destination for logs,
5752// so that any output from your application can still be piped to other processes.
58- func NewLogger (dst io.Writer ) Logger {
59- output := zerolog.ConsoleWriter {Out : dst , TimeFormat : time .Kitchen }
53+ func NewLogger (dst io.Writer , options ... Option ) Logger {
54+ logCfg := defaultConfig
55+ for _ , opt := range options {
56+ opt (& logCfg )
57+ }
58+
59+ output := dst
60+ if ! logCfg .OutputJSON {
61+ output = zerolog.ConsoleWriter {Out : dst , TimeFormat : time .Kitchen }
62+ }
63+
64+ if logCfg .Filter != nil {
65+ output = NewFilterWriter (output , logCfg .Filter )
66+ }
67+
6068 logger := zerolog .New (output ).With ().Timestamp ().Logger ()
61- return zeroLogWrapper {& logger }
62- }
6369
64- // NewLoggerWithKV is shorthand for NewLogger(dst).With(key, value).
65- func NewLoggerWithKV (dst io.Writer , key , value string ) Logger {
66- return NewLogger (dst ).With (key , value )
70+ if logCfg .Level != zerolog .NoLevel {
71+ logger = logger .Level (logCfg .Level )
72+ }
73+
74+ return zeroLogWrapper {& logger }
6775}
6876
6977// NewCustomLogger returns a new logger with the given zerolog logger.
@@ -101,33 +109,10 @@ func (l zeroLogWrapper) Impl() interface{} {
101109 return l .Logger
102110}
103111
104- // FilterKeys returns a new logger that filters out all key/value pairs that do not match the filter.
105- // This functions assumes that the logger is a zerolog.Logger, which is the case for the logger returned by log.NewLogger().
106- // NOTE: filtering has a performance impact on the logger.
107- func FilterKeys (logger Logger , filter FilterFunc ) Logger {
108- zl , ok := logger .Impl ().(* zerolog.Logger )
109- if ! ok {
110- panic ("logger is not a zerolog.Logger" )
111- }
112-
113- filteredLogger := zl .Hook (zerolog .HookFunc (func (e * zerolog.Event , lvl zerolog.Level , _ string ) {
114- // TODO(@julienrbrt) wait for https://github.com/rs/zerolog/pull/527 to be merged
115- // keys, err := e.GetKeys()
116- // if err != nil {
117- // panic(err)
118- // }
119-
120- keys := []string {}
121-
122- for _ , key := range keys {
123- if filter (key , lvl .String ()) {
124- e .Discard ()
125- break
126- }
127- }
128- }))
129-
130- return NewCustomLogger (filteredLogger )
112+ // NewNopLogger returns a new logger that does nothing.
113+ func NewNopLogger () Logger {
114+ // The custom nopLogger is about 3x faster than a zeroLogWrapper with zerolog.Nop().
115+ return nopLogger {}
131116}
132117
133118// nopLogger is a Logger that does nothing when called.
0 commit comments