-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
123 lines (110 loc) · 3.22 KB
/
logging.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package suzu
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/natefinch/lumberjack.v2"
)
// InitLogger ロガーを初期化する
func InitLogger(config *Config) error {
if f, err := os.Stat(config.LogDir); os.IsNotExist(err) || !f.IsDir() {
return err
}
logPath := fmt.Sprintf("%s/%s", config.LogDir, config.LogName)
// https://github.com/rs/zerolog/issues/77
zerolog.TimestampFunc = func() time.Time {
return time.Now().UTC()
}
zerolog.TimeFieldFormat = "2006-01-02T15:04:05.000000Z"
if config.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
if config.Debug && config.LogStdout {
writer := zerolog.ConsoleWriter{
Out: os.Stdout,
FormatTimestamp: func(i interface{}) string {
darkGray := "\x1b[90m"
reset := "\x1b[0m"
return strings.Join([]string{darkGray, i.(string), reset}, "")
},
NoColor: false,
}
prettyFormat(&writer)
log.Logger = zerolog.New(writer).With().Caller().Timestamp().Logger()
} else if config.LogStdout {
writer := os.Stdout
log.Logger = zerolog.New(writer).With().Caller().Timestamp().Logger()
} else {
var logRotateMaxSize, logRotateMaxBackups, logRotateMaxAge int
if config.LogRotateMaxSize == 0 {
logRotateMaxSize = DefaultLogRotateMaxSize
}
if config.LogRotateMaxBackups == 0 {
logRotateMaxBackups = DefaultLogRotateMaxBackups
}
if config.LogRotateMaxAge == 0 {
logRotateMaxAge = DefaultLogRotateMaxAge
}
writer := &lumberjack.Logger{
Filename: logPath,
MaxSize: logRotateMaxSize,
MaxBackups: logRotateMaxBackups,
MaxAge: logRotateMaxAge,
Compress: false,
}
log.Logger = zerolog.New(writer).With().Caller().Timestamp().Logger()
}
return nil
}
// 現時点での prettyFormat
// 2023-04-17 12:51:56.333485Z [INFO] config.go:102 > CONF | debug=true
func prettyFormat(w *zerolog.ConsoleWriter) {
const Reset = "\x1b[0m"
w.FormatLevel = func(i interface{}) string {
var color, level string
// TODO: 各色を定数に置き換える
// TODO: 他の logLevel が必要な場合は追加する
switch i.(string) {
case "info":
color = "\x1b[32m"
case "error":
color = "\x1b[31m"
case "warn":
color = "\x1b[33m"
case "debug":
color = "\x1b[34m"
default:
color = "\x1b[37m"
}
level = strings.ToUpper(i.(string))
return fmt.Sprintf("%s[%s]%s", color, level, Reset)
}
w.FormatCaller = func(i interface{}) string {
return fmt.Sprintf("[%s]", filepath.Base(i.(string)))
}
// TODO: Caller をファイル名と行番号だけの表示で出力する
// 以下のようなフォーマットにしたい
// 2023-04-17 12:50:09.334758Z [INFO] [config.go:102] CONF | debug=true
// TODO: name=value が無い場合に | を消す方法がわからなかった
w.FormatMessage = func(i interface{}) string {
if i == nil {
return ""
} else {
return fmt.Sprintf("%s |", i)
}
}
w.FormatFieldName = func(i interface{}) string {
const Cyan = "\x1b[36m"
return fmt.Sprintf("%s%s=%s", Cyan, i, Reset)
}
// TODO: カンマ区切りを同実現するかわからなかった
w.FormatFieldValue = func(i interface{}) string {
return fmt.Sprintf("%s", i)
}
}