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

slay-contextlog

v2.0.0

Published

slay plugin providing an async aware logger `app.contextLog`

Downloads

11

Readme

slay-contextlog

Build
Status

Creates a context configurable logger for use with asynchronous workflows.

Usage

This module can be used by doing:

app.preboot(require('slay-contextlog');
// ...
  app.contextLog.info('hiya!');
// ...

Description

Sometimes when writing code you need to create asynchronous workflows.

It can be hard to manage object references to your logger, this module will attach an app.contextLog property to track your async workflows.

var buildId = 1;
app.contextLog.info('about to do things');
app.withBreadcrumb({
  buildId: buildId
}, () => {
  // app.contextLog includes build id in any metadata
  app.contextLog.info('something happened');
  fs.readFile('thing.txt', (err. body) => {
    // app.contextLog still has build id
    app.withBreadcrumb({
      fileCRC: crc(body)
    }, () => {
      app.contextLog.info('something else happened');
    });
  });
});

outputs:

about to do things
something happened {breadcrumbs=[buildId=1]}
something else happened {breadcrumbs=[buildId=1,fileCRC=DEADBEEF]}

API

app.contextLog

This is a winston logger that can be used to log info based upon your current context. It is the preferred approach to logging vs app.log.

app.runWithLoggerBreadcrumb(value, fn);

A bit of a mouthful! This function runs fn with a app.contextLog that contains value in the metadata array breadcrumbs. Any callbacks queued during execution of fn.

NOTE: It does not ensure Promise callbacks are set to the proper context for now due to technical limitations of Promise.

Workflows

Retries

Some operations need to track retries of operations like unsafe operations. If we add breadcrumbs to an existing operation, it will produce 2 breadcrumbs with retries_left:n and retries_left:n-1. This is undesirable. The solution is to ensure attempt is always given the logger it originally gave a breadcrumb.

function attempt(fn, retriesLeft, done) {
  // cache original logger
  const logger = app.contextLog;
  function next(err) {
    if (err) {
      perform(retriesLeft--);
      return;
    }

    done();
  }

  function perform(retriesLeft) {
    if (retriesLeft === 0) {
      return void done(Error('No success'));
    }

    app.contextLog = logger;
    app.withBreadcrumb({retriesLeft}, () => fn(next));
  }

  perform(retriesLeft);
}

There are various ways to cache the original logger besides this, but it is something to note.

Test

npm test

License

MIT