Skip to content

Commit

Permalink
Use slog logger for echo logging
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim committed Aug 26, 2024
1 parent 9a7d1fe commit c3df49e
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions cmd/arc/services/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package cmd
import (
"context"
"fmt"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_core"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_jetstream"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/nats_connection"
"log/slog"
"net/http"
"net/url"
"time"

"github.com/bitcoin-sv/arc/config"
"github.com/bitcoin-sv/arc/internal/blocktx/blocktx_api"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_core"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_jetstream"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/nats_connection"
"github.com/bitcoin-sv/arc/internal/metamorph/metamorph_api"
"github.com/bitcoin-sv/arc/pkg/api"
"github.com/bitcoin-sv/arc/pkg/api/handler"
"github.com/bitcoin-sv/arc/pkg/blocktx"
"github.com/bitcoin-sv/arc/pkg/metamorph"

"github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware"
"github.com/ordishs/go-bitcoin"
Expand All @@ -39,8 +40,27 @@ func StartAPIServer(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), e
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}))

// use the standard echo logger
e.Use(echomiddleware.Logger())
e.Use(echomiddleware.RequestLoggerWithConfig(echomiddleware.RequestLoggerConfig{
LogStatus: true,
LogURI: true,
LogError: true,
HandleError: true, // forwards error to the global error handler, so it can decide appropriate status code
LogValuesFunc: func(c echo.Context, v echomiddleware.RequestLoggerValues) error {
if v.Error == nil {
logger.LogAttrs(context.Background(), slog.LevelInfo, "REQUEST",
slog.String("uri", v.URI),
slog.Int("status", v.Status),
)
} else {
logger.LogAttrs(context.Background(), slog.LevelError, "REQUEST_ERROR",
slog.String("uri", v.URI),
slog.Int("status", v.Status),
slog.String("err", v.Error.Error()),
)
}
return nil
},
}))

// load the ARC handler from config
// If you want to customize this for your own server, see examples dir
Expand Down

0 comments on commit c3df49e

Please sign in to comment.