/
logger.go
217 lines (178 loc) · 5.55 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package logger
import (
"context"
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
"github.com/clicworth-com/gomicroutils/v3/amqp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type ctxKey struct{}
var once sync.Once
type CBLogger struct {
logger *zap.Logger
}
var cblog *CBLogger
const FUNCTION_SKIP_LEVEL = 1
// Get initializes a zap.Logger instance if it has not been initialized
// already and returns the same instance for subsequent calls.
func Get() *CBLogger {
once.Do(func() {
stdout := zapcore.AddSync(os.Stdout)
msgq := zapcore.AddSync(amqp.Get())
level := zap.InfoLevel
levelEnv := os.Getenv("LOG_LEVEL")
if levelEnv != "" {
levelFromEnv, err := zapcore.ParseLevel(levelEnv)
if err != nil {
log.Println(
fmt.Errorf("invalid level, defaulting to INFO: %w", err),
)
}
level = levelFromEnv
}
logLevel := zap.NewAtomicLevelAt(level)
productionCfg := zap.NewProductionEncoderConfig()
productionCfg.TimeKey = "timestamp"
productionCfg.EncodeTime = zapcore.ISO8601TimeEncoder
productionCfg.CallerKey = "caller"
productionCfg.EncodeCaller = zapcore.ShortCallerEncoder
developmentCfg := zap.NewDevelopmentEncoderConfig()
developmentCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
developmentCfg.CallerKey = "caller"
developmentCfg.EncodeCaller = zapcore.ShortCallerEncoder
consoleEncoder := zapcore.NewConsoleEncoder(developmentCfg)
msgqEncoder := zapcore.NewJSONEncoder(productionCfg)
sn := os.Getenv("SRV_NAME")
if sn == "" {
sn = "dev"
}
st := os.Getenv("SRV_TAG")
if st == "" {
st = "dev"
}
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, stdout, logLevel),
zapcore.NewCore(msgqEncoder, msgq, logLevel).
With(
[]zapcore.Field{
zap.String("srv_name", sn),
zap.String("srv_tag", st),
},
),
)
logger := zap.New(core)
cblog = &CBLogger{
logger: logger,
}
})
return cblog
}
// FromCtx returns the Logger associated with the ctx. If no logger
// is associated, the default logger is returned, unless it is nil
// in which case a disabled logger is returned.
func FromCtx(ctx context.Context) *CBLogger {
if l, ok := ctx.Value(ctxKey{}).(*CBLogger); ok {
return l
} else if l := cblog; l != nil {
return l
}
return &CBLogger{zap.NewNop()}
}
// WithCtx returns a copy of ctx with the Logger attached.
func WithCtx(ctx context.Context, l *CBLogger) context.Context {
if lp, ok := ctx.Value(ctxKey{}).(*CBLogger); ok {
if lp == l {
// Do not store same logger.
return ctx
}
}
return context.WithValue(ctx, ctxKey{}, l)
}
// WithCtx returns a copy of ctx with the Logger attached.
func (l *CBLogger) With(f zapcore.Field) *CBLogger {
nl := l.logger.With(f)
return &CBLogger{nl}
}
func (c *CBLogger) Info(msg string, fields ...zap.Field) {
pc, file, line, ok := runtime.Caller(FUNCTION_SKIP_LEVEL)
if ok {
filetok := strings.Split(file, "/")
filename := filetok[len(filetok)-1]
function := runtime.FuncForPC(pc).Name()
functok := strings.Split(function, ".")
funcname := functok[len(functok)-1]
caller := filename + "->" + funcname + ":" + fmt.Sprint(line)
fields = append(fields, zap.String("caller", caller))
}
c.logger.Info(msg, fields...)
}
func (c *CBLogger) Debug(msg string, fields ...zap.Field) {
pc, file, line, ok := runtime.Caller(FUNCTION_SKIP_LEVEL)
if ok {
filetok := strings.Split(file, "/")
filename := filetok[len(filetok)-1]
function := runtime.FuncForPC(pc).Name()
functok := strings.Split(function, ".")
funcname := functok[len(functok)-1]
caller := filename + "->" + funcname + ":" + fmt.Sprint(line)
fields = append(fields, zap.String("caller", caller))
}
c.logger.Debug(msg, fields...)
}
func (c *CBLogger) Warn(msg string, fields ...zap.Field) {
pc, file, line, ok := runtime.Caller(FUNCTION_SKIP_LEVEL)
if ok {
filetok := strings.Split(file, "/")
filename := filetok[len(filetok)-1]
function := runtime.FuncForPC(pc).Name()
functok := strings.Split(function, ".")
funcname := functok[len(functok)-1]
caller := filename + "->" + funcname + ":" + fmt.Sprint(line)
fields = append(fields, zap.String("caller", caller))
}
c.logger.Warn(msg, fields...)
}
func (c *CBLogger) Error(msg string, fields ...zap.Field) {
pc, file, line, ok := runtime.Caller(FUNCTION_SKIP_LEVEL)
if ok {
filetok := strings.Split(file, "/")
filename := filetok[len(filetok)-1]
function := runtime.FuncForPC(pc).Name()
functok := strings.Split(function, ".")
funcname := functok[len(functok)-1]
caller := filename + "->" + funcname + ":" + fmt.Sprint(line)
fields = append(fields, zap.String("caller", caller))
}
c.logger.Error(msg, fields...)
}
func (c *CBLogger) Panic(msg string, fields ...zap.Field) {
pc, file, line, ok := runtime.Caller(FUNCTION_SKIP_LEVEL)
if ok {
filetok := strings.Split(file, "/")
filename := filetok[len(filetok)-1]
function := runtime.FuncForPC(pc).Name()
functok := strings.Split(function, ".")
funcname := functok[len(functok)-1]
caller := filename + "->" + funcname + ":" + fmt.Sprint(line)
fields = append(fields, zap.String("caller", caller))
}
c.logger.Panic(msg, fields...)
}
func (c *CBLogger) Fatal(msg string, fields ...zap.Field) {
pc, file, line, ok := runtime.Caller(FUNCTION_SKIP_LEVEL)
if ok {
filetok := strings.Split(file, "/")
filename := filetok[len(filetok)-1]
function := runtime.FuncForPC(pc).Name()
functok := strings.Split(function, ".")
funcname := functok[len(functok)-1]
caller := filename + "->" + funcname + ":" + fmt.Sprint(line)
fields = append(fields, zap.String("caller", caller))
}
c.logger.Fatal(msg, fields...)
}