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

@rhobweb/console-logger

v1.1.0

Published

Utility for outputting formatted messages to the console

Downloads

133

Readme

Overview

Module to provide console logging with log levels, printf style formatting and coloured log levels.

Usage

Environment Variables

| Variable | Description | Default | |---------------------|------------------------------------|-----------| | NODE_LOG_LEVEL | Only output log items at this level and above, below. See NODE_LOG_LEVEL | 'info' | | LOG_COLOURS | Either 'true' to use the default colours or a comma separated list of log level colours, see LOG_COLOURS. | false | | LOG_FORMAT_OPTIONS | Comma separated list of util.formatWithOptions options to use by default. | See LOG_FORMAT_OPTIONS. | | NODE_ENV | The development stage, one of: 'production', 'test', 'development'. | | | | This only affects the default values used for the LOG_FORMAT_OPTIONS. | |

NODE_LOG_LEVEL

The level of verbosity is defined using the following log levels in increasing verbosity order.

  • off
  • error
  • warn
  • http
  • info
  • verbose
  • debug
  • silly

If NODE_LOG_LEVEL is not specified, the default vaile depends on the stage

| NODE_ENV | Default | |------------|-----------| | production | info | | All others | verbose |

LOG_COLOURS

A comma separated list of log level colours, which outputs the log level in the specified colour. If set to 'true' use the default colours. See the colors module for the supported colours. The default colours are:

LOG_FORMAT_OPTIONS

A comma separated list of format options to be passed to util.formatWithOptions, The default options are:

The format options are extended with custom options that are applicable to this module only.

| Option | Description | |------------|-----------| | maxMessageLength | Custom option that limits the maximum number of characters of the output message. If null, messages are not truncated. | | | This is applied to the final message after all other format options have been applied to it. | | paramSeparator | Custom option to specify the string that separates top level parameters. Defaults to the empty string. | | | e.g., params [ 'p1', 'p2' ] would by default be output as: 'p1p2' | | stringified | Custom option to output Objects as stringified JSON, i.e., double quoted property names. This is useful for copying and pasting the output into a JSON file. | | | NOTE: that the output object shall be incomplete if the object contains any types that cannot be stringified, e.g., RegExp. | | maxArrayLength | The number of array elements to print, truncated arrays are output with the number of elements missing, e.g., '1, 2, ... 1 more item'. If null, no truncation. | | depth | The depth to traverse objects, e.g., '{ p1: 'v1', p2: { p2_1: 'v21', p2_2: [Object] } }'. If null, no truncation. | | breakLength | Determines the maximum number of characters to fit on one line when outputting objects. Defaults to 80. (Note: affected by the 'compact' option) |

Type Formatting

The util.inspect method formats types as per the following table. It should be noted that the documentation for util.inspect states:

| Type | Output | |----------|--------| | string | As a top level item output without quotes, otherwise in single quotes. | | | e.g., ( 'str1', { p1: 'str2' } ) => str1, { p2_1: 'v21' } | | number | e.g., ( 123e-5 ) => 0.00123 | | Object | e.g., ( { p1: { p1_1: 'v11' } } ) => { p1: { p1_1: 'v11' } } | | Array | e.g., [ 1, 2, 3 ] => [ 1, 2, 3 ] | | RegExp | e.g., RegExp( /^fred/ ) => /^fred/ | | boolean | e.g., true => true | | function | e.g., function f() {} => [Function: f] | | Symbol | e.g., Symbol(bert) => Symbol(bert) | | Buffer | A sequence of decimal values | | | e.g., Buffer.from('4143', 'hex') => 65 67 |

Methods

Method Parameters

| Parameter | Description | |-----------|-------------| | options | The 'options' parameter contains a single property 'formatOptions' which contains a format options object with properties that enhance or override the default values defined in LOG_FORMAT_OPTIONS. | | | The 'formatOptions' object may also contain a 'fmt' property that is used to override the default formatting, see util.format. | | | Alternatively the 'options' parameter may contain a single 'fmt' property. | | level | The log level for the message. If the specified level is below the configured log level no message shall be output. | | parameterX | A parameter of any type. |

log Method

Output a formatted message to the console, according to the current log level.

Log Level Methods

This are convenience methods that call log method with the specified log level.

Long Messages

This module uses the default console to write to stdout. The default console uses util.inspect to format the message defaults the maximum message length to 10000 ( see util.inspect ). Messages longer than this maxium length are output using multiple calls to the default console, separated by messages indicating the part number, e.g.,

Example Usage

Using Default Values

The default log level is 'info'.

Should produce something like the following output:

Using Configured Values

Should produce something like the following output:

Using with Webpack

This module can be used client-side, but requires the following additional configuration for webpack 5. Install the following modules:

In the webpack.config file for the browser add the following:

Testing

Pre-Requisites

  • mocha is to be found in the PATH, typically /usr/bin/mocha.

Unit Tests

From the module directory ( '@rhobweb/console-logger' ), run: