neko/internal/http/logger.go

143 lines
2.9 KiB
Go
Raw Normal View History

2020-10-30 10:12:33 +13:00
package http
import (
2021-09-17 10:24:33 +12:00
"context"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
2021-09-17 10:58:50 +12:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2021-09-17 10:24:33 +12:00
"demodesk/neko/internal/http/auth"
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
)
2021-09-17 10:24:33 +12:00
type logEntryKey int
2021-03-20 03:04:01 +13:00
2021-09-17 10:24:33 +12:00
const logEntryKeyCtx logEntryKey = iota
2021-09-17 10:58:50 +12:00
func setLogEntry(r *http.Request, data *logEntry) *http.Request {
2021-09-17 10:24:33 +12:00
ctx := context.WithValue(r.Context(), logEntryKeyCtx, data)
return r.WithContext(ctx)
}
2021-09-17 10:58:50 +12:00
func getLogEntry(r *http.Request) *logEntry {
return r.Context().Value(logEntryKeyCtx).(*logEntry)
2021-09-17 10:24:33 +12:00
}
2021-09-17 10:24:33 +12:00
func LoggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-09-17 10:58:50 +12:00
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
logEntry := newLogEntry(w, r)
defer func() {
logEntry.Write(ww.Status(), ww.BytesWritten())
}()
next.ServeHTTP(ww, setLogEntry(r, logEntry))
2021-09-17 10:24:33 +12:00
})
}
2021-09-17 10:24:33 +12:00
type logEntry struct {
req struct {
2021-09-17 10:58:50 +12:00
Time time.Time
Id string
Scheme string
Proto string
Method string
Remote string
Agent string
Uri string
2021-09-17 10:24:33 +12:00
}
res struct {
2021-09-17 10:58:50 +12:00
Time time.Time
Code int
Bytes int
2021-09-17 10:24:33 +12:00
}
err error
elapsed time.Duration
hasSession bool
session types.Session
}
2021-09-17 10:58:50 +12:00
func newLogEntry(w http.ResponseWriter, r *http.Request) *logEntry {
2021-09-17 10:24:33 +12:00
e := logEntry{}
2021-09-17 10:58:50 +12:00
e.req.Time = time.Now()
2021-09-17 10:24:33 +12:00
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
2021-09-17 10:58:50 +12:00
e.req.Id = reqID
2021-09-17 10:24:33 +12:00
}
2021-09-17 10:24:33 +12:00
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
2021-09-17 10:24:33 +12:00
2021-09-17 10:58:50 +12:00
e.req.Scheme = scheme
e.req.Proto = r.Proto
e.req.Method = r.Method
e.req.Remote = r.RemoteAddr
e.req.Agent = r.UserAgent()
e.req.Uri = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
return &e
}
2021-09-17 10:24:33 +12:00
func (e *logEntry) SetResponse(w http.ResponseWriter, r *http.Request) {
2021-09-17 10:58:50 +12:00
e.res.Time = time.Now()
2021-09-17 10:24:33 +12:00
2021-09-17 10:58:50 +12:00
e.elapsed = e.res.Time.Sub(e.req.Time)
2021-09-17 10:24:33 +12:00
e.session, e.hasSession = auth.GetSession(r)
}
2021-09-17 10:24:33 +12:00
func (e *logEntry) SetError(err error) {
e.err = err
}
2021-09-17 10:58:50 +12:00
func (e *logEntry) Write(status, bytes int) {
e.res.Code = status
e.res.Bytes = bytes
2021-09-17 10:24:33 +12:00
logger := log.With().
Str("module", "http").
Float64("elapsed", float64(e.elapsed.Nanoseconds())/1000000.0).
Interface("req", e.req).
Interface("res", e.res).
Logger()
if e.hasSession {
logger = logger.With().Str("session_id", e.session.ID()).Logger()
}
if e.err != nil {
httpErr, ok := e.err.(*utils.HTTPError)
if !ok {
2021-09-17 10:58:50 +12:00
logger.Err(e.err).Msgf("request failed (%d)", e.res.Code)
2021-09-17 10:24:33 +12:00
return
}
if httpErr.Message == "" {
httpErr.Message = http.StatusText(httpErr.Code)
}
2021-09-17 10:58:50 +12:00
var logEvent *zerolog.Event
if httpErr.Code < 500 {
logEvent = logger.Warn()
} else {
logEvent = logger.Error()
}
2021-09-17 10:24:33 +12:00
message := httpErr.Message
if httpErr.InternalMsg != "" {
message = httpErr.InternalMsg
}
2021-09-17 10:58:50 +12:00
logEvent.Err(httpErr.InternalErr).Msgf("request failed (%d): %s", e.res.Code, message)
2021-09-17 10:24:33 +12:00
return
}
2021-09-17 10:58:50 +12:00
logger.Debug().Msgf("request complete (%d)", e.res.Code)
}