logger wrap zerolog.

This commit is contained in:
Miroslav Šedivý 2021-09-17 19:27:21 +02:00
parent fb6042ada1
commit 7e548ec00a
3 changed files with 14 additions and 10 deletions

View File

@ -13,10 +13,12 @@ import (
"demodesk/neko/internal/utils" "demodesk/neko/internal/utils"
) )
type logFormatter struct{} type logFormatter struct {
logger zerolog.Logger
}
func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry { func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
e := logEntry{} e := logEntry{logger: l.logger}
e.req.Time = time.Now() e.req.Time = time.Now()
if reqID := middleware.GetReqID(r.Context()); reqID != "" { if reqID := middleware.GetReqID(r.Context()); reqID != "" {
@ -55,6 +57,7 @@ type logEntry struct {
} }
err error err error
session *types.Session session *types.Session
logger zerolog.Logger
} }
func (e *logEntry) SetError(err error) { 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.Code = status
e.res.Bytes = bytes e.res.Bytes = bytes
logger := log.With(). logger := e.logger.With().
Str("module", "http").
Float64("elapsed", float64(elapsed.Nanoseconds())/1000000.0). Float64("elapsed", float64(elapsed.Nanoseconds())/1000000.0).
Interface("req", e.req). Interface("req", e.req).
Interface("res", e.res). Interface("res", e.res).

View File

@ -23,7 +23,7 @@ type HttpManagerCtx struct {
func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, config *config.Server) *HttpManagerCtx { func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, config *config.Server) *HttpManagerCtx {
logger := log.With().Str("module", "http").Logger() logger := log.With().Str("module", "http").Logger()
router := newRouter() router := newRouter(logger)
router.UseBypass(cors.Handler(cors.Options{ router.UseBypass(cors.Handler(cors.Options{
AllowOriginFunc: func(r *http.Request, origin string) bool { AllowOriginFunc: func(r *http.Request, origin string) bool {
return config.AllowOrigin(origin) return config.AllowOrigin(origin)

View File

@ -1,23 +1,25 @@
package http package http
import ( import (
"demodesk/neko/internal/http/auth"
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
"net/http" "net/http"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "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 { type router struct {
chi chi.Router chi chi.Router
} }
func newRouter() *router { func newRouter(logger zerolog.Logger) *router {
r := chi.NewRouter() r := chi.NewRouter()
r.Use(middleware.RequestID) // Create a request ID for each request 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 r.Use(middleware.Recoverer) // Recover from panics without crashing server
return &router{r} return &router{r}
} }