logging
This commit is contained in:
parent
65326b0ff5
commit
2900582eb4
@ -79,6 +79,7 @@ type contextKey string
|
||||
|
||||
const userKey contextKey = "web_user"
|
||||
const permissionsKey contextKey = "web_permissions"
|
||||
const RequestBodyKey contextKey = "request_body"
|
||||
|
||||
func SetUser(r *http.Request, user *data.User) *http.Request {
|
||||
return r.WithContext(context.WithValue(r.Context(), userKey, user))
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
@ -128,8 +129,12 @@ func ReadCSV(qs url.Values, key string, defaultValue []string) []string {
|
||||
// ── Error responses ──────────────────────────────────────────────────────────
|
||||
|
||||
func (app *Application) LogError(r *http.Request, err error) {
|
||||
app.Logger.PrintError(err, map[string]string{
|
||||
props := map[string]string{
|
||||
"request_method": r.Method,
|
||||
"request_url": r.URL.String(),
|
||||
})
|
||||
}
|
||||
if buf, ok := r.Context().Value(RequestBodyKey).(*bytes.Buffer); ok && buf.Len() > 0 {
|
||||
props["request_body"] = buf.String()
|
||||
}
|
||||
app.Logger.PrintError(err, props)
|
||||
}
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"expvar"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
@ -9,6 +12,15 @@ import (
|
||||
"github.com/felixge/httpsnoop"
|
||||
)
|
||||
|
||||
func (app *Application) CaptureBody(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
buf := &bytes.Buffer{}
|
||||
r.Body = io.NopCloser(io.TeeReader(r.Body, buf))
|
||||
ctx := context.WithValue(r.Context(), RequestBodyKey, buf)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
metricsOnce sync.Once
|
||||
metricTotalRequests *expvar.Int
|
||||
|
||||
@ -40,7 +40,7 @@ func routes(app *common.Application) http.Handler {
|
||||
apiMux.HandleFunc("GET /v1/parlament/votes/{path...}", api.GetParlVoteDetail)
|
||||
apiMux.Handle("GET /debug/vars", expvar.Handler())
|
||||
|
||||
apiChain := app.Metrics(api.RecoverPanic(app.EnableCORS(api.RateLimit(api.Authenticate(apiMux)))))
|
||||
apiChain := app.Metrics(api.RecoverPanic(app.CaptureBody(app.EnableCORS(api.RateLimit(api.Authenticate(apiMux))))))
|
||||
|
||||
// ── Web router ──────────────────────────────────────────────────────────
|
||||
web := web.Web{
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -67,7 +66,6 @@ func (l *Logger) print(level Level, message string, properties map[string]string
|
||||
Time string `json:"time"`
|
||||
Message string `json:"message"`
|
||||
Properties map[string]string `json:"properties,omitempty"`
|
||||
Trace string `json:"trace,omitempty"`
|
||||
}{
|
||||
Level: level.String(),
|
||||
Time: time.Now().UTC().Format(time.RFC3339),
|
||||
@ -75,10 +73,6 @@ func (l *Logger) print(level Level, message string, properties map[string]string
|
||||
Properties: properties,
|
||||
}
|
||||
|
||||
if level >= LevelError {
|
||||
aux.Trace = string(debug.Stack())
|
||||
}
|
||||
|
||||
var line []byte
|
||||
|
||||
line, err := json.Marshal(aux)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user