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

🚀 [Feature]: fiberzap accepts custom fields #750

Closed
3 tasks done
mirusky opened this issue Aug 31, 2023 · 3 comments · Fixed by #753
Closed
3 tasks done

🚀 [Feature]: fiberzap accepts custom fields #750

mirusky opened this issue Aug 31, 2023 · 3 comments · Fixed by #753
Labels
✏️ Feature New feature or request

Comments

@mirusky
Copy link

mirusky commented Aug 31, 2023

Feature Description

I'd like to have a custom field that are user defined zap.Fields.

  • For custom structured logging
  • To add user defined fields
  • Support different entry formats, etc.

Additional Context (optional)

Currently I have an client that want's http headers response into the logging file, that's post processed later. They want all headers, now the fiberzap doesn't have an option for it. Instead of just adding a "respHeaders" field it'll be more extensible to add a custom field that are user definied fields

Code Snippet (optional)

package main

import (
	"log"

	"github.com/gofiber/contrib/fiberzap/v2"
	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"
)

func main() {
	app := fiber.New()
	logger, _ := zap.NewProduction()

	app.Use(fiberzap.New(fiberzap.Config{
		Logger: logger,
		Fields: []string{"custom"},
		CustomFieldFunc: func(c *fiber.Ctx) []zap.Field {
			var fields []zap.Field

			headers := make(map[string]interface{})

			c.Response().Header.VisitAll(func(k, v []byte) {
				headers[string(k)] = string(v)
			})

			fields = append(fields, zap.Any("headers", headers))

			return fields
		},
	}))

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})

	log.Fatal(app.Listen(":3000"))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@mirusky mirusky added the ✏️ Feature New feature or request label Aug 31, 2023
@Jamess-Lucass
Copy link
Contributor

Jamess-Lucass commented Sep 4, 2023

@mirusky Hi, I have literally just ran into a use case for this, have you started working on the feature at all? I don't want to intrude but I have written the code for it before I saw this open issue.

In my implementation I did opt for naming it FieldsFunc instead of CustomFieldsFunc

@Jamess-Lucass
Copy link
Contributor

Jamess-Lucass commented Sep 4, 2023

My use case was using this to inject trace ids into the log, so I can link together APM traces in kibana to specific log records eg.

FieldsFunc: func(c *fiber.Ctx) []zap.Field {
      tr := apm.TransactionFromContext(c.Context())
      traceId := tr.TraceContext().Trace.String()
      
      return []zap.Field{zap.String("trace.id", traceId)}
},

@mirusky
Copy link
Author

mirusky commented Sep 4, 2023

@Jamess-Lucass I didn't started at all, busy week.

I liked your approach, I was thinking on just add a new case statement for custom and call it when user pass custom on fields values. But the way you did gives more flexibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants