-
Notifications
You must be signed in to change notification settings - Fork 113
/
flags.go
78 lines (69 loc) · 1.72 KB
/
flags.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
package interchaintest
import (
"fmt"
"io"
"os"
"time"
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// The value of the extra flags this test supports.
type mainFlags struct {
LogFile string
LogFormat string
LogLevel string
MatrixFile string
ReportFile string
BlockDatabaseFile string
}
func (f mainFlags) Logger() (lc LoggerCloser, _ error) {
var w zapcore.WriteSyncer
switch f.LogFile {
case "stderr", "":
w = os.Stderr
lc.FilePath = "stderr"
case "stdout":
w = os.Stdout
lc.FilePath = "stdout"
default:
file, err := interchaintest.CreateLogFile(f.LogFile)
if err != nil {
return lc, fmt.Errorf("create log file: %w", err)
}
w = file
lc.Closer = file
lc.FilePath = file.Name()
}
lc.Logger = f.newZap(w)
return lc, nil
}
func (f mainFlags) newZap(w zapcore.WriteSyncer) *zap.Logger {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.UTC().Format("2006-01-02T15:04:05.000000Z07:00"))
}
config.LevelKey = "lvl"
enc := zapcore.NewConsoleEncoder(config)
if f.LogFormat == "json" {
enc = zapcore.NewJSONEncoder(config)
}
lvl := zap.NewAtomicLevel()
if err := lvl.UnmarshalText([]byte(f.LogLevel)); err != nil {
lvl = zap.NewAtomicLevelAt(zap.InfoLevel)
}
return zap.New(zapcore.NewCore(enc, w, lvl))
}
type LoggerCloser struct {
*zap.Logger
io.Closer
FilePath string
}
func (lc LoggerCloser) Close() error {
// ignore error because of https://github.com/uber-go/zap/issues/880 with stderr/stdout
_ = lc.Logger.Sync()
if lc.Closer == nil {
return nil
}
return lc.Closer.Close()
}