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

Not getting the stack in an error #462

Open
conor-tread opened this issue Aug 16, 2022 · 7 comments
Open

Not getting the stack in an error #462

conor-tread opened this issue Aug 16, 2022 · 7 comments

Comments

@conor-tread
Copy link

Im using the package in an AWS serverless codebase and when I force an error Im not seeing the stack trace in the logs. In the init() function I have zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack.

I can see my error message in the log that gets printed its just not printing the stack

@Mehrdad-Dadkhah
Copy link

+1

@skormos
Copy link

skormos commented Sep 4, 2022

You need to use WithStack() from pkg/errors.

The documentation lists pkg/errors as a requirement: https://github.com/rs/zerolog#error-logging-with-stacktrace

Not sure if there are any downsides to the below approach, but you can just wrap the MarshalStack reference in a closure, so you don't need to pollute the entire codebase with pkg/errors imports, and only use this reference when you add the .Stack() call to the logger context chain:

import (
	"github.com/pkg/errors"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/pkgerrors"
)
...
	zerolog.ErrorStackMarshaler = func(err error) interface{} {
		return pkgerrors.MarshalStack(errors.WithStack(err))
	}

@skormos
Copy link

skormos commented Sep 4, 2022

I created another issue requesting pkg/errors be removed as a dependency since that library is now in maintenance mode.

@xsteadfastx
Copy link
Contributor

You need to use WithStack() from pkg/errors.

The documentation lists pkg/errors as a requirement: https://github.com/rs/zerolog#error-logging-with-stacktrace

Not sure if there are any downsides to the below approach, but you can just wrap the MarshalStack reference in a closure, so you don't need to pollute the entire codebase with pkg/errors imports, and only use this reference when you add the .Stack() call to the logger context chain:

import (
	"github.com/pkg/errors"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/pkgerrors"
)
...
	zerolog.ErrorStackMarshaler = func(err error) interface{} {
		return pkgerrors.MarshalStack(errors.WithStack(err))
	}

could you explain why we need this? your code just works fine, but why its not in the official code? i couldnt get it to work with the example out of the readme. should we create a PR to add your fix?

@xsteadfastx
Copy link
Contributor

also this doesnt show a full stack of the wrapped errors. it just show the stack trace from defining the zerolog.ErrorStackMarshaler to the goexit. im my case i have a few wrapped errors and get this output:

{"level":"error","devEui":"000","deviceName":"IOT-ELCMI-0001","applictionName":"Testlab","applicationId":954,"tags":{"productive":"false","usecase":"zaehler","vendor":"landis+gyr"},"pusher":"db","stack":[{"func":"init.0.func1","line":"87","source":"integration_test.go"},{"func":"(*Event).Err","line":"381","source":"event.go"},{"func":"Push.func1.1","line":"55","source":"event.go"},{"func":"goexit","line":"1594","source":"asm_amd64.s"}],"error":"could not push db entry: failed to read sql asset: open sqls/insert-recordings-without-location.sql: file does not exist","time":"2023-01-27T13:32:46Z","message":"error on push"}

@tpokki
Copy link

tpokki commented Feb 4, 2023

@xsteadfastx, if you use the pkgerrors.MarshalStack, it can only print stack traces for errors that are from github.com/pkg/errors. That is, it won't print stack traces to any arbitrary error you encounter. In order to add the stack traces to "external errors", wrap them with errors.Wrap(...) when you first encounter them.

Example from here:
https://pkg.go.dev/github.com/pkg/errors#hdr-Adding_context_to_an_error

_, err := ioutil.ReadAll(r)
if err != nil {
        return errors.Wrap(err, "read failed")
}

For example in my tests

import (
	"fmt"

	"github.com/pkg/errors"
	"github.com/rs/zerolog"
	zl "github.com/rs/zerolog/log"
	"github.com/rs/zerolog/pkgerrors"

	"testing"
)

func TestZl(t *testing.T) {
	zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack

	zl.Info().Stack().Err(wrapperError()).Msg("with stack trace")
	zl.Info().Stack().Err(fmtError()).Msg("without stack trace")
}

func fmtError() error {
	return fmt.Errorf("fmt error")
}

func wrapperError() error {
	return errors.Wrap(fmtError(), "wrapped")
}

@xsteadfastx
Copy link
Contributor

xsteadfastx commented Feb 6, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants