Skip to content

Instantly share code, notes, and snippets.

@osv
Last active February 6, 2018 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osv/736dffbb36ef50151cb87bbe4109b925 to your computer and use it in GitHub Desktop.
Save osv/736dffbb36ef50151cb87bbe4109b925 to your computer and use it in GitHub Desktop.
Timestamp standard input with relative timestamp and less noise using node.js

Pretty formatter with relative time stamp, inspired by moreutils ts but with less noise, print only changes :)

Install

Save file

chmod +x ts.js

Example of usage

For example for express app that use debug and frontail and with colors:

DEBUG_COLORS=yes DEBUG="*,-follow-redirects" npm run serve:ssr 2>&1| ./ts.js | frontail --port 8888 -

Or just in console:

21:55 $ DEBUG_COLORS=yes DEBUG="*,-follow-redirects" npm run serve:ssr 2>&1 | ./ts.js 
[21:02  2.246  +293] 
[        .255    +0] > node dist/server
[                  ] 
[21:02  3.133  +878]   express:router:route new '/all' +0ms
[        .134    +1]   express:router:layer new '/all' +5ms
[        .135    +1]   express:router:route get '/all' +2ms
[        .136    +1]   express:router:layer new '/' +0ms
[        .136    +0]   express:router:route new '/cache' +1ms
[                  ]   express:router:layer new '/cache' +0ms
[                  ]   express:router:route get '/cache' +0ms
[        .138    +2]   express:router:layer new '/' +0ms
[        .138    +0]   express:router:route new '/clear-api-cache' +0ms
[                  ]   express:router:layer new '/clear-api-cache' +0ms
[        .141    +3]   express:router:route post '/clear-api-cache' +0ms
[        .141    +0]   express:router:layer new '/' +0ms
[                  ]   express:router:route new '/precache' +0ms
[                  ]   express:router:layer new '/precache' +1ms
[                  ]   express:router:route post '/precache' +0ms
[                  ]   express:router:layer new '/' +0ms
[        .142    +1]   express:router use '/api' router +0ms
[        .142    +0]   express:router:layer new '/api' +0ms
#!/usr/bin/env node
const readline = require('readline');
function formatDate(format, now, last){
let delta = '+' + (now - last);
format = format.replace('%r', pad(delta, 5, ' '));
format = format.replace('%ss', pad(now.getSeconds(),2, ' '));
format = format.replace('%S', pad(now.getSeconds(),2));
format = format.replace('%.S', pad(now.getMilliseconds(), 3));
format = format.replace('%s', now.getSeconds());
format = format.replace('%dd', pad(now.getDate(),2));
format = format.replace('%d', now.getDate());
format = format.replace('%mm', pad(now.getMinutes(),2));
format = format.replace('%m', now.getMinutes());
format = format.replace('%MM', pad(now.getMonth()+1,2));
format = format.replace(/%M(?![ao])/, now.getMonth()+1);
format = format.replace('%yyyy', now.getFullYear());
format = format.replace('%YYYY', now.getFullYear());
format = format.replace('%yy', (now.getFullYear()+"").substring(2));
format = format.replace('%YY', (now.getFullYear()+"").substring(2));
format = format.replace('%HH', pad(now.getHours(),2));
format = format.replace('%H', now.getHours());
return format;
function zeroIfEqual(a, b) {
return a == b ? ' '.repeat(a.length) : a;
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
}
function zeroIfEqual(a, b) {
return a == b ? ' '.repeat(a.length) : a;
}
const rl = readline.createInterface({
input: process.stdin,
crlfDelay: Infinity
});
let last = new Date();
let lastTimeStamp = '';
let lastDelta = '';
// Read line by line and print timestamp but only value
rl.on('line', (line) => {
let now = new Date();
let timeStamp = formatDate('%HH:%MM %ss', now, last);
let delta = formatDate('.%.S %r', now, last);
console.log('[' + zeroIfEqual(timeStamp, lastTimeStamp) +
zeroIfEqual(delta, lastDelta) +
'] ' + line);
last = now;
lastTimeStamp = timeStamp;
lastDelta = delta;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment