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
JSONFormatter isn't handling functions as Field values #642
Comments
Indeed this is annoying |
We skip those unprintable fields and an error field instead of dropping the whole trace. Fixes #642
Before there was introduced a fix for JSONFormatter when func type value passed as Field. This commit does the same but for pointer to func. Ref: sirupsen#642 sirupsen#832
I got the same error but with a pointer to function: package main
import (
log "github.com/sirupsen/logrus"
)
func main() {
myfun := func() {}
log.SetFormatter(&log.JSONFormatter{})
log.WithFields(log.Fields{
"animal": "walrus",
"myfunc": &myfun,
}).Info("A walrus appears")
} results in Because logrus loses all context it's very hard to find in the code base where the error has happened. |
I am still seeing this issue.
It looks like the PR referenced above only fixes the case where field itself is a func, but if it is a struct that has a func field somewhere inside of it, there is still a problem. The solution either needs to be more involved, and go recursively inside each of the struct. |
I am still seeing this issue in v1.7.0 Test Code: Output: |
Today I came across the same error for a struct which has a function as a field type. As a workaround, a tag can be added to the field, so that this is not taken into account for the JSON, |
Exclude the peer discovery Manager's function field from the JSONFormatter used by logrus. Otherwise, the struct cannot be encoded. This issue is already known for logrus: <sirupsen/logrus#642>
If you are logging HTTP request/response, use |
We skip those unprintable fields and an error field instead of dropping the whole trace. Fixes sirupsen#642
Before there was introduced a fix for JSONFormatter when func type value passed as Field. This commit does the same but for pointer to func. Ref: sirupsen#642 sirupsen#832
When using the JSONFormatter, if I accidentally pass a function as a Field value, the entire log message is lost. It would be better if only that one field was lost (even better would be to log that the field couldn't be translated into JSON properly).
Simple example code
Current output
Failed to obtain reader, Failed to marshal fields to JSON, json: unsupported type: func() string
The text was updated successfully, but these errors were encountered: