consologger

A simple logger so you can manage all your 'console.log()' from a main lib. Coloured output, dynamic prefixing, and on/off switch. Feel free to make requests, report bugs, and suggest ideas.

npm install consologger
16 downloads in the last week
25 downloads in the last month

consologger

A simple logger so you can manage all your 'console.log()' from a main lib. Coloured output, dynamic prefixing, and on/off switch. Feel free to make requests, report bugs, and suggest ideas.

prototypes were not harmed in making this library


.someColorAlias(valueToPrint[, other, values])

my default 'palette', you can change it with .setColor

{
    verbose: 'cyan',
    info:    'green',
    data:    'grey',
    warning: 'yellow',
    error:   'red',
    text:    'white'
}

see code below for examples

.setColor(name, color)

give a name and the color you want to use for this name. this will be added to the 'palette' you have.

.setMode(mode)

enable a mode

  • 'off' to disable printing
  • 'on' to enable priting
  • 'noStylePrefix' to leave prefix out of styling
  • 'stylePrefix' to include prefix in styling

.setPrefix(fn)

pass a function that returns a string which will be used as prefix for whatever consologger prints ( you can include/exclude it from being styled )

Use example

//    test.js for consologger

var log = require('./consologger'),
    i   = 0;

//    chaining supported ( will try to implement same line appending later... )
log
.text('hello', { 'from': 'consologger', 'version': '0.1.4'})
.warning('warning: this is a work in progress...')
.info('we can print everything now. objects, functions, whatever');

log.info({and: {colors: 'package', is: ['not needed now']}});

//    set our favorite colours if we don't like the defaults
log
.setColor('debug', 'blue')
.setColor('info', 'magenta')
.setColor('invertError', 'redBG')
//    let's print some stuff
.info('info: nothing serious happened', 'just a color change')
.debug('log.debug()', 'is now blue!')
.verbose('calculating...')
.data('1 + 2 = '+3)
.strikethrough.data('3 + 2 = '+4)
.error('that\'s wrong!')
.bold.error('very wrong!')
.invertError(['crazy', 'inverted', 'error']);

log
//    switch off the logger
.setMode('off')
.data('logger is now off so you can switch it off in a big project and avoid useless printing')
.text('YOU ARE NOT SUPPOSED TO SEE THIS')
.error(undefined)
//    switch on again
.setMode('on')
.bold.info({"let 's add": {a: 'prefix now'}})
//    set a dynamic prefix for every line
.setPrefix(function() {
    return '['+Date().substr(0,24)+']['+(i+=1)+']';
});

//    print functions
log.text(function hello(again){ return 'back';});

//    remove prefix styling
log.setMode('noStylePrefix');

log.underline.text('click here');

//    revert to have prefix styled
log.setMode('stylePrefix');

log
.data('just kidding ( showing off underline )')
.info({ we: 'have', live: ['time','prefix']});

//    set prefix off
log.setPrefix(function() {
    return '';
});

log.text('keep logging...');
log.underline.text('npmjs.org/package/consologger');

//    TODO: add indentation levels
//    TODO: add same line identing

npm loves you