npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-context-logger

v1.0.1

Published

Consistent logging and output for AWS Lambda functions

Downloads

177

Readme

aws-context-logger

Build Status codecov.io Dependencies

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

  1. context: The context argument provided by the handler function.
  2. [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

  1. 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.
  2. [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

  1. data (Any): Takes any value passed and returns it as the payload for the Lambda function.
  2. [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

  1. error (Non-null): If a value other than null or undefined is passed the Lambda function will call #fail with the data passed.
  2. data (Any): Will call #succeed with the data passed. Will only be triggered if the value of the error argument is null or undefined.

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

  1. [type] (String): Looks for either log or error as the first paramter and will print using that method. Defaults to log.
  2. ... (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.