diff options
Diffstat (limited to 'internal/http/response/json')
-rw-r--r-- | internal/http/response/json/json.go | 67 |
1 files changed, 60 insertions, 7 deletions
diff --git a/internal/http/response/json/json.go b/internal/http/response/json/json.go index 5e6024ea..8e99681a 100644 --- a/internal/http/response/json/json.go +++ b/internal/http/response/json/json.go @@ -6,10 +6,11 @@ package json // import "miniflux.app/v2/internal/http/response/json" import ( "encoding/json" "errors" + "log/slog" "net/http" + "miniflux.app/v2/internal/http/request" "miniflux.app/v2/internal/http/response" - "miniflux.app/v2/internal/logger" ) const contentTypeHeader = `application/json` @@ -48,7 +49,18 @@ func Accepted(w http.ResponseWriter, r *http.Request) { // ServerError sends an internal error to the client. func ServerError(w http.ResponseWriter, r *http.Request, err error) { - logger.Error("[HTTP:Internal Server Error] %s => %v", r.URL, err) + slog.Error(http.StatusText(http.StatusInternalServerError), + slog.Any("error", err), + slog.String("client_ip", request.ClientIP(r)), + slog.Group("request", + slog.String("method", r.Method), + slog.String("uri", r.RequestURI), + slog.String("user_agent", r.UserAgent()), + ), + slog.Group("response", + slog.Int("status_code", http.StatusInternalServerError), + ), + ) builder := response.New(w, r) builder.WithStatus(http.StatusInternalServerError) @@ -59,7 +71,18 @@ func ServerError(w http.ResponseWriter, r *http.Request, err error) { // BadRequest sends a bad request error to the client. func BadRequest(w http.ResponseWriter, r *http.Request, err error) { - logger.Error("[HTTP:Bad Request] %s => %v", r.URL, err) + slog.Warn(http.StatusText(http.StatusBadRequest), + slog.Any("error", err), + slog.String("client_ip", request.ClientIP(r)), + slog.Group("request", + slog.String("method", r.Method), + slog.String("uri", r.RequestURI), + slog.String("user_agent", r.UserAgent()), + ), + slog.Group("response", + slog.Int("status_code", http.StatusBadRequest), + ), + ) builder := response.New(w, r) builder.WithStatus(http.StatusBadRequest) @@ -70,7 +93,17 @@ func BadRequest(w http.ResponseWriter, r *http.Request, err error) { // Unauthorized sends a not authorized error to the client. func Unauthorized(w http.ResponseWriter, r *http.Request) { - logger.Error("[HTTP:Unauthorized] %s", r.URL) + slog.Warn(http.StatusText(http.StatusUnauthorized), + slog.String("client_ip", request.ClientIP(r)), + slog.Group("request", + slog.String("method", r.Method), + slog.String("uri", r.RequestURI), + slog.String("user_agent", r.UserAgent()), + ), + slog.Group("response", + slog.Int("status_code", http.StatusUnauthorized), + ), + ) builder := response.New(w, r) builder.WithStatus(http.StatusUnauthorized) @@ -81,7 +114,17 @@ func Unauthorized(w http.ResponseWriter, r *http.Request) { // Forbidden sends a forbidden error to the client. func Forbidden(w http.ResponseWriter, r *http.Request) { - logger.Error("[HTTP:Forbidden] %s", r.URL) + slog.Warn(http.StatusText(http.StatusForbidden), + slog.String("client_ip", request.ClientIP(r)), + slog.Group("request", + slog.String("method", r.Method), + slog.String("uri", r.RequestURI), + slog.String("user_agent", r.UserAgent()), + ), + slog.Group("response", + slog.Int("status_code", http.StatusForbidden), + ), + ) builder := response.New(w, r) builder.WithStatus(http.StatusForbidden) @@ -92,7 +135,17 @@ func Forbidden(w http.ResponseWriter, r *http.Request) { // NotFound sends a page not found error to the client. func NotFound(w http.ResponseWriter, r *http.Request) { - logger.Error("[HTTP:Not Found] %s", r.URL) + slog.Warn(http.StatusText(http.StatusNotFound), + slog.String("client_ip", request.ClientIP(r)), + slog.Group("request", + slog.String("method", r.Method), + slog.String("uri", r.RequestURI), + slog.String("user_agent", r.UserAgent()), + ), + slog.Group("response", + slog.Int("status_code", http.StatusNotFound), + ), + ) builder := response.New(w, r) builder.WithStatus(http.StatusNotFound) @@ -112,7 +165,7 @@ func toJSONError(err error) []byte { func toJSON(v interface{}) []byte { b, err := json.Marshal(v) if err != nil { - logger.Error("[HTTP:JSON] %v", err) + slog.Error("Unable to marshal JSON response", slog.Any("error", err)) return []byte("") } |