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

🤗 [Question]: How to log a custom header #1215

Closed
3 tasks done
kirankumar-grootan opened this issue Dec 28, 2024 · 2 comments
Closed
3 tasks done

🤗 [Question]: How to log a custom header #1215

kirankumar-grootan opened this issue Dec 28, 2024 · 2 comments
Labels
🤔 Question Further information is requested

Comments

@kirankumar-grootan
Copy link

Question Description

I want to log a custom header x-request-id in fiber access log using fiberzerolog. but the value is empty when shown in the console.

curl -X GET http://localhost:3000 -H "X-Request-Id: abc123"

Code Snippet (optional)

package main

import (
	"os"
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"
)

func main() {
	// Initialize the logger
	loggger := zerolog.New(os.Stderr).
		With().
		Timestamp().
		Logger().
		Output(zerolog.ConsoleWriter{
			Out: os.Stderr,
			TimeLocation: time.UTC,
			TimeFormat: "02-Jan-2006 15:04:05",
			}).
		Level(zerolog.DebugLevel)

	// Create a new Fiber app
	app := fiber.New()


	app.Use(fiberzerolog.New(fiberzerolog.Config{
	Logger:   &loggger,
	Fields:   []string{"referer", "ip", "ips", "method", "url", "ua", "latency", "status", "bytesSent", "bytesReceived", "route", "error", "reqHeader:X-Request-Id"},
	SkipURIs: []string{"/internal/metrics", "/internal/health/live", "/internal/health/ready", "/internal/health/startup"},
	}))


	// Define a route
	app.Get("/", func(c *fiber.Ctx) error {
		// Use Fiber's Locals to set custom context values
		c.Locals("test", "test")

		// create logger with subsystem for each modules and utilize it in the functons
		// https://gitlab.grootan.com/qlikverify/development/ezto-gateway/-/blob/master/lib/middleware/logger.go?ref_type=heads
		// Retrieve and log the value from the context
		value := c.Locals("test")
		loggger.Info().Msgf("Retrieved value from context: %s", value)

		// Respond to the client
		return c.SendString("Hello, World!")
	})

	// Start the server
	if err := app.Listen(":3000"); err != nil {
		loggger.Fatal().Err(err).Msg("Fiber app error")
	}
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@kirankumar-grootan kirankumar-grootan added the 🤔 Question Further information is requested label Dec 28, 2024
@ReneWerner87
Copy link
Member

the functionality to log individual headers does not yet exist in our zerolog middleware

this would have to be added first

https://github.com/gofiber/contrib/blob/main/fiberzerolog/config.go#L12-L34

@kirankumar-grootan
Copy link
Author

kirankumar-grootan commented Dec 29, 2024

@ReneWerner87 thanks. the middlware already support request id but it need the requestid middleware to work. passing just the request id header is not working

the above code works.

app.Use(requestid.New())
app.Use(fiberzerolog.New(fiberzerolog.Config{
	Logger:          &logger.Logger,
	FieldsSnakeCase: true,
	Fields:          []string{"referer", "ip", "ips", "method", "url", "ua", "latency", "status", "bytesSent", "bytesReceived", "route", "error", "requestId"},
	SkipURIs:        []string{"/internal/metrics", "/internal/health/live", "/internal/health/ready", "/internal/health/startup"},
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤔 Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants