mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
logger wrap zerolog.
This commit is contained in:
parent
fb6042ada1
commit
7e548ec00a
@ -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).
|
||||
|
@ -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)
|
||||
|
@ -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}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user