Core

/app/lib/log/logger.go (2.3 KB)

 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
package log

import (
"runtime"
"strings"
"sync"

"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"{{{ .Package }}}/app/util"
)

const keyCustom = "custom"

var (
RecentLogs []*zapcore.Entry
recentMU = &sync.Mutex{}
)

func InitLogging(debug bool) (util.Logger, error) {
var logger *zap.Logger
var err error
lf := util.GetEnv("logging_format")
switch {
case strings.EqualFold(lf, "json"):
logger, err = initJSONLogging(getLevel(zap.InfoLevel))
case debug:
logger, err = initDevLogging(getLevel(zap.DebugLevel))
default:
logger, err = initSimpleLogging(getLevel(zap.DebugLevel))
}
if err != nil {
return nil, errors.Wrap(err, "error initializing logging")
}
return logger.Sugar(), nil
}

func CreateTestLogger() (util.Logger, error) {
return InitLogging(false)
}

func initDevLogging(lvl zapcore.Level) (*zap.Logger, error) {
_ = zap.RegisterEncoder(keyCustom, func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
return newEncoder(cfg, runtime.GOOS != "js"), nil
})
config := zap.NewDevelopmentConfig()
config.EncoderConfig = zapcore.EncoderConfig{}
config.Level = zap.NewAtomicLevelAt(lvl)
config.Encoding = keyCustom
return config.Build(zap.AddStacktrace(zap.PanicLevel), zap.AddCaller())
}

func initJSONLogging(lvl zapcore.Level) (*zap.Logger, error) {
config := zap.NewProductionConfig()
config.EncoderConfig.FunctionKey = "func"
config.Level = zap.NewAtomicLevelAt(lvl)
return config.Build(zap.AddStacktrace(zap.PanicLevel), zap.AddCaller())
}

func initSimpleLogging(lvl zapcore.Level) (*zap.Logger, error) {
_ = zap.RegisterEncoder(keyCustom, func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
return createSimpleEncoder(cfg), nil
})
config := zap.NewDevelopmentConfig()
config.EncoderConfig = zapcore.EncoderConfig{}
config.Level = zap.NewAtomicLevelAt(lvl)
config.Encoding = keyCustom
return config.Build(zap.AddStacktrace(zap.PanicLevel), zap.AddCaller())
}

func getLevel(dflt zapcore.Level) zapcore.Level {
l := util.GetEnv("logging_level")
switch {
case strings.EqualFold(l, "debug"):
return zap.DebugLevel
case strings.EqualFold(l, "info"):
return zap.InfoLevel
case strings.EqualFold(l, "warn"):
return zap.WarnLevel
case strings.EqualFold(l, "error"):
return zap.ErrorLevel
}
return dflt
}