Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export gin.defaultLogFormatter #3629

Open
franciscocpg opened this issue Jun 1, 2023 · 0 comments · May be fixed by #3916
Open

Export gin.defaultLogFormatter #3629

franciscocpg opened this issue Jun 1, 2023 · 0 comments · May be fixed by #3916

Comments

@franciscocpg
Copy link

franciscocpg commented Jun 1, 2023

Can we export the defaultLogFormatter below (DefaultLogFormatter)?

gin/logger.go

Line 132 in bb1fc2e

var defaultLogFormatter = func(param LogFormatterParams) string {

In doing so it would be very easy to configure any of the gin.LogFormatterParams, eg:

return gin.LoggerWithConfig(gin.LoggerConfig{
    Formatter: func(param gin.LogFormatterParams) string {
	   // Remove query params from log
	   param.Path = strings.Split(param.Path, "?")[0]
	   return gin.DefaultLogFormatter(param)
    }
})

Currently, to achieve the same result from the above code, I had to copy the entire defaultLogFormatter implementation just to remove the query params from log:

return gin.LoggerWithConfig(gin.LoggerConfig{
	Formatter: func(param gin.LogFormatterParams) string {
		var statusColor, methodColor, resetColor string
		if param.IsOutputColor() {
			statusColor = param.StatusCodeColor()
			methodColor = param.MethodColor()
			resetColor = param.ResetColor()
		}

		if param.Latency > time.Minute {
			param.Latency = param.Latency - param.Latency%time.Second
		}
		return fmt.Sprintf("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
			param.TimeStamp.Format("2006/01/02 - 15:04:05"),
			statusColor, param.StatusCode, resetColor,
			param.Latency,
			param.ClientIP,
			methodColor, param.Method, resetColor,
			// Remove query params from log
			strings.Split(param.Path, "?")[0],
			param.ErrorMessage,
		)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant