forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaplog_test.go
44 lines (39 loc) · 1.17 KB
/
zaplog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package log
import (
"bytes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewZapBufLogger return a buffer logger
func NewZapBufLogger(buf *bytes.Buffer, skip int) Logger {
encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core := zapcore.NewCore(encoder, zapcore.AddSync(buf), zapcore.DebugLevel)
return &zapLog{
levels: []zap.AtomicLevel{},
logger: zap.New(
core,
zap.AddCallerSkip(skip),
zap.AddCaller(),
),
}
}
// NewZapFatalLogger return a fatal hook logger
func NewZapFatalLogger(h zapcore.CheckWriteHook) Logger {
core, _ := newConsoleCore(&OutputConfig{
Writer: "console",
Level: "debug",
Formatter: "console",
})
return &zapLog{
levels: []zap.AtomicLevel{},
logger: zap.New(
core,
zap.AddCallerSkip(1),
zap.AddCaller(),
zap.WithFatalHook(h),
)}
}