-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogging.go
48 lines (42 loc) · 1.02 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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/justinas/alice"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
func configureLogging(logLevel string) {
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
err = fmt.Errorf("Unknown Level String: '%s', defaulting to DebugLevel", level)
log.Warn().Err(err).Msg("")
log.Warn().Err(err).Msg("")
level = zerolog.DebugLevel
}
zerolog.SetGlobalLevel(level)
}
func newLoggingChain() alice.Chain {
logger := zerolog.New(os.Stdout).With().
Timestamp().
Logger()
chain := alice.New(
hlog.NewHandler(logger),
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Str("url", r.URL.String()).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
}),
hlog.RemoteAddrHandler("ip"),
hlog.UserAgentHandler("user_agent"),
hlog.RefererHandler("referer"),
)
return chain
}