Disable request logging for a particular Go-Gin route

gin.Logger() has a longer form with args that can take in args of strings listing paths that should not be logged, i.e. gin.LoggerWithWriter(gin.DefaultWriter, "<NO LOG PATH 1>", ...).

I overwrote the base struct GinService with, baseService.GinEngine = gin.New() and then attached gin.LoggerWithWriter(...) and gin.Recovery() middlewares manually.

That did the trick.


There is also a gin.LoggerWithConfig middleware:

r.Use(gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: "/metrics"}))
type LoggerConfig struct {
    // Optional. Default value is gin.defaultLogFormatter
    Formatter LogFormatter

    // Output is a writer where logs are written.
    // Optional. Default value is gin.DefaultWriter.
    Output io.Writer

    // SkipPaths is a url path array which logs are not written.
    // Optional.
    SkipPaths []string
}

@Paul Lam's solution works! heres the code for reference:

    router := gin. Default()

Becomes

    router := gin.New()
    router.Use(
        gin.LoggerWithWriter(gin.DefaultWriter, "/pathsNotToLog/"),
        gin.Recovery(),
    )

gin.Default() definition referred from github.com/gin-gonic/[email protected]/gin.go

Tags:

Go Gin