Skip to content
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

Single log for multi line strings in AWS Cloudwatch #296

Closed
iwllyu opened this issue Jul 11, 2016 · 10 comments
Closed

Single log for multi line strings in AWS Cloudwatch #296

iwllyu opened this issue Jul 11, 2016 · 10 comments

Comments

@iwllyu
Copy link

iwllyu commented Jul 11, 2016

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

@cadesalaberry
Copy link

I am in the same situation, any progress?

@thebigredgeek
Copy link
Contributor

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

#370

@RichardBronosky
Copy link

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

@gswalden
Copy link

gswalden commented Oct 4, 2017

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.

@srpoucse
Copy link

How do we filter strings with newlines ?

@prmichaelsen
Copy link

prmichaelsen commented Nov 7, 2019

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.

@MatayoshiMariano
Copy link

For future readers, use JSON.stringify

Example:

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

@steinybot
Copy link

steinybot commented Nov 3, 2020

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.

@bgth
Copy link

bgth commented Feb 17, 2022

Hello,
Is there a way to start from beginning of the log in cloudwatch new version?
In the previous version of cloudwatch, we had "initial_position". But that seems to be missing in the new version.

@Qix-
Copy link
Member

Qix- commented Feb 17, 2022

@bgth this isn't the place for AWS support, please contact Amazon or use Stack Overflow for such questions.

@debug-js debug-js locked as off-topic and limited conversation to collaborators Feb 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests