-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.go
105 lines (88 loc) · 2.74 KB
/
logger.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2022 Azugo. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package core
import (
"os"
"strings"
"azugo.io/core/system"
"github.com/mattn/go-colorable"
"go.elastic.co/ecszap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func (a *App) loggerFields(info *system.Info) []zap.Field {
fields := make([]zap.Field, 0, 3)
if a.AppName != "" {
fields = append(fields, zap.String("service.name", a.AppName))
}
if a.AppVer != "" {
fields = append(fields, zap.String("service.version", a.AppVer))
}
fields = append(fields, zap.String("service.environment", strings.ToLower(string(a.Env()))))
if info != nil {
if info.Hostname != "" {
fields = append(fields, zap.String("host.hostname", info.Hostname))
}
if info.IsContainer() {
if info.Container.ID != "" {
fields = append(fields, zap.String("container.id", info.Container.ID))
}
if info.IsKubernetes() {
if info.Container.Kubernetes.Namespace != "" {
fields = append(fields, zap.String("kubernetes.namespace", info.Container.Kubernetes.Namespace))
}
if info.Container.Kubernetes.PodName != "" {
fields = append(fields, zap.String("kubernetes.pod.name", info.Container.Kubernetes.PodName))
}
if info.Container.Kubernetes.PodUID != "" {
fields = append(fields, zap.String("kubernetes.pod.uid", info.Container.Kubernetes.PodUID))
}
if info.Container.Kubernetes.NodeName != "" {
fields = append(fields, zap.String("kubernetes.node.name", info.Container.Kubernetes.NodeName))
}
}
}
}
return fields
}
func (a *App) initLogger() error {
if a.logger != nil {
return nil
}
info := system.CollectInfo()
if a.Env().IsDevelopment() && !info.IsContainer() {
conf := zap.NewDevelopmentEncoderConfig()
conf.EncodeLevel = zapcore.CapitalColorLevelEncoder
a.logger = zap.New(
zapcore.NewCore(
zapcore.NewConsoleEncoder(conf),
zapcore.AddSync(colorable.NewColorableStdout()),
zap.DebugLevel,
),
zap.AddCaller(),
zap.AddStacktrace(zap.ErrorLevel),
).With(a.loggerFields(info)...)
return nil
}
encoderConfig := ecszap.NewDefaultEncoderConfig()
core := ecszap.NewCore(encoderConfig, os.Stdout, zap.InfoLevel)
a.logger = zap.New(core, zap.AddCaller()).With(a.loggerFields(info)...)
return nil
}
// ReplaceLogger replaces current application logger with custom.
//
// Default fields are automatically added to the logger.
func (a *App) ReplaceLogger(logger *zap.Logger) error {
a.logger = logger.With(a.loggerFields(system.CollectInfo())...)
return a.initLogger()
}
// Log returns application logger.
func (a *App) Log() *zap.Logger {
if a.logger == nil {
if err := a.initLogger(); err != nil {
panic(err)
}
}
return a.logger
}