-
Notifications
You must be signed in to change notification settings - Fork 12
/
runenv_logger.go
55 lines (47 loc) · 1.13 KB
/
runenv_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
package runtime
import (
"os"
"path/filepath"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func (re *RunEnv) initLogger() {
level := zap.NewAtomicLevel()
if lvl := os.Getenv("LOG_LEVEL"); lvl != "" {
if err := level.UnmarshalText([]byte(lvl)); err != nil {
defer func() {
// once the logger is defined...
if re.logger != nil {
re.logger.Sugar().Errorf("failed to decode log level '%q': %s", lvl, err)
}
}()
}
} else {
level.SetLevel(zapcore.InfoLevel)
}
paths := []string{"stdout"}
if re.TestOutputsPath != "" {
paths = append(paths, filepath.Join(re.TestOutputsPath, "run.out"))
}
cfg := zap.Config{
Development: false,
Level: level,
DisableCaller: true,
DisableStacktrace: true,
OutputPaths: paths,
Encoding: "json",
InitialFields: map[string]interface{}{
"run_id": re.TestRun,
"group_id": re.TestGroupID,
},
}
enc := zap.NewProductionEncoderConfig()
enc.LevelKey, enc.NameKey = "", ""
enc.EncodeTime = zapcore.EpochNanosTimeEncoder
cfg.EncoderConfig = enc
var err error
re.logger, err = cfg.Build()
if err != nil {
panic(err)
}
}