Gin framework - logger implement

Hello,
I am new to Go Lang,

I choose Gin framework to develop web services.

Can anyone help me how to write log files in gin middleware?

func Logger() gin.HandlerFunc {
    return func(c *gin.Context) {
        // before
        // how to convert context object to string?
       
        c.Next()
        // before
        // how to convert context object to string?
    }
}

You can’t “convert” a gin.Context to a string as it is a struct that wraps (amongst other things), the request, response and URL parameters. Logging all of those would not be sensible.

You should call log.Printf(...) on the specific fields you want to log.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.