aws-lambda-console
Patch the native console in order to get requestId on every log-message, AND set log-level to reduse noise in production.
Usage
Install
npm install aws-lambda-console --save
Patching the console
require('aws-lambda-console')(console, context, [options]);
console
The global console
context
The second parameter in your lambda handler, AWS Lambda uses this parameter to provide details of your Lambda function's execution. For more information, see more details (Node.js).
options
As we use console-stamp you can override options about everything exept
options.metadata, witch is used to print the requestId.
-
options.level
A string choosing the most verbose logging function to allow. Ordered/grouped as such: 'log dir', 'info', 'warn assert', 'error'
Default:LOG_LEVELenvironment variable orlog
Note that you can set the LOG_LEVEL environment variable in the AWS Lambda Managment Console and it will be used by this module.
Examples
exports.myHandler = async (event, context) => {
require('aws-lambda-console')(console, context);
console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
return 'Hello, World!'
}
result:
START RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d Version: $LATEST
LOG RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d This is a console.log message
INFO RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d This is a console.info message
WARN RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d This is a console.warn message
ERROR RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d This is a console.error message
END RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d
REPORT RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d Duration: XX ms Billed Duration: XX ms ...
or with the LOG_LEVEL set to error
START RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d Version: $LATEST
ERROR RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d This is a console.error message
END RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d
REPORT RequestId: 4e0467ec-8f2a-11e8-9d6f-a1a6a46e672d Duration: XX ms Billed Duration: XX ms ...
Note START, END and REPORT is AWS Lambda standard log