Skip to content

Commit

Permalink
Merge pull request #4 from moul/dev/moul/with
Browse files Browse the repository at this point in the history
  • Loading branch information
moul authored Mar 19, 2021
2 parents 4bf35cc + be768e7 commit b953569
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions zapring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b953569

Please sign in to comment.