-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
71 lines (65 loc) · 1.89 KB
/
zap.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package go_common_aws_sqs
import (
"encoding/json"
zinc "github.com/yxw21/go-commons-zinc"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)
type ZincWriter struct {
zinc.Client
}
func (zincWriter *ZincWriter) Write(p []byte) (n int, err error) {
var document = make(map[string]interface{})
if err := json.Unmarshal(p, &document); err != nil {
return 0, err
}
_, _, err = zincWriter.DocumentIndex(document)
if err != nil {
return 0, err
}
return len(p), nil
}
func NewZincWriter(index string) *ZincWriter {
endpoint := os.Getenv("ZINC_ENDPOINT")
username := os.Getenv("ZINC_USERNAME")
password := os.Getenv("ZINC_PASSWORD")
instance := &ZincWriter{}
instance.SetEndpoint(endpoint)
instance.SetAuth(username, password)
instance.SetIndex(index)
return instance
}
func NewLoggerWithZinc(zincWriter *ZincWriter) *zap.Logger {
lowPriority := zap.LevelEnablerFunc(func(lv zapcore.Level) bool {
return lv >= zapcore.DebugLevel
})
jsonEnc := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
stdCore := zapcore.NewCore(jsonEnc, zapcore.Lock(os.Stdout), lowPriority)
syncer := zapcore.AddSync(zincWriter)
redisCore := zapcore.NewCore(jsonEnc, syncer, lowPriority)
core := zapcore.NewTee(stdCore, redisCore)
return zap.New(core).WithOptions(
zap.AddCaller(),
zap.AddStacktrace(zapcore.WarnLevel),
zap.Fields(
zap.String("time", time.Now().Format("2006-01-02 15:04:05")),
),
)
}
func NewLogger() *zap.Logger {
lowPriority := zap.LevelEnablerFunc(func(lv zapcore.Level) bool {
return lv >= zapcore.DebugLevel
})
jsonEnc := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
stdCore := zapcore.NewCore(jsonEnc, zapcore.Lock(os.Stdout), lowPriority)
core := zapcore.NewTee(stdCore)
return zap.New(core).WithOptions(
zap.AddCaller(),
zap.AddStacktrace(zapcore.WarnLevel),
zap.Fields(
zap.String("time", time.Now().Format("2006-01-02 15:04:05")),
),
)
}