-
Notifications
You must be signed in to change notification settings - Fork 8
/
loggy.go
62 lines (52 loc) · 1.18 KB
/
loggy.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
// Package loggy is just a little zap-logger wrapper.
package loggy
import (
"github.com/google/uuid"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"strings"
)
var logger *zap.Logger
var (
botID = strings.Split(uuid.New().String(), "-")[0]
GetBotID = func() string {
return botID
}
)
func init() {
level := zapcore.InfoLevel
switch os.Getenv("TRADEBOT_LOG_LEVEL") {
case "DEBUG":
level = zapcore.DebugLevel
case "INFO":
level = zapcore.InfoLevel
case "WARN":
level = zapcore.WarnLevel
case "ERROR":
level = zapcore.ErrorLevel
}
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(level),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
InitialFields: map[string]interface{}{
"env": os.Getenv("TRADEBOT_ENV"),
"bot_id": botID,
},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
logger, _ = cfg.Build()
}
func GetLogger() *zap.Logger {
return logger
}