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

pn-lambda-logger

v1.0.2

Published

Logger for AWS Lambda nodejs.

Downloads

16,086

Readme

A nodejs logger for AWS Lambda

  • Writes logs to stdout using console.log
  • Logs in JSON format
  • Supports log levels: DEBUG, INFO, WARN, ERROR and OFF
  • Defaults to log level INFO
  • Support for pretty print when debugging
  • Built using typescript and includes types

Basic example:

import { APIGatewayEvent } from 'aws-lambda';
import { log } from 'pn-lambda-logger';
import { ok } from '../aws/response';

export const healthCheck = async (event: APIGatewayEvent, context: AWSLambda.Context, cb: AWSLambda.Callback) => {

// A standard INFO log message
log.info('My message');

}
  // Output
  {
    "@timestamp": 1541421856289,
    "level": "INFO",
    "message": "My message"
  }

Context and meta data

import { APIGatewayEvent } from 'aws-lambda';
import { log } from 'pn-lambda-logger';
import { ok } from '../aws/response';

export const healthCheck = async (event: APIGatewayEvent, context: AWSLambda.Context, cb: AWSLambda.Callback) => {

  // Setup so that the AWS Lambda meta data will be added to log output
  log.setup({ context, host: event.headers['Host'], verb: event.httpMethod, useragent: event.headers['User-Agent'], query: event.queryStringParameters, path: event.path, xRequestId: event.headers['X-Request-ID'] });

  // A standard INFO log message
  log.info({ message: 'Message' });

  cb(null, ok({ status: 'UP' }));
};
{
  "@timestamp": 1541460988579,
  "level": "INFO",
  "message": "Message",
  "host": "abc123.execute-api.eu-west-1.amazonaws.com",
  "verb": "GET",
  "path": "/manage/health",
  "useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
  "query": {
    "hello": "world"
  },
  "awsRequestId": "asdas-adsas-asdasdas-asdasda-adss",
  "functionName": "hello",
  "invokedFunctionArn": "arn:cccccc",
  "memoryLimitInMB": 1024,
  "remainingTime": 11147
}

Log Levels

The default log level is INFO log.level = LogLevels.INFO;. To show debug messages you need to explicitly set the log.level to DEBUG like this:

// Set loglevel
log.level = LogLevels.DEBUG;
// Write debug log
log.debug({ message: 'message' });
{
  "@timestamp": 1541421856291,
  "level": "DEBUG",
  "message": "Message",
}

Logging extra data

You can add any number of extra properties to the object when logging, like this:

// Setup so that the AWS Lambda meta data will be added to log output
  log.setup({ context, host: event.headers['Host'], verb: event.httpMethod, useragent: event.headers['User-Agent'], query: event.queryStringParameters, path: event.path, xRequestId: event.headers['X-REQUEST-ID'] });

// Write info log
log.info({ message: 'Message', myProp: 'hello', myNestedProp: {subProp: 'something'} });
{
  "@timestamp": 1541429192677,
  "level": "INFO",
  "message": "Message",
  "host": "abc123.execute-api.eu-west-1.amazonaws.com",
  "verb": "GET",
  "path": "/manage/health",
  "useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
  "query": {
    "hello": "world"
  },
  "awsRequestId": "a3de505e-f16b-42f4-b3e6-bcd2e4a73903",
  "functionName": "ExampleCloudFormationStackName-ExampleLambdaFunctionResourceName-AULC3LB8Q02F",
  "functionVersion": "$LATEST",
  "invokedFunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:ExampleCloudFormationStackName-ExampleLambdaFunctionResourceName-AULC3LB8Q02F",
  "memoryLimitInMB": 128,
  "remainingTime": 3,
  "myProp": "hello",
  "myNestedProp": {
    "subProp": "something"
  }
}

Pretty print

You can improve the appearance of logs when debugging, to make them easier to read. This will also add color to your logs if your terminal supports it.

To use, set the PN_LOG_PRETTY environment variable.

export PN_LOG_PRETTY=true

Mute all logs:

  log.level = LogLevels.OFF;

Set log level with environment variable

You can easily set the initial log level by setting the environment variable PN_LOG_LEVEL. This can be useful for setting different log levels in different environments TEST|PROD etc. Use 'OFF', 'DEBUG', 'INFO', 'WARN' or 'ERROR', like this:

PN_LOG_LEVEL: 'DEBUG'

Release process

When you are ready to release a version, take the following steps.

  1. Bump the version in package.json according to semver. If we are making a non-breaking change, compared to the last version, the new version would be 0.12.0.

    "version": "0.11.0" to "version": "0.12.0"

  2. Make a commit titled "Release version":

    git add package.json

    git commit -m "Release 0.12.0"

  3. Tag your commit with the version, this is what triggers the deployment in Bitbucket Pipelines. The tagged commit must start with release-

    git tag -a "release-0.12.0" -m "Release 0.12.0"
  4. Push to master!

    git push --follow-tags