Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to suppress timestamps when useColors is false - maintains backwards compatibility #488

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ change the behavior of the debug logging:
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
| `DEBUG_DEPTH` | Object inspection depth. |
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
| `DEBUG_TIMESTAMPS` | Turns off timestamps when DEBUG_COLORS is false. |


__Note:__ The environment variables beginning with `DEBUG_` end up being
Expand Down
1 change: 1 addition & 0 deletions src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function createDebug(namespace) {
debug.namespace = namespace;
debug.enabled = exports.enabled(namespace);
debug.useColors = exports.useColors();
debug.printTimestamp = exports.printTimestamp ? exports.printTimestamp() : true;
debug.color = selectColor(namespace);
debug.destroy = destroy;

Expand Down
19 changes: 17 additions & 2 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.printTimestamp = printTimestamp;

/**
* Colors.
Expand All @@ -43,7 +44,7 @@ try {
/**
* Build up the default `inspectOpts` object from the environment variables.
*
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled DEBUG_TIMESTAMPS=no node script.js
*/

exports.inspectOpts = Object.keys(process.env).filter(function (key) {
Expand Down Expand Up @@ -76,6 +77,17 @@ function useColors() {
: tty.isatty(process.stderr.fd);
}

/**
* Print timestamps when useColours() is false. This flag enables
* timestamps to be turned off when useColours() is false.
*/

function printTimestamp() {
return 'timestamps' in exports.inspectOpts
? Boolean(exports.inspectOpts.timestamps)
: true;
}

/**
* Map %o to `util.inspect()`, all on a single line.
*/
Expand Down Expand Up @@ -104,6 +116,7 @@ exports.formatters.O = function(v) {
function formatArgs(args) {
var name = this.namespace;
var useColors = this.useColors;
var printTimestamp = this.printTimestamp;

if (useColors) {
var c = this.color;
Expand All @@ -112,9 +125,11 @@ function formatArgs(args) {

args[0] = prefix + args[0].split('\n').join('\n' + prefix);
args.push(colorCode + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
} else {
} else if (printTimestamp) {
args[0] = new Date().toISOString()
+ ' ' + name + ' ' + args[0];
} else {
args[0] = name + ' ' + args[0];
}
}

Expand Down