mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 18:00:00 +08:00
fix: avoid duplicate in logx plain mode (#4080)
This commit is contained in:
@@ -254,11 +254,10 @@ func (n nopWriter) Stack(_ any) {
|
|||||||
func (n nopWriter) Stat(_ any, _ ...LogField) {
|
func (n nopWriter) Stat(_ any, _ ...LogField) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildPlainFields(fields ...LogField) []string {
|
func buildPlainFields(fields logEntry) []string {
|
||||||
var items []string
|
items := make([]string, 0, len(fields))
|
||||||
|
for k, v := range fields {
|
||||||
for _, field := range fields {
|
items = append(items, fmt.Sprintf("%s=%+v", k, v))
|
||||||
items = append(items, fmt.Sprintf("%s=%+v", field.Key, field.Value))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return items
|
return items
|
||||||
@@ -289,15 +288,17 @@ func output(writer io.Writer, level string, val any, fields ...LogField) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fields = combineGlobalFields(fields)
|
fields = combineGlobalFields(fields)
|
||||||
|
// +3 for timestamp, level and content
|
||||||
|
entry := make(logEntry, len(fields)+3)
|
||||||
|
for _, field := range fields {
|
||||||
|
entry[field.Key] = field.Value
|
||||||
|
}
|
||||||
|
|
||||||
switch atomic.LoadUint32(&encoding) {
|
switch atomic.LoadUint32(&encoding) {
|
||||||
case plainEncodingType:
|
case plainEncodingType:
|
||||||
writePlainAny(writer, level, val, buildPlainFields(fields...)...)
|
plainFields := buildPlainFields(entry)
|
||||||
|
writePlainAny(writer, level, val, plainFields...)
|
||||||
default:
|
default:
|
||||||
entry := make(logEntry)
|
|
||||||
for _, field := range fields {
|
|
||||||
entry[field.Key] = field.Value
|
|
||||||
}
|
|
||||||
entry[timestampKey] = getTimestamp()
|
entry[timestampKey] = getTimestamp()
|
||||||
entry[levelKey] = level
|
entry[levelKey] = level
|
||||||
entry[contentKey] = val
|
entry[contentKey] = val
|
||||||
|
|||||||
@@ -189,6 +189,41 @@ func TestWritePlainAny(t *testing.T) {
|
|||||||
assert.Contains(t, buf.String(), "runtime/debug.Stack")
|
assert.Contains(t, buf.String(), "runtime/debug.Stack")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWritePlainDuplicate(t *testing.T) {
|
||||||
|
old := atomic.SwapUint32(&encoding, plainEncodingType)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
atomic.StoreUint32(&encoding, old)
|
||||||
|
})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
output(&buf, levelInfo, "foo", LogField{
|
||||||
|
Key: "first",
|
||||||
|
Value: "a",
|
||||||
|
}, LogField{
|
||||||
|
Key: "first",
|
||||||
|
Value: "b",
|
||||||
|
})
|
||||||
|
assert.Contains(t, buf.String(), "foo")
|
||||||
|
assert.NotContains(t, buf.String(), "first=a")
|
||||||
|
assert.Contains(t, buf.String(), "first=b")
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
output(&buf, levelInfo, "foo", LogField{
|
||||||
|
Key: "first",
|
||||||
|
Value: "a",
|
||||||
|
}, LogField{
|
||||||
|
Key: "first",
|
||||||
|
Value: "b",
|
||||||
|
}, LogField{
|
||||||
|
Key: "second",
|
||||||
|
Value: "c",
|
||||||
|
})
|
||||||
|
assert.Contains(t, buf.String(), "foo")
|
||||||
|
assert.NotContains(t, buf.String(), "first=a")
|
||||||
|
assert.Contains(t, buf.String(), "first=b")
|
||||||
|
assert.Contains(t, buf.String(), "second=c")
|
||||||
|
}
|
||||||
|
|
||||||
func TestLogWithLimitContentLength(t *testing.T) {
|
func TestLogWithLimitContentLength(t *testing.T) {
|
||||||
maxLen := atomic.LoadUint32(&maxContentLength)
|
maxLen := atomic.LoadUint32(&maxContentLength)
|
||||||
atomic.StoreUint32(&maxContentLength, 10)
|
atomic.StoreUint32(&maxContentLength, 10)
|
||||||
|
|||||||
Reference in New Issue
Block a user