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 = ;
Browser
Once the script is loaded, MagiConsole is available on window. You can also require MagiConsole.js with your favorite AMD loader.
var ioConsole = 'IO';var dbConsole = '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;dbConsole;
(nothing is output to the console)
Enable MagiConsole namespaces with regex patterns:
// setup namespace regex pattern:MagiConsole; ioConsole;dbConsole;
> [IO] socket error
Enable all namespaces with a special wildcard string:
MagiConsole; ioConsole;dbConsole;
> [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;MagiConsole; ioConsole;dbConsole;ioConsole;
> [IO] a warning> [DB] an error
Whitelist a loglevel:
var onlyThisLevel = true;MagiConsole;MagiConsole; ioConsole;dbConsole;
> [IO] connected to foo.b.ar
Reenable all console methods:
MagiConsole;MagiConsole; ioConsole;dbConsole;
> [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;
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