From 6ba24214b5ea22e45e9fe6136b323b195d499922 Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Tue, 31 Jul 2012 18:58:21 +0400 Subject: [PATCH 1/8] show process pid in log --- lib/debug.js | 85 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 969d1222..0390ba73 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -16,7 +16,8 @@ module.exports = debug; */ var names = [] - , skips = []; + , skips = [] + , showPID = false; (process.env.DEBUG || '') .split(/[\s,]+/) @@ -25,10 +26,25 @@ var names = [] if (name[0] === '-') { skips.push(new RegExp('^' + name.substr(1) + '$')); } else { - names.push(new RegExp('^' + name + '$')); + if (name == 'PID') { + showPID = true; + } else { + names.push(new RegExp('^' + name + '$')); + } } }); +function isEnabled(name) { + var disabled = skips.some(function(re) { + return re.test(name); + }); + if (disabled) return false; + return names.some(function(re) { + return re.test(name); + }); +} + + /** * Colors. */ @@ -64,6 +80,16 @@ function color() { return colors[prevColor++ % colors.length]; } +function c3(c) { + return '\033[3' + c + 'm'; +} + +function c9(c) { + return '\033[9' + c + 'm'; +} + +var clearColor = '\033[0m'; + /** * Humanize the given `ms`. * @@ -92,44 +118,37 @@ function humanize(ms) { */ function debug(name) { - function disabled(){} - disabled.enabled = false; - - var match = skips.some(function(re){ - return re.test(name); - }); - - if (match) return disabled; + var log = function disabled(){} - match = names.some(function(re){ - return re.test(name); - }); + if (isEnabled(name)) { - if (!match) return disabled; - var c = color(); + if (isatty) { + var c = color() + , pid = showPID ? c9(Number(process.pid) % 7) + process.pid + ' ' + c9(0) : '' + , pre = ' ' + pid + c9(c) + name + ' ' + c3(c) + c9(0) + , post = ' ' + c3(c) + c9(c) + '+' - function colored(fmt) { - var curr = new Date; - var ms = curr - (prev[name] || curr); - prev[name] = curr; + log = function colored(fmt) { + var curr = new Date; + var ms = curr - (prev[name] || curr); + prev[name] = curr; - fmt = ' \033[9' + c + 'm' + name + ' ' - + '\033[3' + c + 'm\033[90m' - + fmt + '\033[3' + c + 'm' - + ' +' + humanize(ms) + '\033[0m'; + fmt = pre + fmt + post + humanize(ms) + clearColor; + console.error.apply(this, arguments); + } + } else { + var pre = showPID ? process.pid + ' ' : '' + , post = ' ' + name + ' ' - console.error.apply(this, arguments); - } + log = function plain(fmt) { + fmt = pre + new Date().toUTCString() + post + fmt; + console.error.apply(this, arguments); + } + } - function plain(fmt) { - fmt = new Date().toUTCString() - + ' ' + name + ' ' + fmt; - console.error.apply(this, arguments); + log.enabled = true; } - colored.enabled = plain.enabled = true; - return isatty - ? colored - : plain; + return log; } From 9165d07504366d16207eb609d6a397d67b95cde3 Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Thu, 30 Aug 2012 19:58:06 +0400 Subject: [PATCH 2/8] add specific loggers (error, debug, warn, info) --- lib/debug.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 0390ba73..184e6732 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -117,15 +117,16 @@ function humanize(ms) { * @api public */ -function debug(name) { +function debug(name, kind, kindColor) { var log = function disabled(){} - if (isEnabled(name)) { + if (isEnabled(name) || (kind && isEnabled(kind + ':' + name))) { if (isatty) { var c = color() , pid = showPID ? c9(Number(process.pid) % 7) + process.pid + ' ' + c9(0) : '' - , pre = ' ' + pid + c9(c) + name + ' ' + c3(c) + c9(0) + , knd = kind ? c9(kindColor) + kind + ' ' + c9(0) : '' + , pre = ' ' + pid + knd + c9(c) + name + ' ' + c3(c) + c9(0) , post = ' ' + c3(c) + c9(c) + '+' log = function colored(fmt) { @@ -137,7 +138,9 @@ function debug(name) { console.error.apply(this, arguments); } } else { - var pre = showPID ? process.pid + ' ' : '' + var pid = showPID ? process.pid + ' ' : '' + , knd = kind ? kind + ' ' : '' + , pre = pid + knd , post = ' ' + name + ' ' log = function plain(fmt) { @@ -149,6 +152,12 @@ function debug(name) { log.enabled = true; } + if (!kind) { + log.error = debug(name, 'ERROR', 1); + log.debug = debug(name, 'DEBUG', 2); + log.warn = debug(name, 'WARN', 3); + log.info = debug(name, 'INFO', 4) + } return log; } From b9468ce57725b806adfb51bbeec84926aabe43ba Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Thu, 6 Sep 2012 19:14:50 +0400 Subject: [PATCH 3/8] use ISO formatted date in file output --- lib/debug.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/debug.js b/lib/debug.js index 184e6732..1977dfae 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -144,7 +144,7 @@ function debug(name, kind, kindColor) { , post = ' ' + name + ' ' log = function plain(fmt) { - fmt = pre + new Date().toUTCString() + post + fmt; + fmt = pre + new Date().toISOString() + post + fmt; console.error.apply(this, arguments); } } @@ -161,3 +161,25 @@ function debug(name, kind, kindColor) { return log; } + +if (!Date.prototype.toISOString) { + (function() { + function pad(number) { + var r = String(number); + if (r.length === 1) { + r = '0' + r; + } + return r; + } + Date.prototype.toISOString = function() { + return this.getUTCFullYear() + + '-' + pad( this.getUTCMonth() + 1 ) + + '-' + pad( this.getUTCDate() ) + + 'T' + pad( this.getUTCHours() ) + + ':' + pad( this.getUTCMinutes() ) + + ':' + pad( this.getUTCSeconds() ) + + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 ) + + 'Z'; + }; + }()); +} From 58560ea43791e07b2db70e05b11ecbd08e6e5589 Mon Sep 17 00:00:00 2001 From: Dmitry Lipovoi Date: Tue, 20 Nov 2012 18:23:57 +0400 Subject: [PATCH 4/8] absolute time in tty mode --- lib/debug.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/debug.js b/lib/debug.js index 1977dfae..63be9c04 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -134,7 +134,7 @@ function debug(name, kind, kindColor) { var ms = curr - (prev[name] || curr); prev[name] = curr; - fmt = pre + fmt + post + humanize(ms) + clearColor; + fmt = new Date().toISOString() + pre + fmt + post + humanize(ms) + clearColor; console.error.apply(this, arguments); } } else { From 17760fedea02441be8470cd2a1643ea3bd6681cb Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Wed, 27 Jan 2016 11:49:19 +0300 Subject: [PATCH 5/8] write to stderr --- lib/debug.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 63be9c04..28a11c52 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -4,6 +4,7 @@ */ var tty = require('tty'); +var util = require('util'); /** * Expose `debug()` as the module. @@ -119,6 +120,7 @@ function humanize(ms) { function debug(name, kind, kindColor) { var log = function disabled(){} + var format = util.format; if (isEnabled(name) || (kind && isEnabled(kind + ':' + name))) { @@ -135,7 +137,9 @@ function debug(name, kind, kindColor) { prev[name] = curr; fmt = new Date().toISOString() + pre + fmt + post + humanize(ms) + clearColor; - console.error.apply(this, arguments); + var out = process.stderr; + out.write(format.apply(this, arguments)); + out.write("\n"); } } else { var pid = showPID ? process.pid + ' ' : '' @@ -145,7 +149,9 @@ function debug(name, kind, kindColor) { log = function plain(fmt) { fmt = pre + new Date().toISOString() + post + fmt; - console.error.apply(this, arguments); + var out = process.stderr; + out.write(format.apply(this, arguments)); + out.write("\n"); } } From 4b776e6b1d82f6a7abf48ed233883ee392c3467e Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Wed, 27 Jan 2016 11:55:33 +0300 Subject: [PATCH 6/8] use single write --- lib/debug.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 28a11c52..203e7fa3 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -137,9 +137,7 @@ function debug(name, kind, kindColor) { prev[name] = curr; fmt = new Date().toISOString() + pre + fmt + post + humanize(ms) + clearColor; - var out = process.stderr; - out.write(format.apply(this, arguments)); - out.write("\n"); + process.stderr.write(format.apply(this, arguments) + "\n"); } } else { var pid = showPID ? process.pid + ' ' : '' @@ -149,9 +147,7 @@ function debug(name, kind, kindColor) { log = function plain(fmt) { fmt = pre + new Date().toISOString() + post + fmt; - var out = process.stderr; - out.write(format.apply(this, arguments)); - out.write("\n"); + process.stderr.write(format.apply(this, arguments) + "\n"); } } From 03d30ad97579456cd407a4cadb0ac01e0375ffbf Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Mon, 16 Apr 2018 16:08:40 +0300 Subject: [PATCH 7/8] add trace level --- lib/debug.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/debug.js b/lib/debug.js index 203e7fa3..de2be772 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -158,7 +158,8 @@ function debug(name, kind, kindColor) { log.error = debug(name, 'ERROR', 1); log.debug = debug(name, 'DEBUG', 2); log.warn = debug(name, 'WARN', 3); - log.info = debug(name, 'INFO', 4) + log.info = debug(name, 'INFO', 4); + log.trace = debug(name, 'TRACE', 5); } return log; From 54e19d3f199fe27628cdc5c16ea9dce157fc89cc Mon Sep 17 00:00:00 2001 From: Sergey Martynov Date: Mon, 16 Apr 2018 16:22:54 +0300 Subject: [PATCH 8/8] justify kind to 5 symbols wide --- lib/debug.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/debug.js b/lib/debug.js index de2be772..c70231c9 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -123,6 +123,7 @@ function debug(name, kind, kindColor) { var format = util.format; if (isEnabled(name) || (kind && isEnabled(kind + ':' + name))) { + if (kind && kind.length == 4) kind = kind + ' '; if (isatty) { var c = color()