@@ -16,62 +16,31 @@ import (
1616 "go.uber.org/zap/zapcore"
1717)
1818
19- type logConfig struct {
20- core zapcore.Core
21- cleanup func () error
22- err error
23- }
19+ type SyncFunc func () error
2420
2521type SinkOption func (* sinkConfig )
2622
27- // New creates a new log object with the provided configurations . If no sinks
28- // are provided, a no-op sink will be used. Returns the logger and a cleanup
23+ // New creates a new log object with the provided sinks . If no sinks are
24+ // provided, a no-op sink will be used. Returns the logger and a cleanup
2925// function that should be executed before the program exits.
30- func New (service string , configs ... logConfig ) (logr.Logger , func () error ) {
31- return NewWithCaller (service , false , configs ... )
26+ func New (service string , cores ... zapcore. Core ) (logr.Logger , SyncFunc ) {
27+ return NewWithCaller (service , false , cores ... )
3228}
3329
34- // NewWithCaller creates a new logger named after the specified service with the provided sink configurations. If
35- // addCaller is true, call site information will be attached to each emitted log message. (This behavior can be disabled
36- // on a per-sink basis using WithSuppressCaller.)
37- func NewWithCaller (service string , addCaller bool , configs ... logConfig ) (logr.Logger , func () error ) {
38- var cores []zapcore.Core
39- var cleanupFuncs []func () error
40-
41- // create cores for the logger
42- for _ , config := range configs {
43- if config .err != nil {
44- continue
45- }
46- cores = append (cores , config .core )
47- if config .cleanup != nil {
48- cleanupFuncs = append (cleanupFuncs , config .cleanup )
49- }
50- }
30+ // NewWithCaller creates a new logger named after the specified service with
31+ // the provided sinks. If addCaller is true, call site information will be
32+ // attached to each emitted log message. (This behavior can be disabled on a
33+ // per-sink basis using WithSuppressCaller.)
34+ func NewWithCaller (service string , addCaller bool , cores ... zapcore.Core ) (logr.Logger , SyncFunc ) {
5135 // create logger
5236 zapLogger := zap .New (zapcore .NewTee (cores ... ), zap .WithCaller (addCaller ))
53- cleanupFuncs = append (cleanupFuncs , zapLogger .Sync )
5437 logger := zapr .NewLogger (zapLogger ).WithName (service )
5538
56- // report the errors we encountered in the configs
57- for _ , config := range configs {
58- if config .err != nil {
59- logger .Error (config .err , "error configuring logger" )
60- }
61- }
62-
63- return logger , firstErrorFunc (cleanupFuncs ... )
39+ return logger , zapLogger .Sync
6440}
6541
66- // WithSentry adds sentry integration to the logger. This configuration may
67- // fail, in which case, sentry will not be added and execution will continue
68- // normally.
69- func WithSentry (opts sentry.ClientOptions , tags map [string ]string ) logConfig {
70- client , err := sentry .NewClient (opts )
71- if err != nil {
72- return logConfig {err : err }
73- }
74-
42+ // WithSentry adds sentry integration to the logger.
43+ func WithSentry (client * sentry.Client , tags map [string ]string ) zapcore.Core {
7544 // create sentry core
7645 cfg := zapsentry.Configuration {
7746 Tags : tags ,
@@ -81,16 +50,14 @@ func WithSentry(opts sentry.ClientOptions, tags map[string]string) logConfig {
8150 }
8251 core , err := zapsentry .NewCore (cfg , zapsentry .NewSentryClientFromClient (client ))
8352 if err != nil {
84- return logConfig {err : err }
53+ // NewCore should never fail because NewSentryClientFromClient
54+ // never returns an error and NewCore only returns an error if
55+ // the zapsentry.Configuration is invalid, which would indicate
56+ // a programmer error.
57+ panic (err )
8558 }
8659
87- return logConfig {
88- core : core ,
89- cleanup : func () error {
90- sentry .Flush (5 * time .Second )
91- return nil
92- },
93- }
60+ return core
9461}
9562
9663type sinkConfig struct {
@@ -102,8 +69,8 @@ type sinkConfig struct {
10269}
10370
10471// WithJSONSink adds a JSON encoded output to the logger.
105- func WithJSONSink (sink io.Writer , opts ... SinkOption ) logConfig {
106- return newCoreConfig (
72+ func WithJSONSink (sink io.Writer , opts ... SinkOption ) zapcore. Core {
73+ return newCore (
10774 zapcore .NewJSONEncoder (defaultEncoderConfig ()),
10875 zapcore .Lock (zapcore .AddSync (sink )),
10976 globalLogLevel ,
@@ -112,8 +79,8 @@ func WithJSONSink(sink io.Writer, opts ...SinkOption) logConfig {
11279}
11380
11481// WithConsoleSink adds a console-style output to the logger.
115- func WithConsoleSink (sink io.Writer , opts ... SinkOption ) logConfig {
116- return newCoreConfig (
82+ func WithConsoleSink (sink io.Writer , opts ... SinkOption ) zapcore. Core {
83+ return newCore (
11784 zapcore .NewConsoleEncoder (defaultEncoderConfig ()),
11885 zapcore .Lock (zapcore .AddSync (sink )),
11986 globalLogLevel ,
@@ -135,32 +102,23 @@ func defaultEncoderConfig() zapcore.EncoderConfig {
135102 return conf
136103}
137104
138- // WithCore adds any user supplied zap core to the logger.
139- func WithCore (core zapcore.Core ) logConfig {
140- return logConfig {core : core }
141- }
142-
143105// AddSentry initializes a sentry client and extends an existing
144106// logr.Logger with the hook.
145- func AddSentry (l logr.Logger , opts sentry.ClientOptions , tags map [string ]string ) (logr.Logger , func () error , error ) {
146- return AddSink (l , WithSentry (opts , tags ))
107+ func AddSentry (l logr.Logger , client * sentry.Client , tags map [string ]string ) (logr.Logger , SyncFunc , error ) {
108+ return AddSink (l , WithSentry (client , tags ))
147109}
148110
149111// AddSink extends an existing logr.Logger with a new sink. It returns the new logr.Logger, a cleanup function, and an
150112// error.
151113//
152114// The new sink will not inherit any of the existing logger's key-value pairs. Key-value pairs can be added to the new
153115// sink specifically by passing them to this function.
154- func AddSink (l logr.Logger , sink logConfig , keysAndValues ... any ) (logr.Logger , func () error , error ) {
155- if sink .err != nil {
156- return l , nil , sink .err
157- }
158-
116+ func AddSink (l logr.Logger , core zapcore.Core , keysAndValues ... any ) (logr.Logger , SyncFunc , error ) {
159117 // New key-value pairs cannot be ergonomically added directly to cores. logr has code to do it, but that code is not
160118 // exported. Rather than replicating it ourselves, we indirectly use it by creating a temporary logger for the new
161119 // core, adding the key-value pairs to the temporary logger, and then extracting the temporary logger's modified
162120 // core.
163- newSinkLogger := zapr .NewLogger (zap .New (sink . core ))
121+ newSinkLogger := zapr .NewLogger (zap .New (core ))
164122 newSinkLogger = newSinkLogger .WithValues (keysAndValues ... )
165123 newCoreLogger , err := getZapLogger (newSinkLogger )
166124 if err != nil {
@@ -186,7 +144,7 @@ func AddSink(l logr.Logger, sink logConfig, keysAndValues ...any) (logr.Logger,
186144
187145 zapLogger = zapLogger .WithOptions (newLoggerOptions ... )
188146 newLogger := zapr .NewLogger (zapLogger )
189- return newLogger , firstErrorFunc ( zapLogger .Sync , sink . cleanup ) , nil
147+ return newLogger , zapLogger .Sync , nil
190148}
191149
192150// getZapLogger is a helper function that gets the underlying zap logger from a
@@ -241,31 +199,14 @@ func ToSlogger(l logr.Logger) *slog.Logger {
241199 return slog .New (logr .ToSlogHandler (l ))
242200}
243201
244- // firstErrorFunc is a helper function that returns a function that executes
245- // all provided args and returns the first error, if any.
246- func firstErrorFunc (fs ... func () error ) func () error {
247- return func () error {
248- var firstErr error = nil
249- for _ , f := range fs {
250- if f == nil {
251- continue
252- }
253- if err := f (); err != nil && firstErr == nil {
254- firstErr = err
255- }
256- }
257- return firstErr
258- }
259- }
260-
261- // newCoreConfig is a helper function that creates a default sinkConfig,
202+ // newCore is a helper function that creates a default sinkConfig,
262203// applies the options, then creates a zapcore.Core.
263- func newCoreConfig (
204+ func newCore (
264205 defaultEncoder zapcore.Encoder ,
265206 defaultSink zapcore.WriteSyncer ,
266207 defaultLevel levelSetter ,
267208 opts ... SinkOption ,
268- ) logConfig {
209+ ) zapcore. Core {
269210 conf := sinkConfig {
270211 encoder : defaultEncoder ,
271212 sink : defaultSink ,
@@ -288,5 +229,5 @@ func newCoreConfig(
288229 core = & suppressCallerCore {Core : core }
289230 }
290231
291- return logConfig { core : core }
232+ return core
292233}
0 commit comments