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

scope-logger

v1.2.0

Published

Logs a variable and a sequence of scopes through which it's accessed

Downloads

100

Readme

scope-logger

What it does

Logs a variable and a sequence of scopes through which it's accessed. It automatically logs the name of the variable. A NodeJs logger. Inspired by the debug library.

Why??

Too lazy to write console.log("variableName %o", variableValue) :)) The same effect can be achieved with console.log({variable}) => $ variableName: variableValue
But this logger shows the sequence of scopes(i.e., functions) from the variable is logged, a feature that I wished I had when I was logging many different variables.

Example

function outerFn() {
  function innerFn() {
    const logger = new Logger("lazy-log");

    const foofoo = "barbar";
    logger.log({ foofoo });
  }

  innerFn();
}

outerFn();

Output: usage-sample-output

Installation

$ npm install scope-logger

Usage

  1. Create an instance of Logger. Namespace and options are optional args for constructor.

  2. Pass the variable you want to log to the log method inside curly brackets {}!

Additional Control

  • disableAll() Toggle to disable all the logging of a logger/namespace. A much more efficient approach than commenting every console.log() line (and deleting them) before pushing the code to production. Can be used to get rid of a logger instance's logs in the terminal.

For instance:

const logger = new Logger("Log tester").disableAll();
//or
logger.disableAll();

const foo = "bar";
logger.log({ foo });

Output: nothing! $

Configuration Options

  1. ignoreIterators (boolean): set true to omit the native iterator calls (e.g., Array.forEach) in the scope log statement. This applies to all types of array-like iterators available in JS and NodeJs such as Map, Set, Array, Int8Array, and so on.
function outerFn() {
  function innerFn() {
    const logger = new Logger("Server");

    const testArr = [1, 2, 3];
    testArr.forEach((val) => {
      logger.log({ val });
    });
  }

  innerFn();
}

outerFn();

Default output:

ignore-iterators

testArr.forEach((val) => {
  logger.log({ val }, { ignoreIterators: true });
});

Configured output: Array.forEach is omitted

ignore-iterators-enabled

  1. onlyFirstElem (boolean): set to true to log only the first element in an iterator call. This is useful in scenarios where you only care about the scope journey of a variable in the iterator call, but not about the value of each variable.

All the elements would have the same scope signature, therefore it's redundant to print all those logs. The non-first variables are not logged. This applies recursively for nested iterator calls.

function main() {
  const outerArr = [1, 2, 3];
  const innerArr = [1, 2, 3];

  outerArr.forEach(() => {
    innerArr.map((val) => {
      logger.log({ val });
    });
  });
}

main();

Default output: The following 3 lines x 3 = 9 logs in total

only-first-elem

outerArr.forEach(() => {
  innerArr.map((val) => {
    logger.log({ val }, { onlyFirstElem: true });
  });
});

Configured output: Only the first element is logged

only-first-elem-enabled

The default configuration:

  {
    ignoreIterators: false,
    onlyFirstElem: false
  }

Limitations

  1. Cannot pass a property of an object. Because the library is based on JS object destructing (console.log({foo}) outputs the same as console.log({foo: <value>})).
  • Where foo.name = "bar" Cannot type logger.log({foo.name}). This will throw a syntax error.

Test

$ npm run test