Skip to content

octatone/MagiConsole

Repository files navigation

NPM version Dependency Status Build Status Code Climate Code Climate Coverage

MagiConsole

A magical namespaced console wrapper with loglevel support for node.js and the browser.

Usage

Require or include MagiConsole and create some namespaced console objects:

Node

var MagiConsole = require('magiconsole');

Browser

<script TYPE="text/javascript" src="MagiConsole.min.js"></script>

Once the script is loaded, MagiConsole is available on window. You can also require MagiConsole.js with your favorite AMD loader.

var ioConsole = new MagiConsole('IO');
var dbConsole = new MagiConsole('DB');

The MagiConsole constructor caches namespaced instances. You can require and create namespaced consoles throughout your project, but only one object instance will be created per namespace.

Namespaces

By default nothing is output from magiconsole:

ioConsole.info('web socket connected');
dbConsole.info('useing indexeddb');

(nothing is output to the console)

Enable MagiConsole namespaces with regex patterns:

// setup namespace regex pattern:
MagiConsole.setPattern('IO');

ioConsole.error('socket error');
dbConsole.warn('table not found');
> [IO] socket error

Enable all namespaces with a special wildcard string:

MagiConsole.setPattern('*');

ioConsole.debug('200 OK');
dbConsole.log('all data loaded');
> [IO] 200 OK
> [DB] all data loaded

Enable namespaces via environment variables:

> MLOG=foo node fooAndBarLogs.js

MagiConsole.setPattern

Sets the current regex pattern namespaces are tested against.

MagiConsole.setPattern('network|db') will allow all namespaces containing 'network' or 'db'.

MagiConsole.setPattern('^file') will allow all namespaces starting with 'file'.

MagiConsole.setPattern('^(?!io).+') will allow all namespaces that do not start with 'io'.

Log Levels

MagiConsole wraps all methods normally available via console in modern browsers and ensures that the leveled methods error, warn, log, info and debug are always available both in the browser and node.

By default no log level is set and all methods are enabled to write to the console.

Set a log level and allow only messages of the same or greater severity:

var onlyThisLevel = false;
MagiConsole.setLevel('warn', onlyThisLevel);
MagiConsole.setPattern('*');

ioConsole.warn('a warning');
dbConsole.error('an error');
ioConsole.debug('a boring debug message');
> [IO] a warning
> [DB] an error

Whitelist a loglevel:

var onlyThisLevel = true;
MagiConsole.setLevel('warn', onlyThisLevel);
MagiConsole.setPattern('*');

ioConsole.log('connected to foo.b.ar');
dbConsole.warn('write not completed within 100ms');
> [IO] connected to foo.b.ar

Reenable all console methods:

MagiConsole.setLevel('*');
MagiConsole.setPattern('*');

ioConsole.log('connecting ...');
dbConsole.warn('store not found');
> [IO] connecting ...
> [DB] store not found

Set log level via environment variable:

> MLEVEL=info node allSortsOfMagiConsoleMethods.js

Reset

Reset MagiConsole to the default state of not logging anything:

MagiConsole.reset();

Environment Variables

MagiConsole can be configured via the command line using environment variables:

  • MLOG sets the namespace regex pattern

  • MLEVEL sets the loglevel

  • MLEVELONLY set to only allow the loglevel and no other severities

Develop and contribute

You need to have node and npm installed. Then fork this repo and open it in your terminal.

Install dependencies

$ make install

Run tests

$ make test

Run tests and watch for changes

$ make start

Generate coverage report

$ make coverage

Run JSLint

$ make lint

Build for distribution

$ make build

Resources