-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
50 lines (42 loc) · 1.25 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
package main
import (
"io"
"net/http"
"os"
"time"
"github.com/felixge/httpsnoop"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/negroni"
)
func initLogger(opts Config) {
logLevel, err := zerolog.ParseLevel(opts.LogLevel)
if err != nil {
logLevel = zerolog.InfoLevel
log.Warn().Msgf("Invalid log level '%s', fallback to '%s'", opts.LogLevel, logLevel.String())
}
if logLevel == zerolog.NoLevel {
logLevel = zerolog.InfoLevel
}
opts.LogLevel = logLevel.String()
var logWriter io.Writer = os.Stderr
if opts.LogPretty {
logWriter = &zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly}
}
zerolog.DurationFieldUnit = time.Millisecond
logger := zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger()
log.Logger = logger
zerolog.DefaultContextLogger = &logger
}
func NewLoggingMiddleware() negroni.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
metrics := httpsnoop.CaptureMetrics(next, writer, request)
log.Info().
Str("path", request.URL.Path).
Str("referer", request.Referer()).
Dur("duration", metrics.Duration).
Int("status_code", metrics.Code).
Int64("response_size", metrics.Written).
Msg("Handled request")
}
}