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]: Add custom tags to logger middleware #2187

Closed
3 tasks done
Skyenought opened this issue Nov 1, 2022 · 2 comments · Fixed by #2188
Closed
3 tasks done

🚀 [Feature]: Add custom tags to logger middleware #2187

Skyenought opened this issue Nov 1, 2022 · 2 comments · Fixed by #2188

Comments

@Skyenought
Copy link
Member

Feature Description

I want to replace switch matching tag with map[string]func() matching tag before processing. This adds custom tag functionality.

This allows us to add custom tags, but it is not thread-safe.

Additional Context (optional)

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
})
// ...

Code Snippet (optional)

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")
}

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.
@ReneWerner87
Copy link
Member

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

@Skyenought
Copy link
Member Author

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.

@Skyenought Skyenought changed the title 🚀 [Feature]: Add custom tags to logger middleware 🚀 [Feature]: Add custom tags to logger middleware Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants