aws-context-logger
v1.0.1
Published
Consistent logging and output for AWS Lambda functions
Downloads
177
Maintainers
Readme
aws-context-logger
Consistent logging and output for AWS Lambda functions. Designed primarily for use with API Gateway this will also provide reliable logging and context output for any Lambda function.
Installation
npm install aws-context-logger --save
Use
aws-context-logger
creates three methods (succeed, fail, and done) that mimic the behavior of the provided Lambda context.
var logger = require('aws-context-logger');
exports.handler = function (event, context) {
var ctx = new logger(context);
// Lambda code here...
// Succeed
ctx.succeed(data, 201);
// Fail
ctx.fail(message, 404);
// Done
ctx.done(err, data);
};
Methods
constructor(context[, options])
Instantiates a new logger. Use of the new
keyword when using the constructor is optional.
Arguments
- context: The context argument provided by the handler function.
- [options] (Object): Default status codes for success and error states. Used for the done method as well as if status codes aren't provided to the succeed or fail methods.
- defaultSuccess: 200
- defaultError: 500
exports.handler = function (event, context) {
var ctx = logger(context, {
defaultSuccess: 201,
defaultError: 404
});
};
fail(message[, status])
The fail method will take whatever status and message is passed to it and convert it to an error that is easily machine parseable and human readable. Converting it to an error has several advantages for Cloudwatch logs and API Gateway responses.
Arguments
- message (String | Object | Array | Function | Error): Takes any value passed to it and converts it to a string that is included in the error message.
- [status] ](Number): The number of the status code. It will be converted to the text name in the actual error message. See the status codes below for the currently supported statuses. If not provided it will use the
defaultError
status code.
succeed(data[, status])
The fail method will take whatever status and message is passed to it and convert it to an error that is easily machine parseable and human readable. Converting it to an error has several advantages for Cloudwatch logs and API Gateway responses.
Arguments
- data (Any): Takes any value passed and returns it as the payload for the Lambda function.
- [status] ](Number): The number of the status code. If not provided it will use the
defaulError
status code. The status code is only included in the log output, it is not included in the returned data.
done(error[, data])
The done
method is a standard Node style callback where the first parameter is returns as an error and the second returns successfully with the data passesd. The default status codes are used for error and success when using done
.
Arguments
- error (Non-null): If a value other than
null
orundefined
is passed the Lambda function will call #fail with the data passed. - data (Any): Will call #succeed with the data passed. Will only be triggered if the value of the error argument is
null
orundefined
.
log([type,]...)
Sugar for the console.log
function. Behaves almost identically and used internally to allow for expansion of logging in the future. Currently it only looks for a type option to determine what type of output the log should have.
Arguments
- [type] (String): Looks for either
log
orerror
as the first paramter and will print using that method. Defaults tolog
. - ... (Any): Accepts any number of comma separated arguments and prints them via the console.
Status Codes
All current status codes are supported. Please see https://httpstatuses.com/ for information on when to use a specific status code.