logrus-stackdriver-formatter
logrus-stackdriver-formatter copied to clipboard
Using WithError for Warn levels and below behaves weirdly
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
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