Skip to content

[usable, not-being-maintained] Log used in NodeJS environment; featuring parsing objects using `util.inspect` and coloured outputs; supporting NodeJS 6 and above.

License

Notifications You must be signed in to change notification settings

unknownmoon/utility-node-log

Repository files navigation

utility-node-log

Note that this project is still in preview stage

GitHub version Dependency Status devDependency Status

Master Develop
Build Status Master Build Status Develop
codecov codecov

Log utility used in NodeJS environment.

  • Featuring parsing objects using util.inspect and coloured outputs;
  • Supporting NodeJS 6 and above.

The currently supported log levels are

Level Description
MUTE Avoid all outputs
INFO Informational outputs
LOG Generic logging, when enabled, the outputs of INFO will also be printed.
WARN Warning, when enabled, the outputs of INFO and LOG will also be printed.
ERROR Errors, when enabled, the outputs of INFO, LOG and WARN will also be printed
DEBUG Debugging information, when enabled, the outputs of INFO, LOG, WARN and ERROR will also be printed
VERBOSE Anything, including the outputs of all of the other logging levels.

Set LOG_${levelName} environment variable to true to enable the given level, i.e. LOG_DEBUG=true;

Basic unit test powered by Mocha/Chai/Sinon;

Table of Contents

Installation

Install from GitHub Repository

# command format
npm install --save github:unknownmoon/utility-node-log#<version>

# example
npm i -S github:unknownmoon/utility-node-log#v0.0.4

Basic Module Usages

Detailed APIs please reference to the JSDoc. Generate JSDoc Documentation

Set log level programmatically

import {logger} from 'utility-node-log';

// if muted
logger.logLevel = 'MUTE';
logger.log('anything'); // no outputs

// if error
logger.logLevel = 'ERROR';
logger.log('anything'); // [<time>][LOG] anything
logger.error('anything'); // [<time>][ERROR] anything 
logger.debug('anything'); // no outputs

Set time format programmatically

import {logger} from 'utility-node-log';

// will be used to feed `moment.format`
logger.timeFormat = 'HH.mm.ss.SS';

Wrap a content with colour

import {logger} from 'utility-node-log';

// used `chalk` colour functions to do the job
const result = logger.highlight('something');
// result: '<color-code>something<color-code>'

Change colour functions

The currently supported colour cases are:

  • highlight
  • time
  • info
  • warn
  • error
  • debug - also used in log and verbose cases.
  • help - not in use
import {logger} from 'utility-node-log';
import chalk from 'chalk';

// only the given functions will be overridden
logger.colorMapping = new Map([
    ['highlight', chalk.cyan],
    ['time', chalk.gray]
]);

Logging APIs

import {logger} from 'utility-node-log';

// similar to use `console.log`, but with `[<time>][<log level>]` wrapper and coloured.
logger.info('anything'); // [<time>][INFO] anything
logger.log('anything'); // [<time>][LOG] anything
logger.warn('anything'); // [<time>][WARN] anything
logger.error('anything'); // [<time>][ERROR] anything
logger.debug('anything'); // [<time>][DEBUG] anything
logger.verbose('anything'); // [<time>][VERBOSE] anything

// -------

logger.inspect({ a: 1 }); 
// is the shorthand of
logger.debug( require( 'util' )
  .inspect( { a: 1 }, { depth: 7, colors: true } ) );

Development

Initialisation

# Have Node ^6.0.0 & NPM ^3.8.6 installed

# install dependencies
npm install

Clean Up

# remove the built code, for now only the test result 
npm run clean

# remove the built code and node modules
npm run reset

Test

Coverage report can be found in ./coverage folder.

npm test

Build

The source code is written in ES2015, hence before NodeJS fully support ES2015, we need to build the code to es2015-node using [Babel][babel-link].

The built code can be found in ./dist folder.

npm run build

Generate JSDoc Documentation

# generate the documentation
npm run doc

# serve the generated documentation using `http-server`
# note that no watch functionality is hooked, hence
# changing code won't trigger documentation regeneration.
npm run serve-doc

Release

Shorthand script to generate release content, including ./coverage, ./jsdoc and ./dist.

npm run release

About

[usable, not-being-maintained] Log used in NodeJS environment; featuring parsing objects using `util.inspect` and coloured outputs; supporting NodeJS 6 and above.

Resources

License

Stars

Watchers

Forks

Packages

No packages published