neko/internal/http/logger.go

136 lines
3.0 KiB
Go
Raw Normal View History

2020-10-30 10:12:33 +13:00
package http
import (
"fmt"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
2021-09-17 10:58:50 +12:00
"github.com/rs/zerolog"
2022-03-20 23:43:00 +13:00
"gitlab.com/demodesk/neko/server/pkg/types"
"gitlab.com/demodesk/neko/server/pkg/utils"
2021-09-17 10:24:33 +12: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 {
// exclude health & metrics from logs
if r.RequestURI == "/health" || r.RequestURI == "/metrics" {
2021-09-18 06:35:41 +12:00
return &nulllog{}
}
2021-09-18 06:20:10 +12:00
req := map[string]interface{}{}
2021-09-17 10:24:33 +12:00
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
2021-09-18 06:20:10 +12:00
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-18 06:20:10 +12:00
req["scheme"] = scheme
req["proto"] = r.Proto
req["method"] = r.Method
req["remote"] = r.RemoteAddr
req["agent"] = r.UserAgent()
req["uri"] = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
return &logEntry{
logger: l.logger.With().Interface("req", req).Logger(),
}
}
2021-09-18 05:22:49 +12:00
type logEntry struct {
2021-09-18 06:20:10 +12:00
logger zerolog.Logger
2021-09-18 05:22:49 +12:00
err error
2021-09-18 06:20:10 +12:00
panic *logPanic
2021-09-18 05:22:49 +12:00
session *types.Session
}
2021-09-18 06:20:10 +12:00
type logPanic struct {
message string
stack string
}
func (e *logEntry) Panic(v interface{}, stack []byte) {
e.panic = &logPanic{
message: fmt.Sprintf("%+v", v),
stack: string(stack),
}
}
func (e *logEntry) Error(err error) {
2021-09-17 10:24:33 +12:00
e.err = err
}
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{}) {
2021-09-18 06:20:10 +12:00
res := map[string]interface{}{}
res["time"] = time.Now().UTC().Format(time.RFC1123)
res["status"] = status
res["bytes"] = bytes
res["elapsed"] = float64(elapsed.Nanoseconds()) / 1000000.0
2021-09-17 10:58:50 +12:00
2021-09-18 06:20:10 +12:00
logger := e.logger.With().Interface("res", res).Logger()
2021-09-17 10:24:33 +12:00
2021-09-18 06:20:10 +12:00
// add session ID to logs (if exists)
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
}
2021-09-18 06:20:10 +12:00
// handle panic error message
if e.panic != nil {
logger.WithLevel(zerolog.PanicLevel).
Err(e.err).
Str("stack", e.panic.stack).
Msgf("request failed (%d): %s", status, e.panic.message)
return
}
// handle panic error message
2021-09-17 10:24:33 +12:00
if e.err != nil {
httpErr, ok := e.err.(*utils.HTTPError)
if !ok {
2021-09-18 06:20:10 +12:00
logger.Err(e.err).Msgf("request failed (%d)", status)
2021-09-17 10:24:33 +12:00
return
}
if httpErr.Message == "" {
httpErr.Message = http.StatusText(httpErr.Code)
}
2021-09-18 06:20:10 +12:00
var logLevel zerolog.Level
2021-09-17 10:58:50 +12:00
if httpErr.Code < 500 {
2021-09-18 06:20:10 +12:00
logLevel = zerolog.WarnLevel
2021-09-17 10:58:50 +12:00
} else {
2021-09-18 06:20:10 +12:00
logLevel = zerolog.ErrorLevel
2021-09-17 10:58:50 +12:00
}
2021-09-17 10:24:33 +12:00
message := httpErr.Message
if httpErr.InternalMsg != "" {
message = httpErr.InternalMsg
}
2021-09-18 06:20:10 +12:00
logger.WithLevel(logLevel).Err(httpErr.InternalErr).Msgf("request failed (%d): %s", status, message)
2021-09-17 10:24:33 +12:00
return
}
2021-09-18 06:20:10 +12:00
logger.Debug().Msgf("request complete (%d)", status)
2021-09-18 05:22:49 +12:00
}
2021-09-18 06:35:41 +12:00
type nulllog struct{}
func (e *nulllog) Panic(v interface{}, stack []byte) {}
func (e *nulllog) Error(err error) {}
func (e *nulllog) SetSession(session types.Session) {}
func (e *nulllog) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
}