Skip to content

Commit

Permalink
Merge pull request #101 from wesen/task/add-request-logging
Browse files Browse the repository at this point in the history
✨ Add simple zerolog request logging
  • Loading branch information
wesen committed May 13, 2024
2 parents 332c8d6 + 105cdf5 commit b4f7739
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
utils_fs "github.com/go-go-golems/parka/pkg/utils/fs"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"io/fs"
"net/http"
"net/http/pprof"
"time"
)

//go:embed "web/src/templates/*"
Expand Down Expand Up @@ -171,6 +173,25 @@ func WithGzip() ServerOption {
func NewServer(options ...ServerOption) (*Server, error) {
router := echo.New()

// Custom middleware logger using zerolog
router.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
log.Info().
Str("URI", v.URI).
Int("status", v.Status).
Str("method", v.Method).
Str("path", v.URIPath).
Str("host", v.Host).
Str("remote_ip", v.RemoteIP).
Dur("latency", time.Since(v.StartTime)).
Msg("request")

return nil
},
}))

s := &Server{
Router: router,
StaticPaths: []utils_fs.StaticPath{},
Expand All @@ -188,6 +209,7 @@ func NewServer(options ...ServerOption) (*Server, error) {

func (s *Server) RegisterDebugRoutes() {
handlers_ := map[string]http.HandlerFunc{
"/debug/pprof/": pprof.Index,
"/debug/pprof/cmdline": pprof.Cmdline,
"/debug/pprof/profile": pprof.Profile,
"/debug/pprof/symbol": pprof.Symbol,
Expand All @@ -196,6 +218,7 @@ func (s *Server) RegisterDebugRoutes() {
"/debug/pprof/allocs": pprof.Index,
"/debug/pprof/block": pprof.Index,
"/debug/pprof/goroutine": pprof.Index,
"/debug/pprof/heap": pprof.Index,
}

for route, handler := range handlers_ {
Expand Down

0 comments on commit b4f7739

Please sign in to comment.