1.1.0 • Published 7 months ago

@rhobweb/console-logger v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

Overview

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

Usage

Environment Variables

VariableDescriptionDefault
NODE_LOG_LEVELOnly output log items at this level and above, below. See NODE_LOG_LEVEL'info'
LOG_COLOURSEither 'true' to use the default colours or a comma separated list of log level colours, see LOG_COLOURS.false
LOG_FORMAT_OPTIONSComma separated list of util.formatWithOptions options to use by default.See LOG_FORMAT_OPTIONS.
NODE_ENVThe 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_ENVDefault
productioninfo
All othersverbose

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.

OptionDescription
maxMessageLengthCustom 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.
paramSeparatorCustom 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'
stringifiedCustom 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.
maxArrayLengthThe 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.
depthThe depth to traverse objects, e.g., '{ p1: 'v1', p2: { p2_1: 'v21', p2_2: Object } }'. If null, no truncation.
breakLengthDetermines 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:

TypeOutput
stringAs a top level item output without quotes, otherwise in single quotes.
e.g., ( 'str1', { p1: 'str2' } ) => str1, { p2_1: 'v21' }
numbere.g., ( 123e-5 ) => 0.00123
Objecte.g., ( { p1: { p1_1: 'v11' } } ) => { p1: { p1_1: 'v11' } }
Arraye.g., 1, 2, 3 => 1, 2, 3
RegExpe.g., RegExp( /^fred/ ) => /^fred/
booleane.g., true => true
functione.g., function f() {} => Function: f
Symbole.g., Symbol(bert) => Symbol(bert)
BufferA sequence of decimal values
e.g., Buffer.from('4143', 'hex') => 65 67

Methods

Method Parameters

ParameterDescription
optionsThe '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.
levelThe log level for the message. If the specified level is below the configured log level no message shall be output.
parameterXA 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:

1.1.0

7 months ago

1.0.2

10 months ago

1.0.1

11 months ago

1.0.0

11 months ago

0.1.0

11 months ago