Skip to content

Commit

Permalink
Expose the With method of the zap logger (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku authored and nolouch committed Jan 17, 2020
1 parent b742a5d commit d28c14d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func Fatal(msg string, fields ...zap.Field) {
L().WithOptions(zap.AddCallerSkip(1)).Fatal(msg, fields...)
}

// With creates a child logger and adds structured context to it.
// Fields added to the child don't affect the parent, and vice versa.
func With(fields ...zap.Field) *zap.Logger {
return L().WithOptions(zap.AddCallerSkip(1)).With(fields...)
}

// SetLevel alters the logging level.
func SetLevel(l zapcore.Level) {
_globalP.Level.SetLevel(l)
Expand Down
9 changes: 9 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package log

import (
. "github.com/pingcap/check"
"go.uber.org/zap"
)

var _ = Suite(&testLogSuite{})
Expand All @@ -30,4 +31,12 @@ func (t *testLogSuite) TestExport(c *C) {
Warn("Testing")
Error("Testing")
lg.AssertContains("log_test.go:")

lg = newZapTestLogger(conf, c)
ReplaceGlobals(lg.Logger, nil)
logger := With(zap.String("name", "tester"), zap.Int64("age", 42))
logger.Info("hello")
logger.Debug("world")
lg.AssertContains(`name=tester`)
lg.AssertContains(`age=42`)
}

0 comments on commit d28c14d

Please sign in to comment.