package logging import ( "net/http" "time" "github.com/sirupsen/logrus" ) var Logger *logrus.Logger // Init initializes the logger func Init() { Logger = logrus.New() Logger.SetFormatter(&logrus.JSONFormatter{}) Logger.SetLevel(logrus.InfoLevel) } // RequestMiddleware logs HTTP requests func RequestMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() Logger.WithFields(logrus.Fields{ "method": r.Method, "path": r.URL.Path, }).Info("Request started") next.ServeHTTP(w, r) Logger.WithFields(logrus.Fields{ "method": r.Method, "path": r.URL.Path, "duration": time.Since(start).String(), }).Info("Request completed") }) }