0

Golang vesion: 1.18.3

Logging libs used:

"log"

"github.com/kdar/logrus-cloudwatchlogs"

"github.com/sirupsen/logrus"

I'm writing a AWS Lambda using Go. It sits behind an AWS APIGateway as a REST API. I'm trying to log the requests got into it and the responses it sends back. Let's say the example request payload is:

const jsonData = `{"name": "Yomiko","address": {"city": "Tokyo","street": "Shibaura St"},"children":[{"lastName": "Takayashi"}],"isEmployed": false}`

When I log it using standard "log" library as below,

log.Println("JSON Data: ", jsonData)

It's printed nicely in the targeted CloudWatch Stream maintaining the JSON format as below, enter image description here Image 1: CloudWatch log with standard "log" library

However, when I used logrus with logrus-cloudwatchlogs as below,

    hook, err := logrus_cloudwatchlogs.NewHook(transactionLogGroup, transactionLogStream, sess)
    l := logrus.New()
    l.Hooks.Add(hook)
    l.Out = ioutil.Discard
    l.Formatter = &logrus.JSONFormatter{}
    l.Println("JSON Data: ", jsonData)

It adds backslashes as shown below and loses the nice JSON Format,

enter image description here Image 2: CloudWatch log with "logrus" library

My question for you good people is,

How can I persevere the nice JSON format as shown in Image 1 when I use the "logrus" library? Please note that using standard "log" library is not an option here.

samme4life
  • 1,143
  • 2
  • 14
  • 20
  • Shouldn't you be using cloudwatch's formatter option? - https://github.com/kdar/logrus-cloudwatchlogs/blob/facbc54742f0fe2e2dcc3feca57cd1e01b4f663a/examples/basic/basic.go#L32 – Inian Aug 22 '22 at 08:01
  • @Inian l.Formatter = logrus_cloudwatchlogs.NewProdFormatter() gives the same result – samme4life Aug 23 '22 at 04:17

0 Answers0