Skip to content

Conversation

@aldas
Copy link
Contributor

@aldas aldas commented Sep 8, 2021

Added request logger middleware which helps to use custom logger library for logging requests.

Usage would be as follows:


Example for fmt.Printf

    e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
        LogStatus: true,
        LogURI:    true,
        LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
            fmt.Printf("REQUEST: uri: %v, status: %v\n", v.URI, v.Status)
            return nil
        },
    }))

Example for Zerolog (https://github.com/rs/zerolog)

    logger := zerolog.New(os.Stdout)
    e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
        LogURI:    true,
        LogStatus: true,
        LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
            logger.Info().
                Str("URI", v.URI).
                Int("status", v.Status).
                Msg("request")

            return nil
        },
    }))

Example for Zap (https://github.com/uber-go/zap)

    logger, _ := zap.NewProduction()
    e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
        LogURI:    true,
        LogStatus: true,
        LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
            logger.Info("request",
                zap.String("URI", v.URI),
                zap.Int("status", v.Status),
            )

            return nil
        },
    }))

Example for Logrus (https://github.com/sirupsen/logrus)

    log := logrus.New()
    e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
        LogURI:    true,
        LogStatus: true,
        LogValuesFunc: func(c echo.Context, values middleware.RequestLoggerValues) error {
            log.WithFields(logrus.Fields{
                "URI":    values.URI,
                "status": values.Status,
            }).Info("request")

            return nil
        },
    }))

Note about benchmarks for RequestLogger - these do not format message and therefore are not 1-to-1 comparable with Logger middleware performance.

// without header/query/form fields
BenchmarkLoggerWithConfig_withoutMapFields-6         428011          2713 ns/op        1472 B/op         24 allocs/op
BenchmarkRequestLogger_withoutMapFields-6            721497          1567 ns/op        1384 B/op         17 allocs/op

// with header/query/form fields
BenchmarkLoggerWithConfig_withMapFields-6            330147          3585 ns/op        1937 B/op         29 allocs/op
BenchmarkRequestLogger_withMapFields-6               429079          3113 ns/op        3048 B/op         28 allocs/op

@aldas aldas requested review from lammel, pafuent and vishr September 8, 2021 20:20
@codecov
Copy link

codecov bot commented Sep 8, 2021

Codecov Report

Merging #1980 (536f5e7) into master (7f502b1) will increase coverage by 0.18%.
The diff coverage is 97.56%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1980      +/-   ##
==========================================
+ Coverage   91.04%   91.22%   +0.18%     
==========================================
  Files          32       33       +1     
  Lines        2802     2884      +82     
==========================================
+ Hits         2551     2631      +80     
- Misses        160      161       +1     
- Partials       91       92       +1     
Impacted Files Coverage Δ
echo.go 94.15% <ø> (ø)
middleware/request_logger.go 97.56% <97.56%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f502b1...536f5e7. Read the comment docs.

@aldas
Copy link
Contributor Author

aldas commented Sep 8, 2021

@pafuent , @lammel or @vishr any of you please review this PR.

Copy link
Contributor

@pafuent pafuent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!!
I added some comments about minors things.
Thanks @aldas for being so active on Echo community!!!

@aldas aldas requested a review from pafuent September 9, 2021 20:06
@aldas aldas merged commit 1e7e67c into labstack:master Sep 14, 2021
@aldas aldas deleted the request_logger_mw branch January 8, 2022 15:43
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

Successfully merging this pull request may close these issues.

2 participants