1
votes

I have developed a lambda function in Node.js. Currently am using console.log to for logging my application logic.

As expected the output of console.log is available in my AWS Cloud Watch event log.

Is there any alternative logging mechanism available for AWS Lambda functions ?

2
why do you want an alternative? - Vijayanath Viswanathan
For my enterprise application we used winston logger. So I wants to know any libraries available to log AWS Lambda rather than using console.log. - Ashok JayaPrakash
Questions asking for resource/library recommendations are not on topic for Stack Overflow. However... it seems like you should be able to use winston logger in Lambda, if that's what you want -- just not for logging to files because there isn't a persistent place for the files to be written (since the containers are ephemeral). It sounds like you'd need to use an alternate community contributed transport for winston. - Michael - sqlbot

2 Answers

1
votes

I believe you can ship your logs to an ELK stack you run on a EC2 instance.

You can setup a rule in CloudWatch Events that invokes a subscribe-log-group on a Lambda function to setup the subscription of new Log Groups.

1
votes

The docs points squarely at console.log and its variants, without hinting at alternative logging interfaces.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html

Logging (Node.js)

Your Lambda function can contain logging statements. AWS Lambda writes these logs to CloudWatch. If you use the Lambda console to invoke your Lambda function, the console displays the same logs.

The following Node.js statements generate log entries:

  • console.log()
  • console.error()
  • console.warn()
  • console.info()

For example, consider the following Node.js code example.

console.log('Loading function');

exports.handler = function(event, context, callback) {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    console.log('value1 =', event.key1);
    console.log('value2 =', event.key2);
    console.log('value3 =', event.key3);
    callback(null, event.key1); // Echo back the first key value

};

The screenshot shows an example Log output section in Lambda console, you can also find these logs in CloudWatch. For more information, see Accessing Amazon CloudWatch Logs for AWS Lambda.