diff --git a/example_test.go b/example_test.go index 2d7c07b..33b44b5 100644 --- a/example_test.go +++ b/example_test.go @@ -77,12 +77,13 @@ func Example_composite() { }() composite.Info("hello composite 2!") cli.Info("hello cli 2!") + composite.With(zap.String("foo", "bar")).Warn("warn composite!") scanner := bufio.NewScanner(r) lines := 0 for scanner.Scan() { fmt.Println("-> ", scanner.Text()) lines++ - if lines == 2 { + if lines == 3 { break } } @@ -92,8 +93,10 @@ func Example_composite() { // {"level":"info","msg":"hello composite!"} // {"level":"info","msg":"hello composite 2!"} // {"level":"info","msg":"hello cli 2!"} + // {"level":"warn","msg":"warn composite!","foo":"bar"} // -> {"L":"INFO","M":"hello composite!"} // -> {"L":"INFO","M":"hello composite 2!"} + // -> {"L":"WARN","M":"warn composite!","foo":"bar"} } func Example_simple() { diff --git a/zapring.go b/zapring.go index a802d38..b7bb303 100644 --- a/zapring.go +++ b/zapring.go @@ -59,6 +59,23 @@ func (c *Core) Enabled(level zapcore.Level) bool { return c.Core.Enabled(level) } +func (c *Core) clone() *Core { + return &Core{ + buffer: c.buffer, + enc: c.enc.Clone(), + Core: c.Core, + } +} + +// With implements zapcore.Core. +func (c *Core) With(fields []zapcore.Field) zapcore.Core { + clone := c.clone() + for _, field := range fields { + field.AddTo(clone.enc) + } + return clone +} + // Check implements zapcore.Core. func (c *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry { c.setup()