Skip to content

Commit

Permalink
fix: get the right http response status code
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jun 29, 2023
1 parent 13ab586 commit 125cad0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slogecho

import (
"context"
"errors"
"net/http"
"time"

Expand Down Expand Up @@ -38,7 +39,6 @@ func New(logger *slog.Logger) echo.MiddlewareFunc {
func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
c.Path()
start := time.Now()
path := c.Path()

Expand All @@ -53,8 +53,15 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
end := time.Now()
latency := end.Sub(start)

status := c.Response().Status

httpErr := new(echo.HTTPError)
if errors.As(err, &httpErr) {
status = httpErr.Code
}

attributes := []slog.Attr{
slog.Int("status", c.Response().Status),
slog.Int("status", status),
slog.String("method", c.Request().Method),
slog.String("path", path),
slog.String("ip", c.RealIP()),
Expand Down

0 comments on commit 125cad0

Please sign in to comment.