aquavitae
aquavitae

Reputation: 19114

Appending to last line logged with winston

I am trying to generate a console log something like this:

2015-08-03T08:17:13.730Z - info:  Doing task A... ✓
2015-08-03T08:19:13.730Z - info:  Doing task B...

So that once a task is complete, a tick appears at the end of the line.

My code is (mostly) synchronous, so in theory something like this should work:

log.info('Doing task A');
taskA();
log.appendLastLine('✓');

Is there a way to do this using winston? Or any other NodeJS logger? There are a few things that are asynchronous, but I can deal with the occasional messed up log line if that happens.

Upvotes: 1

Views: 573

Answers (1)

Andrew Andrew
Andrew Andrew

Reputation: 302

You can use basic stdout object and manually control line breaks:

process.stdout.write('Doing task A');
taskA();
process.stdout.write('✓ \n');

Moreover you can event overwrite messages in the same line, creating a countdown on one line, by adding '\r' at the end.

process.stdout.write('Task A status: ' + mystatus + ' % \r');

Some logging libs also support such mode, but that's basicly a wrapper for stdout, but you can use other features too, like easy measuring taskA() execution time:

var logger = require('devmetrics-core')();
logger.info('Measure function execution time');
taskA = logger.measureTimeWrap(taskA, 'taskA');

Upvotes: 2

Related Questions