Logrus: Is it possible to log a data struct or JSON object so we don't have to enumerate/list the fields?

Created on 21 Mar 2016  路  4Comments  路  Source: sirupsen/logrus

Hi,

Thanks for sharing the nice logger.
I just want check if this has a functionality to log a JSON or a data struct?

Example:
log.WithFields(&jsonObject).Info("A JSON object is logged from mongodb!")
log.WithFields(&tableStruct).Info("A data struct is logged from datastore!")

Best Regards,
Edwin

Most helpful comment

For those who will stumble upon the same problem in the future.
You can display the actual map or data struc without having to convert it to JSON:

Logger:

// Get HTTP GET parameters as a map
getParameters, _ := url.ParseQuery(r.URL.RawQuery)

log.WithFields(logrus.Fields{
  "getParameters": getParameters,
}).Info("Request received!")

Result:

{"getParameters":{"from":["verify"],"text":["SampleMessage!"],"to":["817031805898"]},"level":"info","msg":"Request received!","time":"2018-04-12 17:32:48"}

If you use logrus.Field{"name": &jsonObject} it wouldn't display the actual value as @RongxinZhang mentioned.

And if you convert the JSON to a string, you'll have a lot of escape characters which is not friendly with services such as Loggly:

// Get HTTP GET parameters as a map
getParameters, _ := url.ParseQuery(r.URL.RawQuery)
getParametersJson, err := json.Marshal(getParameters)

log.WithFields(logrus.Fields{
  "getParameters": string(getParametersJson),
}).Info("Request received!")

Result:

{"getParameters":"{\"from\":[\"verify\"],\"text\":[\"SampleMessage!\"],\"to\":[\"817031805898\"]}","level":"info","msg":"Request received!","time":"2018-04-12 16:05:18"}

All 4 comments

You'll need to pass a logrus.Field{"name": &jsonObject} and then that jsonObject will be printed as JSON if you use the JSONFormatter.

@aybabtme Is there an example that you can show? I just tried the below and the response was a bunch on weird encoding:

please advise:

Logger:

jsonStr := []string{"1", "2", "3"}
jsonObj, _ := json.Marshal(jsonStr)
fmt.Println(string(jsonObj))

log.WithFields(log.Fields{
  "json": &jsonObj, 
}).Info("message")

Result:

{"json":"WyIxIiwiMiIsIjMiXQ==","level":"info","msg":"message","time":"2016-05-06T15:07:52-07:00"}

do:

jsonStr := []string{"1", "2", "3"}
log.WithFields(log.Fields{
  "json": jsonStr, 
}).Info("message")

For those who will stumble upon the same problem in the future.
You can display the actual map or data struc without having to convert it to JSON:

Logger:

// Get HTTP GET parameters as a map
getParameters, _ := url.ParseQuery(r.URL.RawQuery)

log.WithFields(logrus.Fields{
  "getParameters": getParameters,
}).Info("Request received!")

Result:

{"getParameters":{"from":["verify"],"text":["SampleMessage!"],"to":["817031805898"]},"level":"info","msg":"Request received!","time":"2018-04-12 17:32:48"}

If you use logrus.Field{"name": &jsonObject} it wouldn't display the actual value as @RongxinZhang mentioned.

And if you convert the JSON to a string, you'll have a lot of escape characters which is not friendly with services such as Loggly:

// Get HTTP GET parameters as a map
getParameters, _ := url.ParseQuery(r.URL.RawQuery)
getParametersJson, err := json.Marshal(getParameters)

log.WithFields(logrus.Fields{
  "getParameters": string(getParametersJson),
}).Info("Request received!")

Result:

{"getParameters":"{\"from\":[\"verify\"],\"text\":[\"SampleMessage!\"],\"to\":[\"817031805898\"]}","level":"info","msg":"Request received!","time":"2018-04-12 16:05:18"}

Was this page helpful?
0 / 5 - 0 ratings