A magical namespaced console wrapper with loglevel support for node.js and the browser.
Require or include MagiConsole and create some namespaced console objects:
var MagiConsole = require('magiconsole');
<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.
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
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'.
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 MagiConsole to the default state of not logging anything:
MagiConsole.reset();
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
You need to have node and npm installed. Then fork this repo and open it in your terminal.
$ make install
$ make test
$ make start
$ make coverage
$ make lint
$ make build