/
logger.go
143 lines (125 loc) · 3.38 KB
/
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
package server
import (
"errors"
"fmt"
"os"
"path"
"time"
"github.com/fabienbellanger/goutils"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// InitLogger initializes custom Zap logger configuration.
func InitLogger() (*zap.Logger, error) {
// Logs outputs
// ------------
outputs, err := getLoggerOutputs(viper.GetStringSlice("LOG_OUTPUTS"), viper.GetString("APP_NAME"), viper.GetString("LOG_PATH"))
if err != nil {
return nil, err
}
// Level
// -----
level := getLoggerLevel(viper.GetString("LOG_LEVEL"), viper.GetString("APP_ENV"))
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(level),
OutputPaths: outputs,
ErrorOutputPaths: outputs,
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.RFC3339TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
logger, err := cfg.Build()
if err != nil {
return zap.NewProduction()
}
return logger, nil
}
// getLoggerOutputs returns an array with the log outputs.
// Outputs can be stdout and/or file.
func getLoggerOutputs(logOutputs []string, appName, filePath string) (outputs []string, err error) {
if goutils.StringInSlice("file", logOutputs) {
logPath := path.Clean(filePath)
_, err := os.Stat(logPath)
if err != nil {
return nil, err
}
if appName == "" {
return nil, errors.New("no APP_NAME variable defined")
}
outputs = append(outputs, fmt.Sprintf("%s/%s.log",
logPath,
appName))
}
if goutils.StringInSlice("stdout", logOutputs) {
outputs = append(outputs, "stdout")
}
return
}
// getLoggerLevel returns the minimum log level.
// If nothing is specified in the environment variable LOG_LEVEL,
// The level is DEBUG in development mode and WARN in others cases.
func getLoggerLevel(l string, env string) (level zapcore.Level) {
switch l {
case "debug":
level = zapcore.DebugLevel
case "info":
level = zapcore.InfoLevel
case "warn":
level = zapcore.WarnLevel
case "error":
level = zapcore.ErrorLevel
case "panic":
level = zapcore.PanicLevel
case "fatal":
level = zapcore.FatalLevel
default:
if env == "development" {
level = zapcore.DebugLevel
} else {
level = zapcore.WarnLevel
}
}
return
}
// zapLogger is a middleware and zap to provide an "access log" like logging for each request.
// TODO: https://github.com/gofiber/contrib/tree/main/fiberzap
func zapLogger(log *zap.Logger) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
start := time.Now().UTC()
err := next(c)
if err != nil {
c.Error(err)
}
stop := time.Since(start)
req := c.Request()
res := c.Response()
fields := []zapcore.Field{
zap.Int("code", res.Status),
zap.String("method", req.Method),
zap.String("path", req.URL.Path),
zap.String("host", req.Host),
zap.String("ip", c.RealIP()),
zap.String("userAgent", req.UserAgent()),
zap.String("latency", stop.String()),
zap.String("protocol", req.Proto),
}
id := req.Header.Get(echo.HeaderXRequestID)
if id == "" {
id = res.Header().Get(echo.HeaderXRequestID)
fields = append(fields, zap.String("requestId", id))
}
log.Info("", fields...)
return nil
}
}
}