[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [downloads-image]: https://img.shields.io/npm/dm/consologger.svg [npm-url]: https://npmjs.org/package/consologger [npm-image]: https://img.shields.io/npm/v/consologger.svg
A simple logger so you can manage all your 'console.log()' from a main lib. Styled output, dynamic prefixing and custom presets. Feel free to make requests, report bugs, and suggest ideas.
prototypes were not harmed in making this library
Let's see a simple example. ( we assume you use browserify or something so you can use the module on the browser )
// Get the module, which is a constructor.
var Consologger = require('consologger');
// Make a new instance for logging.
var logger = new Consologger();
// Let's start logging.
// We make some green text.
logger.green('Here\'s some green text');
// And then we print it.
logger.print();
You should have some green text in the console! Big deal.
You can chain those calls.
So the above would become
logger
.green('Here\'s some green text')
.print();
Now, you can also chain presets ( styles basically ).
For example, u
is the preset for underline and mono
is the preset for monospace font.
logger
.u.mono.red('red monospace for retro errors')
.print();
And you can add more text/presets before hitting print, so that will result in a line with those combined.
logger
.red('[ERROR] ')
.mono.bgRed('Ln 0 Col 0')
// text is the "plain" preset, without any styles
.text(' Some error happened there (go fix it!)')
.print();
There are some presets coming with the module, covering the most common and simple use cases.
you can see them in the presets.json
file.
But it's very easy to add your own presets. Here's an example
var Consologger = require('consologger');
var logger = new Consologger();
var myNewStyle;
myNewStyle = {
name: 'cyanItalic',
// style is basically CSS
style: {
color: 'cyan',
'font-style': 'italic'
}
};
logger
.addPreset(myNewStyle);
logger
.cyanItalic('my fancy cyan text')
.print();
logger
.text('✅')
.green.lThrough('make a module to make logging awesome')
.print();
You can add a prefix on a consologger instance. Let's say I'm in a context in my code where I want to separate visually whether the logging I see comes from that context or not.
var foo = function(){
var fooLogger = new Consologger();
fooLogger
.bgRed.mono('[ foo() ]')
.prefix();
// ...
fooLogger
.red('not this error again')
.print();
};
setTimeout(foo, 500);
var logger = new Consologger();
logger
.red('not this error again')
.print();
For node there are some presets with terminal specific stuff.
.addPreset
doesn't work because it makes no sense to add a new preset...
contructor of a new consologger instance. That new instance can hold prefix state.
when using a preset ( see list of presets here ), you add the style of that preset to the current styles of the consologger instance.
when passing a text to a preset function, you apply the current styles to that text, and keep it in the consologger line buffer. ( for now, only strings are accepted )
prints whatever the line buffer has, with the styles applied on each string separately. If the consologger instance has a prefix set, that's going to be prepended to the line buffer.
takes the line buffer and saves is as a prefix so that every next .print()
will inlude the prefix value first.
Adds a new preset.
presetObject
must have a 'name' field with a string value, and a 'style' field that's an object and has the CSS values you want to apply.
( for now, you cannot override presets that already exist )
Will make the print
function do nothing. Effectively turning off printing.
Will make the print
function print again, reverting any previous switchOff
call.