mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 17:20:24 +08:00
feat: add logger.WithFields (#2546)
This commit is contained in:
parent
9cadab2684
commit
06e4914e41
@ -45,4 +45,6 @@ type Logger interface {
|
|||||||
WithContext(ctx context.Context) Logger
|
WithContext(ctx context.Context) Logger
|
||||||
// WithDuration returns a new logger with the given duration.
|
// WithDuration returns a new logger with the given duration.
|
||||||
WithDuration(d time.Duration) Logger
|
WithDuration(d time.Duration) Logger
|
||||||
|
// WithFields returns a new logger with the given fields.
|
||||||
|
WithFields(fields ...LogField) Logger
|
||||||
}
|
}
|
||||||
|
@ -123,6 +123,11 @@ func (l *richLogger) WithDuration(duration time.Duration) Logger {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) WithFields(fields ...LogField) Logger {
|
||||||
|
l.fields = append(l.fields, fields...)
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
func (l *richLogger) buildFields(fields ...LogField) []LogField {
|
func (l *richLogger) buildFields(fields ...LogField) []LogField {
|
||||||
fields = append(l.fields, fields...)
|
fields = append(l.fields, fields...)
|
||||||
fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip)))
|
fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip)))
|
||||||
|
@ -272,6 +272,23 @@ func TestLogWithCallerSkip(t *testing.T) {
|
|||||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoggerWithFields(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
writer.lock.RLock()
|
||||||
|
defer func() {
|
||||||
|
writer.lock.RUnlock()
|
||||||
|
writer.Store(old)
|
||||||
|
}()
|
||||||
|
|
||||||
|
l := WithContext(context.Background()).WithFields(Field("foo", "bar"))
|
||||||
|
l.Info(testlog)
|
||||||
|
|
||||||
|
var val mockValue
|
||||||
|
assert.Nil(t, json.Unmarshal([]byte(w.String()), &val))
|
||||||
|
assert.Equal(t, "bar", val.Foo)
|
||||||
|
}
|
||||||
|
|
||||||
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
||||||
var val mockValue
|
var val mockValue
|
||||||
dec := json.NewDecoder(strings.NewReader(body))
|
dec := json.NewDecoder(strings.NewReader(body))
|
||||||
|
Loading…
Reference in New Issue
Block a user