Debug: Single log for multi line strings in AWS Cloudwatch

Created on 11 Jul 2016  路  8Comments  路  Source: visionmedia/debug

Hey, I'm using debug for a node aws lambda where I make a request and log the response body.

If I use console.log it logs the entire body in one collapsible section in AWS cloudwatch (http://i.imgur.com/WQMVIGu.png), but if I use debug it creates a section for each line (http://i.imgur.com/cpLeEsr.png).

Is there an option or something to make it not do this multi section behavior? I tried using %j but it leaves the visible \n characters in the statement

Most helpful comment

I don't use this project, but this discussion as the only valid Google result for "cloudwatch logs multiline". I did some testing and found that CloudWatch log entries can be made multiline by using \r as the line delimiter. Using either \n (Unix) or \r\n (DOS) line endings will result in separate entries. This screenshot demonstrates this. (NOTE: This is Python code, not Node.) I hope that helps you improve your product.
screenshot 2017-03-27 17 38 30

All 8 comments

I am in the same situation, any progress?

Will gladly review a PR if anyone has time to look into this. In the mean time, check out the middleware discussion here as I believe it provides a way to handle these kinds of cases without mutzing about with the core lib code

https://github.com/visionmedia/debug/issues/370

I don't use this project, but this discussion as the only valid Google result for "cloudwatch logs multiline". I did some testing and found that CloudWatch log entries can be made multiline by using \r as the line delimiter. Using either \n (Unix) or \r\n (DOS) line endings will result in separate entries. This screenshot demonstrates this. (NOTE: This is Python code, not Node.) I hope that helps you improve your product.
screenshot 2017-03-27 17 38 30

For AWS Lambda, a simple solution is overriding debug.log to use console.log. As seen in the readme:

debug.log = console.log.bind(console);

This will result in each debug statement being grouped in one collapsable section.

How do we filter strings with newlines ?

Unfortunately, in our python 2.7 lambda we can't change our \n to \r because \n has special significance in the bulk API requests we are making to Elasticsearch. I don't want to escape the log messages or regex on them because then they do not accurately represent what we are trying to log. Does anyone have another solution to this issue?

Perhaps we could log the messages as json objects.

For future readers, use JSON.stringify

Example:

log.info("this/is/my/route PARAMS:", JSON.stringify(req.params));

For those who came here by doing a Google search for "cloudwatch logs multiline" try searching for "cloudwatch agent multiline logs" instead.

One of the results there is for https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html#CloudWatch-Agent-Configuration-File-Logssection which will tell you how to configure the CloudWatch agent to recognise the start of a new pattern.

For example:

  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "/var/log/my-app/my-app.log",
            "log_group_name": "my-app",
            "log_stream_name": "{instance_id}",
            "timestamp_format" : "%Y-%m-%d %H:%M:%S,%f",
            "multi_line_start_pattern": "{timestamp_format}"
          }
        ]
      }
    }
  },

I don't know if the CloudWatch Agent can be configured like this in AWS Lambda but it works nicely for EC2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kopax picture kopax  路  4Comments

realityking picture realityking  路  6Comments

Qix- picture Qix-  路  7Comments

xkubow picture xkubow  路  4Comments

dave-ops picture dave-ops  路  6Comments