-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Extend WithLazy() method to SugaredLogger #1378
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ package zap | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"go.uber.org/zap/internal/exit" | ||
|
@@ -54,6 +56,9 @@ func TestSugarWith(t *testing.T) { | |
} | ||
} | ||
|
||
type withAny func(*SugaredLogger, ...interface{}) *SugaredLogger | ||
withMethods := []withAny{(*SugaredLogger).With, (*SugaredLogger).WithLazy} | ||
|
||
tests := []struct { | ||
desc string | ||
args []interface{} | ||
|
@@ -141,16 +146,94 @@ func TestSugarWith(t *testing.T) { | |
} | ||
|
||
for _, tt := range tests { | ||
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) { | ||
logger.With(tt.args...).Info("") | ||
output := logs.AllUntimed() | ||
if len(tt.errLogs) > 0 { | ||
for i := range tt.errLogs { | ||
assert.Equal(t, tt.errLogs[i], output[i], "Unexpected error log at position %d for scenario %s.", i, tt.desc) | ||
for _, withMethod := range withMethods { | ||
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) { | ||
withMethod(logger, tt.args...).Info("") | ||
output := logs.AllUntimed() | ||
if len(tt.errLogs) > 0 { | ||
for i := range tt.errLogs { | ||
assert.Equal(t, tt.errLogs[i], output[i], "Unexpected error log at position %d for scenario %s.", i, tt.desc) | ||
} | ||
} | ||
assert.Equal(t, len(tt.errLogs)+1, len(output), "Expected only one non-error message to be logged in scenario %s.", tt.desc) | ||
assert.Equal(t, tt.expected, output[len(tt.errLogs)].Context, "Unexpected message context in scenario %s.", tt.desc) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func TestSugarWithCaptures(t *testing.T) { | ||
type withAny func(*SugaredLogger, ...interface{}) *SugaredLogger | ||
|
||
tests := []struct { | ||
name string | ||
withMethods []withAny | ||
wantJSON []string | ||
}{ | ||
{ | ||
name: "with captures arguments at time of With", | ||
withMethods: []withAny{(*SugaredLogger).With}, | ||
wantJSON: []string{ | ||
`{ | ||
"m": "hello 0", | ||
"a0": [0], | ||
"b0": [1] | ||
}`, | ||
`{ | ||
"m": "world 0", | ||
"a0": [0], | ||
"c0": [2] | ||
}`, | ||
}, | ||
}, | ||
{ | ||
name: "lazy with captures arguments at time of With or Logging", | ||
withMethods: []withAny{(*SugaredLogger).WithLazy}, | ||
wantJSON: []string{ | ||
`{ | ||
"m": "hello 0", | ||
"a0": [1], | ||
"b0": [1] | ||
}`, | ||
`{ | ||
"m": "world 0", | ||
"a0": [1], | ||
"c0": [2] | ||
}`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/uber-go/zap/pull/1319/files also has a few more scenarios for the capture tests. While you are testing some aspect of chained With and WithLazy, I think it makes sense to also test interleaved calls of With then WithLazy and vis-versa as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually thinking about including those interleaved I'm totally fine with adding more tests if it really helps. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that there would be testing overlap between the two loggers. I think it would be nice for zap to set up testing in a way that would be easy to verify sugared and non sugared behavior through the same process where they should be similar. However, that doesn't need to be part of this change. |
||
t.Run(tt.name, func(t *testing.T) { | ||
enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{ | ||
MessageKey: "m", | ||
}) | ||
|
||
var bs ztest.Buffer | ||
logger := New(zapcore.NewCore(enc, &bs, DebugLevel)).Sugar() | ||
|
||
for i, withMethod := range tt.withMethods { | ||
iStr := strconv.Itoa(i) | ||
x := 10 * i | ||
arr := zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error { | ||
enc.AppendInt(x) | ||
return nil | ||
}) | ||
|
||
logger = withMethod(logger, Array("a"+iStr, arr)) | ||
x++ | ||
logger.Infow(fmt.Sprintf("hello %d", i), Array("b"+iStr, arr)) | ||
x++ | ||
logger = withMethod(logger, Array("c"+iStr, arr)) | ||
logger.Infow(fmt.Sprintf("world %d", i)) | ||
} | ||
|
||
if lines := bs.Lines(); assert.Len(t, lines, len(tt.wantJSON)) { | ||
for i, want := range tt.wantJSON { | ||
assert.JSONEq(t, want, lines[i], "Unexpected output from the %d'th log.", i) | ||
} | ||
} | ||
assert.Equal(t, len(tt.errLogs)+1, len(output), "Expected only one non-error message to be logged in scenario %s.", tt.desc) | ||
assert.Equal(t, tt.expected, output[len(tt.errLogs)].Context, "Unexpected message context in scenario %s.", tt.desc) | ||
}) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that this may be a typo in the original zap.Logger PR, but I think the test case name should be "lazy with captures arguments at time of Logging"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think so, fixed