neko/server/internal/http/logger.go

136 lines
2.9 KiB
Go
Raw Permalink 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"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/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{}
}
req := map[string]any{}
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
session types.Session
}
2021-09-18 06:20:10 +12:00
type logPanic struct {
message string
stack string
}
func (e *logEntry) Panic(v any, stack []byte) {
2021-09-18 06:20:10 +12:00
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
2021-09-18 05:22:49 +12:00
}
func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra any) {
res := map[string]any{}
2021-09-18 06:20:10 +12:00
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 any, 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 any) {
2021-09-18 06:35:41 +12:00
}