-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_logger.go
192 lines (157 loc) · 3.69 KB
/
app_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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package appctx
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"github.com/spf13/pflag"
)
var (
defaultLevel = "info"
defaultType = "stdout"
defaultLogger = newAppLogger(&loggerConfig{
basePrefix: "core",
defaultLevel: "trace",
defaultType: defaultType,
})
)
type AppLogger interface {
GetLogger(prefix string) Logger
}
func GlobalLogger() AppLogger {
return defaultLogger
}
type appLogger struct {
config loggerConfig
logger *zerolog.Logger
logLevel string
logType string
logPath string
}
type loggerConfig struct {
basePrefix string
defaultLevel string
defaultType string
defaultPath string
}
func newAppLogger(config *loggerConfig) *appLogger {
if config == nil {
config = &loggerConfig{}
}
if config.defaultLevel == "" {
config.defaultLevel = defaultLevel
}
if config.defaultType == "" {
config.defaultType = defaultType
}
level := parseLogLevel(config.defaultLevel)
zerolog.SetGlobalLevel(level)
logger := getNewLogger(prdEnv, config.defaultType, config.defaultPath)
return &appLogger{
config: *config,
logger: logger,
logLevel: config.defaultLevel,
logType: config.defaultType,
logPath: config.defaultPath,
}
}
func parseLogLevel(input string) zerolog.Level {
level, err := zerolog.ParseLevel(input)
if err != nil {
log.Fatal().Err(err).Msg("Error parsing log level")
}
return level
}
func getOutputFormat(env string, writer *os.File) io.Writer {
if prdEnv == env {
return writer
}
output := zerolog.ConsoleWriter{Out: writer, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
output.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("*** %s ***", i)
}
output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%-6s:", i)
}
output.FormatFieldValue = func(i interface{}) string {
return fmt.Sprintf("%s", i)
}
return output
}
func getNewLogger(env, logType, logPath string) *zerolog.Logger {
var writer *os.File
switch logType {
case "stderr":
writer = os.Stderr
case "file":
if logPath == "" {
log.Fatal().Msg("Empty log path")
}
var err error
writer, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal().Err(err).Msg("Cannot write to log file")
}
default:
writer = os.Stdout
}
output := getOutputFormat(env, writer)
logger := zerolog.New(output).With().Timestamp().Logger()
return &logger
}
func (l *appLogger) GetLogger(prefix string) Logger {
var log *zerolog.Logger
prefix = strings.TrimSpace(prefix)
if prefix == "" {
log = l.logger
} else {
prefix = fmt.Sprintf("%s.%s", l.config.basePrefix, prefix)
lg := l.logger.With().Str("prefix", prefix).Logger()
log = &lg
}
return &logger{
Logger: log,
}
}
func (l *appLogger) ID() string {
return "logger"
}
func (l *appLogger) InitFlags() {
pflag.StringVar(
&l.logLevel,
"log-level",
l.config.defaultLevel,
"Log level (panic | fatal | error | warn | info | debug | trace) - Default: trace",
)
pflag.StringVar(
&l.logType,
"log-type",
l.config.defaultType,
"Log type (stdout | stderr | file) - Default: stdout",
)
pflag.StringVar(
&l.logPath,
"log-path",
l.config.defaultPath,
"Log path (require if log type is file) - Ex: \"./app.log\"",
)
}
func (l *appLogger) Run(ac AppContext) error {
level := parseLogLevel(l.logLevel)
zerolog.SetGlobalLevel(level)
if level <= zerolog.DebugLevel {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
}
l.logger = getNewLogger(ac.GetEnvName(), l.logType, l.logPath)
return nil
}
func (l *appLogger) Stop() error {
return nil
}