generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
37 lines (33 loc) · 877 Bytes
/
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
package middleware
import (
"github.com/broadinstitute/sherlock/internal/auth"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"time"
)
func Logger() gin.HandlerFunc {
return func(ctx *gin.Context) {
t := time.Now()
ctx.Next()
path := ctx.Request.URL.Path
if ctx.Request.URL.RawQuery != "" {
path = path + "?" + ctx.Request.URL.RawQuery
}
identity := "client not identified"
if user, err := auth.ExtractUserFromContext(ctx); err == nil {
identity = user.Email
}
var event *zerolog.Event
switch code := ctx.Writer.Status(); {
case code >= 500:
event = log.Error()
case code >= 400:
event = log.Warn()
default:
event = log.Info()
}
event.Msgf("GIN | %3d | %14s | %15s | %30s | %-7s %s",
ctx.Writer.Status(), time.Since(t).String(), ctx.ClientIP(), identity, ctx.Request.Method, path)
}
}