Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the With method of the zap logger #9

Merged
merged 1 commit into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`)
}