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

simple-cw-logger

v1.0.7

Published

A simple console.log wrapper for printing to sdtout, to be used with awslogs docker logging engine

Downloads

52

Readme

Simple Logger

This very simple logger has info(), warn(), debug() and error() methods like most standard loggers but supports only printing to stdout. This was designed to be used in scripts who's stdout is being directed to CloudWatch, esp. via docker-compose "awslogs" logging engine.

const Logger = require( 'simple-cw-logger' );
log = new Logger();
log.debug( 'arguments are just like console.log' );

That will produce:

2019-08-09T15:41:57-0700 [debug] arguments are just like console.log

You can use the following in a docker-compose file:

    logging:
      driver: awslogs
      options:
        awslogs-region: "us-west-q"
        awslogs-group: "yourgroupname"
        awslogs-create-group: "true"
        awslogs-datetime-format: "%Y-%m-%dT%H:%M:%S%z"

Options

const Logger = require( 'simple-cw-logger' );
log = new Logger(
  level: "info",
  datetime: "%Y-%m-%dT%H:%M:%S%z",
  prefix: null, // optional prefix string to add to messages
  colorize: false // set to true to colorize the output
);

The level option ("info" by default) is pretty simplistic; if it set to "error" only log.error() output will be shown. If it not set to "debug" then log.debug() will not be shown. Otherwise show everything.

The datetime option shown is the default. It uses strftime format, the same format that docker-compose awslogs uses for the awslogs-datetime-format argument. If you change the default, make sure it matches what you put in your docker-compose file.

In case you don't know, the awslogs-datetime-format is used to match incoming log messages and breaks them into events based on this regular expression. This is very useful for multi line messages.

The prefix option is optional. If you set it, this string will be included in the output:

const Logger = require( 'simple-cw-logger' );
log = new Logger(
  level: "info",
  datetime: "%Y-%m-%dT%H:%M:%S%z",
  prefix: "mymodule"
);
log.info( 'A message', { with: "metadata" } );

produces:

2019-08-09T16:01:06-0700 mymodule [info] A message { with: 'metadata' }

Node Express Request Logging Middleware

app.log = new Logger({level: debug});
app.use(app.log.formatter(options));

This will return a middleware function that logs (as "info") an incoming request. By default the log message will look something like

2019-08-09T16:01:06-0700 [info] POST /api/endpoint {...}

It will include the method, req.originalUrl and a json object containing the query and/or the body content. It supports some options to customize this output:

  • dontLogPaths: [] - an array of RegExp that are matches against req.originalUrl. Anything that matches is not logged. Useful for not logging health checks for example.
  • messageSizeLimit: 512 - If the JSON.stringified message length is greater than this limit, it is truncated and "..." is added at the end. Useful if some of your message bodies are unreasonably large for logging.
  • mask: ["password", "secret"] - list of properties in the message you want masked. The value will be replaced with "blocked" in the log.
  • userIdFcn: null - If supplied, will be called with the request object and is expected to return a string. This will add (${userIfFcn(req)}) to the log message. Useful if you have authenticated routes and something like req.user.email exists that you want printed with each log line.
  • messageFormatterFcn: null - If supplied, will be called with the request object and is expected to return the log message. Use this if you want to totally override the log message formatting.
  • dataFormatterFcn: null - If supplied, takes (data, req) and should return a string representing the data to be printed at the end of the message. Can be used to perform truncation or compression on large request bodies for example.

Example

app.use(app.log.formatter({
  dontLogPaths: [
    new RegExp(/^/healthCheck/)
  ],
  userIdFcn: (req) => reg.user ? req.user.email : "anonymous"
}));