-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
101 lines (82 loc) · 2.06 KB
/
utils.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
package logger
import (
"os"
"strconv"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02T15:04:05.000Z"))
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.MessageKey = "message"
encoderConfig.CallerKey = "method"
encoderConfig.TimeKey = "date"
encoderConfig.EncodeTime = timeEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}
func getLogLevel(level string) zapcore.Level {
switch level {
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
case "fatal":
return zapcore.FatalLevel
default:
return zapcore.DebugLevel
}
}
func createLogger(config Configuration) *zap.Logger {
level := getLogLevel(config.ConsoleLevel)
writer := zapcore.Lock(os.Stdout)
core := zapcore.NewCore(getEncoder(), writer, level)
logger := zap.New(
core,
zap.AddCallerSkip(2),
zap.AddCaller(),
)
host, _ := os.Hostname()
pid := os.Getpid()
logger = logger.With(
zap.Int("pid", pid),
zap.String("host", host),
)
if config.Service != "" {
logger = logger.With(zap.String("service", config.Service))
}
if config.Environment != "" {
logger = logger.With(zap.String("environment", config.Environment))
}
if config.Team != "" {
logger = logger.With(zap.String("team", config.Team))
}
if config.Project != "" {
logger = logger.With(zap.String("project", config.Project))
}
if config.Version != "" {
logger = logger.With(zap.String("version", config.Version))
}
return logger
}
func parserPayload(payload map[string]string) map[string]interface{} {
parsedPayload := make(map[string]interface{})
for key, value := range payload {
if key == "duration" {
newValue, _ := strconv.Atoi(value)
parsedPayload[key] = newValue
} else {
parsedPayload[key] = value
}
}
return parsedPayload
}
func convertUInt64ToString(id uint64) string {
return strconv.FormatUint(id, 10)
}