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

@squeep/logger-json-console

v3.0.2

Published

A simple logger which spews formatted data and metadata as JSON to stdout; intended for use in Squeep Framework Applications.

Downloads

20

Readme

@squeep/logger-json-console

A simple logger class, which mostly just structures messages, data, and some metadata as JSON and spews it, by default, to Console.

Supports basic scrubbing of well-defined sensitive fields, detection of circular references, and an extensible set of JSON replacer functions for common objects.

If provided with an asyncLocalStorage instance, it expects a stored object which will be spread over the resulting logged JSON.

Intended to be specific to Squeep Framework Applications, this module has some opinions.

API

  • new Logger(options, commonObject, asyncLocalStorage, backend)

    • commonObject will be merged into every log
    • asyncLocalStorage, if provided, should store an object, which will also be merged into log
    • backend is console by default, but may be anything implementing the same log-level functions
  • error(scope, message, data, ...)

    • scope - identifies source of message, e.g. 'class:method'
    • message - text to be logged
    • data - object to be logged, can be scrubbed of sensitive fields, and will be serialized with provided replacers
    • any additional arguments are included as an array
  • warn(scope, message, data, ...)

  • info(scope, message, data, ...)

  • log(scope, message, data, ...)

  • debug(scope, message, data, ...)

JSON Replacers

Includes replacers for these objects:

  • Error
  • BigInt
  • http.IncomingMessage
  • http.OutgoingMessage
  • http.ServerResponse

Additional replacers may be inserted into the logger instance's jsonReplacers array.

Data Sanitizers

Sanitizers may be inserted into the logger instance's dataSanitizers array.

Example

const http = require('node:http');
const { AsyncLocalStorage } = require('node:async_hooks');
const uuid = require('uuid');
const Logger = require('@squeep/logger-json-console');

const loggerOptions = {
  ignoreBelowLevel: 'info',
};
const commonObject = {
  nodeIdentifier: uuid.v1(),
};
const asyncLocalStorage = new AsyncLocalStorage();
const logger = new Logger(loggerOptions, commonObject, asyncLocalStorage);

const scope = 'exampleServer';
http.createServer((req, res) => {
  asyncLocalStorage.run({ requestId: uuid.v1() }, () => {
    logger.debug(scope, 'start', { req, res });
    setImmediate(() => {
      res.end();
      logger.info(scope, 'finish', { req, res }, 'other stuff');
    });
  });
}).listen(8088);

Results in messages such as this:

{
  "nodeIdentifier": "64610280-fc17-11ed-a918-1dd19f027d43",
  "timestamp": "2023-05-26T22:48:17.995Z",
  "timestampMs": 1685141297995,
  "level": "info",
  "scope": "exampleServer",
  "message": "finish",
  "data": {
    "req": {
      "method": "GET",
      "url": "/",
      "httpVersion": "1.1",
      "headers": {
        "host": "localhost:8088",
        "user-agent": "curl/7.83.1",
        "accept": "*/*"
      },
      "trailers": {}
    },
    "res": {
      "statusCode": 200,
      "statusMessage": "OK",
      "headers": {}
    }
  },
  "other": [
    "other stuff"
  ],
  "requestId": "68233aa0-fc17-11ed-a918-1dd19f027d43"
}