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 do Sync on fiberzap? Is that necessary? #1160

Open
3 tasks done
budiprihhastomo opened this issue Jul 24, 2024 · 3 comments
Open
3 tasks done
Labels
🤔 Question Further information is requested

Comments

@budiprihhastomo
Copy link

Question Description

I want to use zap on my go app, but as I understand it, zap creates a Sync method, which is used to flushes the buffer into storage. And it is usually done when the application is exited or done periodically. This will ensure the log on buffer will be written to storage.

Do I need to do it? And where can I do it in fiberzap?

Code Snippet (optional)

No response

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.
@budiprihhastomo budiprihhastomo added the 🤔 Question Further information is requested label Jul 24, 2024
@ReneWerner87
Copy link
Member

@Jamess-Lucass @efectn can you help here

@Jamess-Lucass
Copy link
Contributor

@budiprihhastomo Fiberzap simply uses the existing logger that has been created as part of any zap configuration. You can optionally call .Sync() throughout your application if required. Does the question come from that fact the example provided in the fiberzap README does not contain defer logger.Sync() which is something seen throughout the zap documentation? That hasn't been excluded from the fiberzap documentation on purpose.

https://github.com/gofiber/contrib/tree/main/fiberzap#example

func main() {
    app := fiber.New()
    logger, _ := zap.NewProduction()
    defer logger.Sync() // <- Added this line, which is included in the zap documentation but not fiberzap.

    app.Use(fiberzap.New(fiberzap.Config{
        Logger: logger,
    }))

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

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

The sync functionality is still handled outside of fiberzap, by the core zap library.

I hope this has answered your question, if this hasn't and I've misunderstood your question then please get back to me, a code example might prove helpful if that is the case.

@budiprihhastomo
Copy link
Author

Thank you, your code example and explanation have completed my question. Maybe this is just an idea that crossed my mind, is it possible to do this synchronization (logger.Sync()) )when the response has arrived and been received by the client?

Maybe I thought of putting it there and making sure that the log is completely flushed. Because if we've to wait when the application got exited, I think there will be a possibility of an excess stack / something that we cannot be sure of.

So my question could also be about, is the event/hook already available on gofiber? I've searched for it but haven't found it.

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

3 participants