Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request_logger middleware - unable to log query params #2742

Closed
firefart opened this issue Jan 31, 2025 · 3 comments
Closed

request_logger middleware - unable to log query params #2742

firefart opened this issue Jan 31, 2025 · 3 comments

Comments

@firefart
Copy link

I have the following uri format in my application: uri=/?test132. I would like to log the query with the RequestLogger middleware provided by this package (RequestLoggerWithConfig) via LogValuesFunc and slog. Unfortunately you need to specify all the Query Parameters that should be passed to LogValuesFunc on the LogQueryParams attribute, but in this case the param is parsed as the query name.
Please add an option to have access to all query params inside LogValuesFunc.

@aldas
Copy link
Contributor

aldas commented Jan 31, 2025

could you edit this example to suit your example and provide couple of CURLs to show different requests.

package main

import (
	"context"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"log/slog"
	"net/http"
	"os"
)

func main() {
	e := echo.New()
	logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
	e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
		LogStatus:   true,
		LogURI:      true,
		LogError:    true,
		HandleError: true,
		LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
			if v.Error == nil {
				//test := c.Request().URL.Query().Get("test")
				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
		},
	}))

	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "ok")
	})

	if err := e.Start(":8080"); err != nil {
		slog.Error("failed to start server")
	}
}

@firefart
Copy link
Author

ah thanks I didn't notice I have access to the full request through the echo Context

@aldas
Copy link
Contributor

aldas commented Jan 31, 2025

no problemo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants