2020-10-30 10:12:33 +13:00
|
|
|
package http
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2021-09-17 10:58:50 +12:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-18 05:27:21 +12:00
|
|
|
type logFormatter struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
}
|
2021-03-20 03:04:01 +13:00
|
|
|
|
2021-09-18 05:22:49 +12:00
|
|
|
func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
|
2021-09-18 05:27:21 +12:00
|
|
|
e := logEntry{logger: l.logger}
|
2021-09-17 10:58:50 +12:00
|
|
|
e.req.Time = time.Now()
|
2020-10-23 03:54:50 +13:00
|
|
|
|
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
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
scheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
scheme = "https"
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
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
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-18 05:22:49 +12:00
|
|
|
type logEntry struct {
|
|
|
|
req struct {
|
|
|
|
Time time.Time
|
|
|
|
Id string
|
|
|
|
Scheme string
|
|
|
|
Proto string
|
|
|
|
Method string
|
|
|
|
Remote string
|
|
|
|
Agent string
|
|
|
|
Uri string
|
|
|
|
}
|
|
|
|
res struct {
|
|
|
|
Time time.Time
|
|
|
|
Code int
|
|
|
|
Bytes int
|
|
|
|
}
|
|
|
|
err error
|
|
|
|
session *types.Session
|
2021-09-18 05:27:21 +12:00
|
|
|
logger zerolog.Logger
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func (e *logEntry) SetError(err error) {
|
|
|
|
e.err = err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-18 05:22:49 +12:00
|
|
|
func (e *logEntry) SetSession(session types.Session) {
|
|
|
|
e.session = &session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
|
|
|
e.res.Time = time.Now()
|
2021-09-17 10:58:50 +12:00
|
|
|
e.res.Code = status
|
|
|
|
e.res.Bytes = bytes
|
|
|
|
|
2021-09-18 05:27:21 +12:00
|
|
|
logger := e.logger.With().
|
2021-09-18 05:22:49 +12:00
|
|
|
Float64("elapsed", float64(elapsed.Nanoseconds())/1000000.0).
|
2021-09-17 10:24:33 +12:00
|
|
|
Interface("req", e.req).
|
|
|
|
Interface("res", e.res).
|
|
|
|
Logger()
|
|
|
|
|
2021-09-18 05:22:49 +12:00
|
|
|
if e.session != nil {
|
|
|
|
logger = logger.With().Str("session_id", (*e.session).ID()).Logger()
|
2021-09-17 10:24:33 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:58:50 +12:00
|
|
|
logger.Debug().Msgf("request complete (%d)", e.res.Code)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-09-18 05:22:49 +12:00
|
|
|
|
|
|
|
func (e *logEntry) Panic(v interface{}, stack []byte) {
|
|
|
|
message := fmt.Sprintf("%+v", v)
|
|
|
|
|
|
|
|
log.Fatal().
|
|
|
|
Str("message", message).
|
|
|
|
Str("stack", string(stack)).
|
|
|
|
Msg("got HTTP panic")
|
|
|
|
}
|