From 399cfd827d4aeee29bded9b8ad6e2e5814b2dcc6 Mon Sep 17 00:00:00 2001 From: Michael Thomson Date: Wed, 19 Jun 2024 11:46:22 -0400 Subject: [PATCH] logging --- main.go | 3 ++- middleware/logging.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 middleware/logging.go diff --git a/main.go b/main.go index 04f8bf4..e40da03 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/jackc/pgx/v5/pgxpool" "github.com/joho/godotenv" "michaelthomson.dev/mthomson/go-todos-app/handlers" + "michaelthomson.dev/mthomson/go-todos-app/middleware" "michaelthomson.dev/mthomson/go-todos-app/repositories" "michaelthomson.dev/mthomson/go-todos-app/services" ) @@ -130,7 +131,7 @@ func main() { server := http.Server{ Addr: "localhost:3000", - Handler: router, + Handler: middleware.LoggingMiddleware(router), } log.Fatal(server.ListenAndServe()) diff --git a/middleware/logging.go b/middleware/logging.go new file mode 100644 index 0000000..c73b656 --- /dev/null +++ b/middleware/logging.go @@ -0,0 +1,15 @@ +package middleware + +import ( + "log" + "net/http" +) + +func LoggingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + log.Printf("[%s] %s", r.Method, r.URL.EscapedPath()) + next.ServeHTTP(w, r) + }, + ) +}