GithubHelp home page GithubHelp logo

πŸ€— [Question]: When use `fiberzap` with `WithContext`, caller maybe not show properly. How do I change it? about contrib HOT 3 CLOSED

gofiber avatar gofiber commented on August 11, 2024
πŸ€— [Question]: When use `fiberzap` with `WithContext`, caller maybe not show properly. How do I change it?

from contrib.

Comments (3)

ReneWerner87 avatar ReneWerner87 commented on August 11, 2024

without the fiber logger wrapper it is working

package main

import (
	"github.com/gofiber/contrib/fiberzap/v2"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	//setup.Init()

	app := fiber.New()
	zap := fiberzap.NewLogger(fiberzap.LoggerConfig{
		ZapOptions: []zap.Option{
			zap.AddCaller(),
			zap.AddCallerSkip(2),
			zap.AddStacktrace(zapcore.PanicLevel),
		},
	})
	app.Get("/ready", func(c *fiber.Ctx) error {
		zap.Info(">>>")
		zap.WithContext(c.UserContext()).Info("ready")
		zap.Info("<<<")
		return c.SendString("ready")
	})
	err := app.Listen(":3000")
	if err != nil {
		log.Error(err)
		return
	}
}
{"level":"info","ts":1695285876.640757,"caller":"testGo/main.go:23","msg":">>>"}
{"level":"info","ts":1695285876.6409469,"caller":"testGo/main.go:24","msg":"ready"}
{"level":"info","ts":1695285876.640969,"caller":"testGo/main.go:25","msg":"<<<"}

@Skyenought do you have an idea, why the wrapper function is shifting the outside caller level?

https://github.com/gofiber/fiber/blob/e547bea49e19984fddd1131332d43db092b1f58e/log/fiberlog.go#L120-L122

with skip 2

	log.SetLogger(fiberzap.NewLogger(fiberzap.LoggerConfig{
		ZapOptions: []zap.Option{
			zap.AddCaller(),
			zap.AddCallerSkip(2),
			zap.AddStacktrace(zapcore.PanicLevel),
		},
	}))
	app.Get("/ready", func(c *fiber.Ctx) error {
		log.Info(">>>")
		log.WithContext(c.UserContext()).Info("ready")
		log.Info("<<<")
		return c.SendString("ready")
	})
{"level":"info","ts":1695286200.526693,"caller":"log/fiberlog.go:25","msg":">>>"}
{"level":"info","ts":1695286200.526879,"caller":"testGo/main.go:24","msg":"ready"}
{"level":"info","ts":1695286200.526884,"caller":"log/fiberlog.go:25","msg":"<<<"}

from contrib.

0xmkzt avatar 0xmkzt commented on August 11, 2024

@ReneWerner87 thank you!
If use fiber log directly, it will be more perfect

from contrib.

0xmkzt avatar 0xmkzt commented on August 11, 2024

@ReneWerner87 It's still a problem if use fiber log directly when use WithContext.

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
)

func main() {
	//setup.Init()

	app := fiber.New()
	app.Get("/ready", func(c *fiber.Ctx) error {
		log.Info(">>>")
		log.WithContext(c.UserContext()).Info("ready")
		log.Info("<<<")
		return c.SendString("ready")
	})
	err := app.Listen(":3000")
	if err != nil {
		log.Error(err)
		return
	}

	log.Info("Exit.")
}

curl -i http://127.0.0.1:3000/ready Output:

2023/09/25 10:56:05.581632 main.go:13: [Info] >>>
2023/09/25 10:56:05.581965 router.go:145: [Info] ready
2023/09/25 10:56:05.581970 main.go:15: [Info] <<<

from contrib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.