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

Develop a custom logging middleware to replace ginzap #28

Open
vbelouso opened this issue Aug 31, 2023 · 0 comments
Open

Develop a custom logging middleware to replace ginzap #28

vbelouso opened this issue Aug 31, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request hold Something needs to block this issue from merging

Comments

@vbelouso
Copy link
Collaborator

The current ginzap package provides the following log lines:

{
  "level": "info",
  "timestamp": "2023-08-31T14:33:44.827+0200",
  "msg": "/clusters",
  "status": 200,
  "method": "GET",
  "path": "/clusters",
  "query": "",
  "ip": "127.0.0.1",
  "latency": 0.110493355,
  "time": "2023-08-31T12:33:44Z"
}

There are several problems with this:

  • msg and path are the same. But we don't want to see a URL path in the msg field.
    The issue is here
    Ginzap passes the path as the first parameter to Zap function log.Info which expected message.

  • duplicated fields timestamp and time. Dirty hack for that - don't pass TimeFormat to Ginzap
    Like:

router.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{UTC: true}))

A useful option might be to create our custom middleware

For example

func SetLogger(l *zap.Logger) gin.HandlerFunc {
	return func(c *gin.Context) {
		t1 := time.Now()
		defer func() {
			l.Info("served request",
				zap.String("proto", c.Request.Proto),
				zap.String("method", c.Request.Method),
				zap.String("path", c.Request.URL.Path),
				zap.Duration("lat", time.Since(t1)),
				zap.Int("status", c.Writer.Status()),
				zap.Int("size", c.Writer.Size()),
			)
		}()
	}
}
@vbelouso vbelouso self-assigned this Aug 31, 2023
@vbelouso vbelouso added the enhancement New feature or request label Aug 31, 2023
@r2dedios r2dedios added this to the 0.1-alpha milestone Sep 13, 2023
@r2dedios r2dedios changed the title ginzap incorrect log lines Develop a custom logging middleware to replace ginzap Sep 13, 2023
@r2dedios r2dedios modified the milestones: 0.1-alpha, 0.3 Jun 19, 2024
@r2dedios r2dedios removed this from the 0.3 milestone Dec 4, 2024
@r2dedios r2dedios added the hold Something needs to block this issue from merging label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hold Something needs to block this issue from merging
Projects
None yet
Development

No branches or pull requests

2 participants