• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

748
Views
How to log raw JSON to Cloudwatch from AWS Lambda in node.js?

I have some node.js based Lambdas that are logging data.

In order to properly query and filter the data etc, I want to log as pure JSON data from my Lambdas.

However, when I do a regular console.log it makes an ordinary string of the data.

console.log({a:1,b:2,x:"xxx"})

Results in this:

2020-04-29T14:46:45.722Z    3f64c499-fbae-4a84-996c-5e5f0cb5302c    INFO    { a: 1, b: 2, x: 'xxx' }

The logged line above does not seem to be searchable as JSON using the various filter matching options in CloudWatch.

I've tried to call the AWS.CloudWatchLogs API directly but since I'm using lambda I cannot maintain a token between invocations of the functions, so I'm not sure that's the way to go.

Have anyone else had success in logging raw JSON from a Javascript Lambda?

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

The problem is that console.log() does not go directly to stdout/stderr. You can see that using this Lambda:

const process = require('process');

exports.handler = async (event) => {
    console.log("message 1");
    process.stdout.write("message 2\n");
};

If you invoke that, you will see output like this:

START RequestId: 6942bebc-1997-42cd-90c2-d76b44c637283 Version: $LATEST
2020-04-29T17:06:07.303Z    6935bebc-1d97-42cd-90c2-d76b4a637683    INFO    message 1
message 2
END RequestId: 6942bebc-1997-42cd-90c2-d76b44c637283

So to get the output you want you could either redefine console.log to go to stderr, or write to stdout/stderr directly.

Or you could use a logging framework that writes to stdout/stderr, which may give you more flexibility on how your messages are written. I don't do Node development, but I've heard that Winston is the standard logging framework.

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error