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-bunyan

v1.0.6

Published

Simplified Logging Solution For AWS & ElasticSearch

Downloads

7

Readme

AWS-BUNYAN

Introduction

aws-bunyan is a simple and fast JSON logging library for node.js services. It wraps bunyan providing a very focused and simple API, with the ability to set context data into the logger at runtime - for instance correlationIds.

This library shines when used in conjunction with AWS Lambda, Cloudwatch, and ECS (ElasticSearch).

Manifesto: Server logs should be structured. JSON's a good format. Let's do that. A log record is one line of JSON.stringify'd output. Let's also specify some common names for the requisite and common fields for a log record (see below).

Example Usage

Here's a snippet (typescript):

import { ILogger, logFactory } from "aws-bunyan";
const log: ILogger = logFactory.createLogger("MyLogger");

const data = { compiler: "Webpack", language: "Typescript" };
log.debug("Data", data);

With typings support:

Log Usage With Typings

Since this is a lightweight wrapper around bunyan, you can fully leverage the bunyan CLI for nicely viewing those logs too.

bunyan CLI screenshot

Setting Context

Frequently, there is a need to group logs together by request etc. A way to achieve this is to retrieve a unique correlation id (AWS provides a couple of ways of getting this during AWS Lambda requests for example), and associate this correlation id with every log statement originating from that specific request.

Here is a snippet:

import { ILogger, logFactory } from "aws-bunyan";
const log: ILogger = logFactory.createLogger("MyLogger");

// get the unique correlation id somehow...
const uniqueCorrelationId = "12345678";

log.setContext("x-correlation-id", uniqueCorrelationId);

const data = { compiler: "Webpack", language: "Typescript" };
log.debug("Data", data);

And here's the output:

Log Usage With Context screenshot

AWS Contextual Information

Note that this logger provides extra contextual information when used with AWS. Here is a sample output from Cloudwatch with aws region and function information automatically applied.

Log Usage With AWS Context screenshot

Serializer Support

You can easily add your own serializers.

For example, let's suppose you want a serializer that masks personally identifiable information (PII). Simply write your serializer (eg. maskPII).

Example usage:

import { ILogger, logFactory } from "aws-bunyan";
import maskPII from "./maskPII";

const log: ILogger = logFactory.createLogger("Standalone", {
  maskPII
});

log.info("Phone number, Question & Answer", {
  maskPII: {
    answer: "tablet",
    phonenumber: "080 12345678",
    question: "device type"
  }
});

And here's the output:

Log Usage With Serializer screenshot

Setting Log Level

Simple:

process.env.LOG_LEVEL = "DEBUG";

Example Usages Of Logger API

Just some simple usages to guide you (typescript):

import { ILogger, logFactory } from "aws-bunyan";

const log: ILogger = logFactory.createLogger("MyLogger");

const data = { compiler: "Webpack", language: "Typescript" };

log.debug("Data", data);

log.info("This is the data", data);

const warningError = new Error(
  "Database disconnected unexpectedly. Retrying..."
);

log.warn("Database connection issue", data, warningError);

const fatalError = new Error("Database is offline.");

log.error("Fatal error, aborting.", data, fatalError);

Vanilla Javascript (node) Example Usage

const logFactory = require("aws-bunyan").logFactory;
const log = logFactory.createLogger("MyLogger");

const data = { compiler: "Webpack", language: "Typescript" };
log.debug("Data", data);

Finally

Check the bunyan docs for further details if unsure.