Added a custom logger for dev and production.
This commit is contained in:
@@ -5,8 +5,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
@@ -14,6 +17,46 @@ type contextKey string
|
||||
|
||||
const userIDKey contextKey = "userID"
|
||||
|
||||
func (s *Server) logRequest(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
// 1. Wrap the response writer
|
||||
// This intercepts the Status code when it's written later in the chain
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
|
||||
// 2. Call the next handler in the chain
|
||||
next.ServeHTTP(ww, r)
|
||||
|
||||
// 3. Capture the actual results
|
||||
status := ww.Status()
|
||||
duration := time.Since(start)
|
||||
|
||||
// 4. Build the log attributes
|
||||
// Using slog.Group keeps the "request" data organized
|
||||
requestGroup := slog.Group("request",
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.URL.Path),
|
||||
slog.String("ip", r.RemoteAddr),
|
||||
)
|
||||
|
||||
// 5. Log with appropriate level based on status
|
||||
if status >= 400 {
|
||||
s.logger.Error("HTTP Request Error",
|
||||
requestGroup,
|
||||
slog.Int("status", status),
|
||||
slog.Duration("took", duration),
|
||||
)
|
||||
} else {
|
||||
s.logger.Info("HTTP Request",
|
||||
requestGroup,
|
||||
slog.Int("status", status),
|
||||
slog.Duration("took", duration),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) hasAuth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var tokenString string
|
||||
|
||||
@@ -10,7 +10,10 @@ import (
|
||||
func (s *Server) setupRoutes() *chi.Mux {
|
||||
router := chi.NewRouter()
|
||||
|
||||
router.Use(middleware.Logger)
|
||||
// Global Middleware
|
||||
router.Use(middleware.RequestID)
|
||||
router.Use(middleware.RealIP)
|
||||
router.Use(s.logRequest)
|
||||
router.Use(middleware.Recoverer)
|
||||
|
||||
// TODO: CLEANUP: Not sure this is needed right now
|
||||
|
||||
@@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"nfeeder/internal/db"
|
||||
@@ -12,15 +13,16 @@ var MAX_USER_SESSIONS = 3
|
||||
|
||||
type Server struct {
|
||||
httpServer *http.Server
|
||||
logger *slog.Logger
|
||||
store *db.Store
|
||||
jwtSecret []byte
|
||||
}
|
||||
|
||||
func NewServer(store *db.Store, secret string) *Server {
|
||||
func NewServer(store *db.Store, logger *slog.Logger, secret string) *Server {
|
||||
s := &Server{
|
||||
store: store,
|
||||
// could extend this to something more generic
|
||||
// will do if more fields needed
|
||||
store: store,
|
||||
logger: logger,
|
||||
// could extend this to something more generic will do if more fields needed
|
||||
jwtSecret: []byte(secret),
|
||||
}
|
||||
|
||||
@@ -33,9 +35,12 @@ func NewServer(store *db.Store, secret string) *Server {
|
||||
|
||||
func (s *Server) Start(addr string) error {
|
||||
s.httpServer.Addr = addr
|
||||
s.logger.Info("server starting", "addr", addr)
|
||||
|
||||
return s.httpServer.ListenAndServe()
|
||||
}
|
||||
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
s.logger.Info("shutting down http server")
|
||||
return s.httpServer.Shutdown(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user