cllc
C
ommand L
ine L
ogger and C
ounter
There is a logger and counter two-in-one. And log messages do not erase counter text. Perfect for work process indication in long-time scripts.

Install
npm install cllc
Usage
Logger
Logger output to console log string that consists of timestamp, log level label, logger tag and log message. Only timestamp is not optional.
const log = require('cllc')();
log('Sample message');
Timestamps
Timestamps are formatting by strftime. By default format string is '%T'
, but you can change it at any time. Like this:
log.dateFormat('%F %T');
Any string are correct, even if it doesn't contain any formatting symbols at all.
If you want to disable timestamps just use any falsy value. Like this:
log.dateFormat('');
log.dateFormat();
Any parameter of log.dateFormat
that is not a string or falsy will be ignored.
Log level labels
Default log level label is empty (nothing printed) but you can set default label or specify it explicitly for every log string.
log('log string with default log level label');
log.trace('log string with label <TRACE>');
log.debug('log string with label <DEBUG>');
log.info('log string with label <INFO>');
log.warn('log string with label <WARN>');
log.error('log string with label <ERROR>');
log.level('trace');
log.level('error');
log.level();
Five log levels are possible: trace
, debug
, info
, warn
and error
. Any other parameter in log.level
sets empty default log level label.
Logger tags
Usually tags used if you want to identify several loggers from different modules or functions. Tag is just a short string. By default tag is empty, but you can specify it any time. Like this:
const log = require('cllc')('TAG1');
log('log string with tag "TAG1"');
log.tag('Tag2');
log('log string with tag "Tag2"');
log.tag();
log('log string with no tag');
You can use module
variable on cllc
init or as parameter of log.tag
. In that case module filename and dir will be in tag.
const log = require('cllc')(module);
log('log string with something like "my-module/index" in tag');
Log messages
It's important, that log message is not required. cllc
can output string with only timestamp/label/tag (if not empty). If any params sent to log*
functions then log message will be created same way as in util.format
.
log('This is a log message string', 'This is another log message string');
log();
log({a: 1}, [1, 2], new Date(), null);
Counter
Counter is a text in the end of console, contains digital value(s) that are incrementing step by step.
Start counter
log.start();
Increment counter
log.step();
Stop counter
log.stop();
Restart/change counter
Calling log.start
starting new counter with new text and new values. If another counter was active on that moment it will be destroyed silently (if you want save it - call log.finish
before start next counter).
Current counter text and values are availiable via log.text
and log.counters
functions.
log.start('First counter [%s][%s]', 1, 5);
log start(doSomething(log.text()), ...doSomethingElse(log.counters()));
Safe logger
If counter are visible - log strings appear on string above counter text and will not be erased by counter. So use log
from cllc
instead console.log
when counter active.
log.start('[%s]');
log.step();
log('TEST');
log.finish();
License
MIT