magiconsole v0.2.2
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 errorEnable all namespaces with a special wildcard string:
MagiConsole.setPattern('*');
ioConsole.debug('200 OK');
dbConsole.log('all data loaded');> [IO] 200 OK
> [DB] all data loadedEnable namespaces via environment variables:
> MLOG=foo node fooAndBarLogs.jsMagiConsole.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 errorWhitelist 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.arReenable all console methods:
MagiConsole.setLevel('*');
MagiConsole.setPattern('*');
ioConsole.log('connecting ...');
dbConsole.warn('store not found');> [IO] connecting ...
> [DB] store not foundSet log level via environment variable:
> MLEVEL=info node allSortsOfMagiConsoleMethods.jsReset
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:
MLOGsets the namespace regex patternMLEVELsets the loglevelMLEVELONLYset 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 installRun tests
$ make testRun tests and watch for changes
$ make startGenerate coverage report
$ make coverageRun JSLint
$ make lintBuild for distribution
$ make build