Skip to content

Commit

Permalink
feat(app): add WithZapOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 30, 2023
1 parent bd9236d commit d0d4f06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const (
func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Option) {
// Apply options.
opts := options{
cfg: zap.NewProductionConfig(),
ctx: context.Background(),
zapConfig: zap.NewProductionConfig(),
ctx: context.Background(),
}
for _, o := range op {
o.apply(&opts)
Expand All @@ -54,9 +54,9 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Op
if err := lvl.UnmarshalText([]byte(s)); err != nil {
panic(err)
}
opts.cfg.Level.SetLevel(lvl)
opts.zapConfig.Level.SetLevel(lvl)
}
lg, err := opts.cfg.Build()
lg, err := opts.zapConfig.Build(opts.zapOptions...)
if err != nil {
panic(err)
}
Expand Down
14 changes: 11 additions & 3 deletions app/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type options struct {
cfg zap.Config
ctx context.Context
zapConfig zap.Config
zapOptions []zap.Option
ctx context.Context

meterOptions []autometer.Option
tracerOptions []autotracer.Option
Expand All @@ -31,7 +32,14 @@ type Option interface {
// WithZapConfig sets the default zap config for the application.
func WithZapConfig(cfg zap.Config) Option {
return optionFunc(func(o *options) {
o.cfg = cfg
o.zapConfig = cfg
})
}

// WithZapOptions sets additional zap logger options for the application.
func WithZapOptions(opts ...zap.Option) Option {
return optionFunc(func(o *options) {
o.zapOptions = opts
})
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/sdk-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/go-faster/sdk/app"
"github.com/go-faster/sdk/autometer"
Expand All @@ -20,6 +21,13 @@ func main() {
},
// Configure custom zap config.
app.WithZapConfig(zap.NewDevelopmentConfig()),
app.WithZapOptions(
// Custom zap logger options.
// E.g. hooks, custom core.
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewTee(core)
}),
),

// Redirect metrics and traces to /dev/null.
app.WithMeterOptions(autometer.WithWriter(io.Discard)),
Expand Down

0 comments on commit d0d4f06

Please sign in to comment.