Gin: Ignore specified from default router logging?

Created on 6 Mar 2019  路  11Comments  路  Source: gin-gonic/gin

Say I have the following application

func main() {
    auth := authority.Create(authConfig)

    r := gin.Default()
    r.GET("/ping", handler.PingGet)

    base := r.Group(baseURL)

    oauth := base.Group("/oauth")
    oauth.GET("/login", handler.LoginGet(oAuthConfig))
    oauth.GET("/callback", handler.CallbackGet(oAuthConfig))

    api := base.Group("/api")
    api.Use(auth.TokenMiddleware())
    api.GET("/whoami", handler.WhoAmIGet)
    api.GET("/overview", handler.OverviewGet())

    r.NoRoute(handler.SPAGet(publicFolder, baseURL))
    r.Run("0.0.0.0:5000")
}

By default gin.Default() will print a log for all routes.
I'd like to ignore /ping and everything on /oauth

Can I configure the default router to do that, or must I build my own?

question

Most helpful comment

You can custom the gin logger.

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    // Creates a router without any middleware by default
    r := gin.New()

    // Global middleware
    // Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release.
    // By default gin.DefaultWriter = os.Stdout
    r.Use(gin.LoggerWithConfig(gin.LoggerConfig{
        SkipPaths: []string{"/ping_1"},
    }))

    // Recovery middleware recovers from any panics and writes a 500 if there was one.
    r.Use(gin.Recovery())

    r.GET("/ping_1", func(c *gin.Context) {
        c.String(200, "pong_1")
    })

    r.GET("/ping_2", func(c *gin.Context) {
        c.String(200, "pong_2")
    })

    // Listen and serve on 0.0.0.0:8080
    r.Run(":8080")
}

All 11 comments

You can custom the gin logger.

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    // Creates a router without any middleware by default
    r := gin.New()

    // Global middleware
    // Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release.
    // By default gin.DefaultWriter = os.Stdout
    r.Use(gin.LoggerWithConfig(gin.LoggerConfig{
        SkipPaths: []string{"/ping_1"},
    }))

    // Recovery middleware recovers from any panics and writes a 500 if there was one.
    r.Use(gin.Recovery())

    r.GET("/ping_1", func(c *gin.Context) {
        c.String(200, "pong_1")
    })

    r.GET("/ping_2", func(c *gin.Context) {
        c.String(200, "pong_2")
    })

    // Listen and serve on 0.0.0.0:8080
    r.Run(":8080")
}

@appleboy If this feature landed in gin 1.3?

BTW, i am using following workaround currently.
image

LoggerWithConfig

I don't seem to have access to this when using

go get -u github.com/gin-gonic/gin

Any idea why I wouldn't have the latest logger.go?

@alshdavid What is your go vendor tool? Or try to use the go module in go1.11 version.

I am using go modules and go 1.12. It is pulling version 1.3

On a fresh project

go mod init newproject
go get -u github.com/gin-gonic/gin

The source I get for gin doesn't match the source I see in Github.
Does go cache modules?

Try

go get -u github.com/gin-gonic/gin@master

or specific version

go get -u github.com/gin-gonic/[email protected]

@alshdavid I think you use go mod not go get....
step1: run go mod init github.com/thinkerou/gintest
step2: write code, import gin package
step3: run go mod tidy, not use go get command

@thinkerou go get will get the latest version from github release page.

Currently, LoggerWithConfig is only in master branch.
It is a new feature for future release v1.4.

LoggerWithConfig rerate

I closed the issue. You can download the v1.5 version or try gin-logger.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdnight picture mdnight  路  3Comments

ccaza picture ccaza  路  3Comments

gplume picture gplume  路  3Comments

cxk280 picture cxk280  路  3Comments

frederikhors picture frederikhors  路  3Comments