zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

[Question]Error stack with pkgerrors.MarshalStack seems to be too long in some cases

Open Adachi324 opened this issue 1 year ago • 0 comments

my case

import (
	"github.com/gofiber/fiber/v2"
	"github.com/pkg/errors"
	"github.com/rs/zerolog"
	zelog "github.com/rs/zerolog/log"
	"github.com/rs/zerolog/pkgerrors"
	"testing"
)

func TestAny(t *testing.T) {
	testLogger()
	app := fiber.New()
	app.Use(func(c *fiber.Ctx) error {
		return c.Next()
	})
	app.Use(func(c *fiber.Ctx) error {
		return c.Next()
	})
	app.Use(func(c *fiber.Ctx) error {
		return c.Next()
	})
	app.Use(func(c *fiber.Ctx) error {
		return c.Next()
	})
	app.Use(func(c *fiber.Ctx) error {
		return c.Next()
	})

	app.Get("/", func(c *fiber.Ctx) error {
		testLogger()
		return c.SendString("Hello, World!")
	})
	app.Listen(":3001")
}
func testLogger() {
	zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
	zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack

	err := outer()
	zelog.Error().Stack().Err(err).Msg("")
}

func inner() error {
	return errors.New("seems we have an error here")
}

func middle() error {
	err := inner()
	if err != nil {
		return err
	}
	return nil
}

func outer() error {
	err := middle()
	if err != nil {
		return err
	}
	return nil
}
{"level":"error","stack":[{"func":"inner","line":"50","source":"test_test.go"},{"func":"middle","line":"54","source":"test_test.go"},{"func":"outer","line":"62","source":"test_test.go"},{"func":"testLogger","line":"45","source":"test_test.go"},{"func":"TestAny.func6","line":"36","source":"test_test.go"},{"func":"(*App).next","line":"145","source":"router.go"},{"func":"(*Ctx).Next","line":"1030","source":"ctx.go"},{"func":"TestAny.func5","line":"32","source":"test_test.go"},{"func":"(*Ctx).Next","line":"1027","source":"ctx.go"},{"func":"TestAny.func4","line":"29","source":"test_test.go"},{"func":"(*Ctx).Next","line":"1027","source":"ctx.go"},{"func":"TestAny.func3","line":"26","source":"test_test.go"},{"func":"(*Ctx).Next","line":"1027","source":"ctx.go"},{"func":"TestAny.func2","line":"23","source":"test_test.go"},{"func":"(*Ctx).Next","line":"1027","source":"ctx.go"},{"func":"TestAny.func1","line":"20","source":"test_test.go"},{"func":"(*App).next","line":"145","source":"router.go"},{"func":"(*App).handler","line":"172","source":"router.go"},{"func":"(*Server).serveConn","line":"2359","source":"server.go"},{"func":"(*workerPool).workerFunc","line":"224","source":"workerpool.go"},{"func":"(*workerPool).getCh.func1","line":"196","source":"workerpool.go"},{"func":"goexit","line":"1197","source":"asm_arm64.s"}],"error":"seems we have an error here","time":1706101317}

If I registered some middlewares in fiber, the error stack will become very long and useless. How can I optimize this?

Adachi324 avatar Jan 24 '24 13:01 Adachi324