add package name in log contents (#18251)

Signed-off-by: Zach41 <zongmei.zhang@zilliz.com>
pull/18254/head
Zach 2022-07-12 23:02:26 +08:00 committed by GitHub
parent a926a9eb66
commit 84d73bd618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 43 deletions

View File

@ -113,14 +113,14 @@ func TestLog(t *testing.T) {
zap.Duration("duration", 10*time.Second),
)
ts.assertMessages(
`[INFO] [zap_log_test.go:65] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]`,
`[INFO] [zap_log_test.go:70] ["failed to \"fetch\" [URL]: http://example.com"]`,
`[DEBUG] [zap_log_test.go:71] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]`,
`[INFO] [zap_log_test.go:77] [Welcome]`,
`[INFO] [zap_log_test.go:78] [欢迎]`,
`[WARN] [zap_log_test.go:79] [Type] [Counter=NaN] [Score=+Inf]`,
`[INFO] [zap_log_test.go:84] ["new connection"] [connID=1] [traceID=dse1121]`,
`[INFO] [zap_log_test.go:85] ["Testing typs"] [filed1=noquote] `+
`[INFO] [log/zap_log_test.go:65] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]`,
`[INFO] [log/zap_log_test.go:70] ["failed to \"fetch\" [URL]: http://example.com"]`,
`[DEBUG] [log/zap_log_test.go:71] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]`,
`[INFO] [log/zap_log_test.go:77] [Welcome]`,
`[INFO] [log/zap_log_test.go:78] [欢迎]`,
`[WARN] [log/zap_log_test.go:79] [Type] [Counter=NaN] [Score=+Inf]`,
`[INFO] [log/zap_log_test.go:84] ["new connection"] [connID=1] [traceID=dse1121]`,
`[INFO] [log/zap_log_test.go:85] ["Testing typs"] [filed1=noquote] `+
`[filed2="in quote"] [urls="[http://mock1.com:2347,http://mock2.com:2432]"] `+
`[urls-peer="[t1,\"t2 fine\"]"] ["store ids"="[1,4,5]"] [object="{username=user1}"] `+
`[object2="{username=\"user 2\"}"] [binary="YWIxMjM="] ["is processed"=true] `+
@ -164,9 +164,9 @@ func TestZapCaller(t *testing.T) {
}
expect := []string{
"server.go:132",
"coordinator.go:20",
"ztest_coordinator1.go:20",
"<unknown>",
"server/coordinator.go:20",
"\"z\\\\test_coordinator1.go:20\"",
"undefined",
}
conf := &Config{Level: "deug", File: FileLogConfig{}, DisableTimestamp: true}
enc := NewTextEncoder(conf).(*textEncoder)
@ -191,8 +191,8 @@ func TestLogJSON(t *testing.T) {
"backoff", time.Second,
)
logger.With(zap.String("connID", "1"), zap.String("traceID", "dse1121")).Info("new connection")
ts.assertMessages("{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:188\",\"message\":\"failed to fetch URL\",\"url\":\"http://example.com\",\"attempt\":3,\"backoff\":\"1s\"}",
"{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:193\",\"message\":\"new connection\",\"connID\":\"1\",\"traceID\":\"dse1121\"}")
ts.assertMessages("{\"level\":\"INFO\",\"caller\":\"log/zap_log_test.go:188\",\"message\":\"failed to fetch URL\",\"url\":\"http://example.com\",\"attempt\":3,\"backoff\":\"1s\"}",
"{\"level\":\"INFO\",\"caller\":\"log/zap_log_test.go:193\",\"message\":\"new connection\",\"connID\":\"1\",\"traceID\":\"dse1121\"}")
}
func TestRotateLog(t *testing.T) {

View File

@ -38,7 +38,6 @@ import (
"encoding/json"
"fmt"
"math"
"strings"
"sync"
"time"
"unicode/utf8"
@ -61,35 +60,7 @@ func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
// ShortCallerEncoder serializes a caller in file:line format.
func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(getCallerString(caller))
}
func getCallerString(ec zapcore.EntryCaller) string {
if !ec.Defined {
return "<unknown>"
}
idx := strings.LastIndexByte(ec.File, '/')
buf := _pool.Get()
for i := idx + 1; i < len(ec.File); i++ {
b := ec.File[i]
switch {
case b >= 'A' && b <= 'Z':
buf.AppendByte(b)
case b >= 'a' && b <= 'z':
buf.AppendByte(b)
case b >= '0' && b <= '9':
buf.AppendByte(b)
case b == '.' || b == '-' || b == '_':
buf.AppendByte(b)
default:
}
}
buf.AppendByte(':')
buf.AppendInt(int64(ec.Line))
caller := buf.String()
buf.Free()
return caller
enc.AppendString(caller.TrimmedPath())
}
// For JSON-escaping; see textEncoder.safeAddString below.