Logrus: How to print the pkg/errors

Created on 23 Jan 2019  路  7Comments  路  Source: sirupsen/logrus

How do I get the logger to print the stack trace from errors wrapped with pkg/errors?

What I've done so far is
1) Create a new logger.
2) Create an error wrapped with pkg/errors or created specifically from it.
3) Used the WithError(...) feature
4) Called entry.Error after chaining all of that.

All I see in the logs is the Error() string result. I do not see a stack trace.

I've searched around, but cannot find anything. Thank you.

Most helpful comment

It's strange that you think offering help on an issue that affects us both is considered "harassment". That says a lot more about you than it does about my actions.

All 7 comments

The problem lies within pkg/errors package, because stack is not accessible from outside.

Their Error() implementation only prints the message, ignoring the stack trace, and there's nothing we can do from logrus:
https://github.com/pkg/errors/blob/master/errors.go#L125

The only way to print the stack coming inside a pkg/errors error is by explicitly formatting it with %+v as shown here (but, and here's the thing, it will come also with the message :S):

logrus.WithField("message-and-stack", fmt.Sprintf("%+v", err)).Errorf("%v", err)

The only way I can think of bypassing the stack problem is creating your own error wrapper.

Thank you. I appreciate the official response.

The problem lies within pkg/errors package, because stack is not accessible from outside.

But it is, and the procedure is well documented.

You are purposely being obtuse. Ever other logging framework in every other language prints the full stack of an error when it gets on. Look at Slf4j. What I wanted is to get the full stack by only passing the error to WithError. Such a process does not exist.

If you did research you鈥檇 find a PR that people want for this tool, but are blocked by the interface. Please stop inserting yourself into topics you have no desire to fully appreciate.

@virmundi Are you replying to me? That's a very odd response to a comment in support of your issue, and offering a solution.

@flimzy please stop harassing me across the internet. As I鈥檝e explained to you here and on SO, the question is how to use Logrus as one would use Slf4j, Apache common logging, or any other Loggong framework where stack traces are common.

As I noted on my SO answer, there is no native way for logrus do this. This is not a shortcoming of Logrus. Errors in Go are not exceptions like in Java. The answer you purpose leaks the abstraction of pkg/errors. The goal was to hook into Logrus behind the scenes to have it translate a pkg/error to a well formatted stack trace.

I closed this issue once it was made clear that Logrus does not support this feature. Please stop addressing this issue. You are not helping. You are harassing.

It's strange that you think offering help on an issue that affects us both is considered "harassment". That says a lot more about you than it does about my actions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UnAfraid picture UnAfraid  路  5Comments

anoop-nair picture anoop-nair  路  3Comments

drewwells picture drewwells  路  3Comments

demizer picture demizer  路  4Comments

kubistika picture kubistika  路  3Comments