maniciam
maniciam

Reputation: 375

Change log format of Cloudwatch logs from Lambda (Node.js)

If I do console.log('message') in my code, it shows up in Cloudwatch as
2017-03-16T18:58:21.823Z 863c835c-0a7a-11e7-9140-e5018d6e5029 message.

Is there any way to remove the automatic formatting so that Cloudwatch just displays the argument I pass to console.log?

Upvotes: 13

Views: 4986

Answers (2)

Sky Watcher
Sky Watcher

Reputation: 41

This code helped me to get rid of '<awsRequestId> <logLevel>' when we do console.log

const { Console } = require('console');
console.log = new Console({ stdout: process.stdout, stderr: process.stderr }).log;

Upvotes: 4

idbehold
idbehold

Reputation: 17168

Inside your handler you can overwrite console.log to write directly to stdout:

var util = require('util')

module.exports.handler = function (event, context, done) {
  console.log = function () {
    var args = Array.prototype.slice.call(arguments)
    process.stdout.write(args.map(function (arg) {
      return util.isPrimitive(arg) ? String(arg) : util.inspect(arg)
    }).join(' '))
  }

  // the rest of your handler...
}

Upvotes: 8

Related Questions