-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
37 lines (31 loc) · 737 Bytes
/
log.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 hook
import (
"fmt"
"net/http"
"github.com/alexisvisco/kcd/pkg/errors"
"github.com/go-chi/chi/middleware"
"github.com/sirupsen/logrus"
)
// Log will log the error.
func Log(_ http.ResponseWriter, r *http.Request, err error) {
var logger *logrus.Entry
e, ok := err.(*errors.Error)
if ok {
if logrus.GetLevel() <= logrus.DebugLevel {
fmt.Println("\n" + e.Stacktrace())
}
logger = e.Log()
} else {
logger = logrus.WithError(err)
}
reqID := middleware.GetReqID(r.Context())
if reqID != "" {
logger = logger.WithField("request-id", reqID)
}
logger.WithFields(map[string]interface{}{
"remote": r.RemoteAddr,
"request": r.URL.Path,
"params": r.URL.RawQuery,
"method": r.Method,
}).Error()
}