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

Using WithError for Warn levels and below behaves weirdly #10

Open
fizyk opened this issue Apr 29, 2020 · 1 comment
Open

Using WithError for Warn levels and below behaves weirdly #10

fizyk opened this issue Apr 29, 2020 · 1 comment

Comments

@fizyk
Copy link

fizyk commented Apr 29, 2020

When using WithError alog with Warn and below, the formatter leaves the error key in data, but strips the error message leaving an empty string.

logrus.WithError(errors.New("test")).Warn("Issues!")

{"timestamp":"2009-11-10T23:00:00Z","message":"Issues !","severity":"WARNING","context":{"data":{"error":{}}}}

logrus.WithError(errors.New("test")).Error("Issues!")

{"timestamp":"2009-11-10T23:00:00Z","serviceContext":{},"message":"Issues!: test","severity":"ERROR","context":{"reportLocation":{"filePath":"sandbox634680707/prog.go","lineNumber":21,"functionName":"main"}}}

Console output

@DamianSkrzypczak
Copy link

DamianSkrzypczak commented Apr 29, 2020

I checked and problem lays in different behavior for certain levels (severityError, severityCritical, severityAlert) (https://github.com/TV4/logrus-stackdriver-formatter/blob/master/formatter.go#L150)
Those threat error field specially.

For any other logging level, any error-typed value is just passed within context object (context.Data to be specific) to json.Marshal (https://github.com/TV4/logrus-stackdriver-formatter/blob/master/formatter.go#L187) which just ignores error-typed values (but keeps keys)

example of such behaviour can be found here:
https://play.golang.org/p/pVFYjn1BoL7

So potential fix would be to find all error-typed values and convert them into string using .Error() or require from user to use MarshalJSON()-able objects that fulfill error interface as well.
(example of such custom error https://play.golang.org/p/Z7YNNl6lNZE)

Example of proposed custom type working with logrus.WithError(...).Warn(...) can be found here: https://play.golang.org/p/wOziKs36nRU

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

2 participants