We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I want to replace switch matching tag with map[string]func() matching tag before processing. This adds custom tag functionality.
switch
tag
map[string]func()
This allows us to add custom tags, but it is not thread-safe.
Source code
Pseudocode:
type LogFunc func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) // TagMap use to storage how to log content when parse Tag. var TagMap = map[string]LogFunc{ TagReferer: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { return buf.WriteString(c.Get(fiber.HeaderReferer)) }, } TagMap[TagTime] = func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { return buf.WriteString(timestamp.Load().(string)) } // Loop over template tags to replace it with the correct value _, err = tmpl.ExecuteFunc(buf, func(w io.Writer, tag string) (int, error) { /* switch tag { case TagTime: return buf.WriteString(timestamp.Load().(string)) case TagReferer: return buf.WriteString(c.Get(fiber.HeaderReferer)) ... */ if logFunc, ok := TagMap[tag]; ok { return logFunc(buf, c, w, tag) } else { switch { case strings.HasPrefix(tag, TagReqHeader): return buf.WriteString(c.Get(tag[10:])) // ... } } return 0, nil }) // ...
package main import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/internal/bytebufferpool" "github.com/gofiber/fiber/v2/middleware/logger" "io" ) func main() { app := fiber.New() logger.TagMap["custom_tag"] = func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { return buf.WriteString("it is a custom tag") } buf := bytebufferpool.Get() defer bytebufferpool.Put(buf) app.Use(logger.New(logger.Config{ Format: "${custom_tag}", Output: buf, })) app.Get("/", func(ctx *fiber.Ctx) error { return ctx.JSON(map[string]string{ "message": "hello world", }) }) app.Listen(":3000") }
The text was updated successfully, but these errors were encountered:
Okay, good idea then please make it configurable in the configuration
thread safe it will be if we allow it only at initialization or add other mechanisms
Sorry, something went wrong.
Okay, good idea then please make it configurable in the configuration thread safe it will be if we allow it only at initialization or add other mechanisms
ok, I will submit the code shortly, hopefully you can help me check for potential problems.
logger middleware
Successfully merging a pull request may close this issue.
Feature Description
I want to replace
switch
matchingtag
withmap[string]func()
matchingtag
before processing. This adds customtag
functionality.This allows us to add custom tags, but it is not thread-safe.
Additional Context (optional)
Source code
Pseudocode:
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: