- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
I noticed in the logs that every handler is called twice when I send a request to my backend, don't know whether API is called twice or there is a problem in the gin logger
How to reproduce
func main() {
r := gin.Default()
r.Use(gin.Logger())
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "health ok",
})
})
r.GET("/scrape", <functions>)
log.Printf("Server Started, version: %s", version)
r.Run(":8080")
}
Expectations
[GIN] 2023/10/06 - 01:34:59 | 200 | 1.885762ms | ::1 | GET /
Actual result
[GIN] 2023/10/06 - 01:34:59 | 200 | 1.885762ms | ::1 | GET /
[GIN] 2023/10/06 - 01:34:59 | 200 | 1.250383ms | ::1 | GET /
Environment
- go version: go version go1.20.6 windows/amd64
- gin version (or commit ref): github.com/gin-gonic/gin v1.9.1
- operating system: windows/amd64
Comment From: swarajkumarsingh
The Perfect Solution 💕
Isuue: gin logger is logging the request info twice
Solution: Gin by default always uses its logger, and if you try to add logger again r.Use(gin.Logger()), then the logs are written twice because gin logger is called twice. So the solution is that you should not call gin.Logger is gin is already using it.
Solution in Code:
func main() {
r := gin.Default()
// ** r.Use(gin.Logger()) - don't call logs gin longer again**
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "health ok",
})
})
r.GET("/scrape", controller.<function>)
log.Printf("Server Started, version: %s", version)
r.Run(":8080")
}
Thank you Linkedin: https://www.linkedin.com/in/swarajkumarsingh/
Comment From: zanmato
The logger is added in gin.Default()
, use gin.New()
if you want to do it yourself with some custom logger.
Don't forget to close the issue
Comment From: swarajkumarsingh
The logger is added in
gin.Default()
, usegin.New()
if you want to do it yourself with some custom logger. Don't forget to close the issue
Sure, Thanks!