A simple logger so you can manage all your 'console.log()' from a main lib. Feel free to make requests, report bugs, and suggest ideas.
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [downloads-image]: http://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 = ;// Let's start logging.// We make some green text.loggergreen'Here\'s some green text';// And then we print it.loggerprint;
You should have some green text in the console! Big deal.
You can chain those calls.
So the above would become
loggergreen'Here\'s some green text'print;
Now, you can also chain presets ( styles basically ).
u is the preset for underline and
mono is the preset for monospace font.
loggerumonored'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.
loggerred'[ERROR] 'monobgRed'Ln 0 Col 0'// text is the "plain" preset, without any stylestext' 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
But it's very easy to add your own presets. Here's an example
var Consologger = require'consologger';var logger = ;var myNewStyle;myNewStyle =name: 'cyanItalic'// style is basically CSSstyle:color: 'cyan''font-style': 'italic';loggeraddPresetmyNewStyle;loggercyanItalic'my fancy cyan text'print;loggertext'✅'greenlThrough'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.
varvar fooLogger = ;fooLoggerbgRedmono'[ foo() ]'prefix;// ...fooLoggerred'not this error again'print;;setTimeoutfoo 500;var logger = ;loggerred'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
Will make the