diff --git a/internal/http/logger.go b/internal/http/logger.go index a540d1b4..3767de09 100644 --- a/internal/http/logger.go +++ b/internal/http/logger.go @@ -13,10 +13,12 @@ import ( "demodesk/neko/internal/utils" ) -type logFormatter struct{} +type logFormatter struct { + logger zerolog.Logger +} func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry { - e := logEntry{} + e := logEntry{logger: l.logger} e.req.Time = time.Now() if reqID := middleware.GetReqID(r.Context()); reqID != "" { @@ -55,6 +57,7 @@ type logEntry struct { } err error session *types.Session + logger zerolog.Logger } func (e *logEntry) SetError(err error) { @@ -70,8 +73,7 @@ func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Dur e.res.Code = status e.res.Bytes = bytes - logger := log.With(). - Str("module", "http"). + logger := e.logger.With(). Float64("elapsed", float64(elapsed.Nanoseconds())/1000000.0). Interface("req", e.req). Interface("res", e.res). diff --git a/internal/http/manager.go b/internal/http/manager.go index 6eb01532..39fa170c 100644 --- a/internal/http/manager.go +++ b/internal/http/manager.go @@ -23,7 +23,7 @@ type HttpManagerCtx struct { func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, config *config.Server) *HttpManagerCtx { logger := log.With().Str("module", "http").Logger() - router := newRouter() + router := newRouter(logger) router.UseBypass(cors.Handler(cors.Options{ AllowOriginFunc: func(r *http.Request, origin string) bool { return config.AllowOrigin(origin) diff --git a/internal/http/router.go b/internal/http/router.go index 2d79e872..0e645537 100644 --- a/internal/http/router.go +++ b/internal/http/router.go @@ -1,23 +1,25 @@ package http import ( - "demodesk/neko/internal/http/auth" - "demodesk/neko/internal/types" - "demodesk/neko/internal/utils" "net/http" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" + "github.com/rs/zerolog" + + "demodesk/neko/internal/http/auth" + "demodesk/neko/internal/types" + "demodesk/neko/internal/utils" ) type router struct { chi chi.Router } -func newRouter() *router { +func newRouter(logger zerolog.Logger) *router { r := chi.NewRouter() r.Use(middleware.RequestID) // Create a request ID for each request - r.Use(middleware.RequestLogger(&logFormatter{})) + r.Use(middleware.RequestLogger(&logFormatter{logger})) r.Use(middleware.Recoverer) // Recover from panics without crashing server return &router{r} }