Gin: One query is logged twice

Created on 21 Sep 2017  路  4Comments  路  Source: gin-gonic/gin

Here is sample of my log output:
[ORM] 2017/09/21 - 16:52:08 | SQL | 0.270000ms | SELECT * FROM "transactions" ORDER BY posting_date [GIN] 2017/09/21 - 16:52:08 | 200 | 695.821碌s | 127.0.0.1 | GET /api/transactions [GIN] 2017/09/21 - 16:52:08 | 200 | 822.19碌s | 127.0.0.1 | GET /api/transactions [ORM] 2017/09/21 - 16:52:19 | SQL | 2.080000ms | SELECT * FROM "transactions" ORDER BY posting_date [GIN] 2017/09/21 - 16:52:19 | 200 | 2.338551ms | 127.0.0.1 | GET /api/transactions [GIN] 2017/09/21 - 16:52:19 | 200 | 2.437027ms | 127.0.0.1 | GET /api/transactions

Most helpful comment

Oh, you use gin.Default() and it have included r.Use(gin.Logger()) and r.Use(gin.Recovery()).
So you use either gin.Default() or r := gin.New();r.Use(gin.Logger());

All 4 comments

Hi, @vysokyj can you post your code? thanks!

Hi, here is my code snippet:

func getTransactions(c *gin.Context) {
    var transactions []Transaction
    db := c.Keys["db"].(*gorm.DB)
    db.Order("posting_date").Find(&transactions)
    c.JSON(200, transactions)
}

func database(db *gorm.DB) gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Set("db", db)
        c.Next()
    }
}

func Serve(c *Configuration) {
    db := initDatabase(c)
    defer db.Close()
    if c.DubugMode {
        gin.SetMode(gin.DebugMode)
    } else {
        gin.SetMode(gin.ReleaseMode)
    }
    r := gin.Default()
    r.Use(gin.Logger())
    r.Use(gin.Recovery())
    r.Use(database(db))
    r.GET("/", getIndex)
    r.GET("/api/transactions", getTransactions)
    r.Run()
}

This code works well before the library update.

Thanks.

Oh, you use gin.Default() and it have included r.Use(gin.Logger()) and r.Use(gin.Recovery()).
So you use either gin.Default() or r := gin.New();r.Use(gin.Logger());

Thank you very much! It looks like it was added to Default several months ago. I used this code before and I did not notice this behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xpbliss picture xpbliss  路  3Comments

gplume picture gplume  路  3Comments

ccaza picture ccaza  路  3Comments

lilee picture lilee  路  3Comments

CodingPapi picture CodingPapi  路  3Comments