debug icon indicating copy to clipboard operation
debug copied to clipboard

Object colors with a plurality of parameters

Open jfoclpf opened this issue 6 years ago • 6 comments

When one does

debug({a:1, b:2});

the object properties' values are printed with colours

image

but when one does

debug("string", {a:1, b:2});

colours are gone

image

just FYI

jfoclpf avatar Dec 12 '18 23:12 jfoclpf

(just to add some context) This is because debug automatically adds a "%O" string to the arguments when the first one isn't a string, which is then interpreted as a formatter for the next argument; but it doesn't if the first argument is already a string. The arguments are then passed to util.format(), which does the pretty-print, but without the colors.

If you want to have colors when doing

debug("string", {a:1, b:2});

You'll need to manually add the %O:

debug("string %O", {a:1, b:2});

chagris avatar Dec 14 '18 14:12 chagris

Yep, @chagris is exactly right. This is intended behavior.

Qix- avatar Dec 14 '18 15:12 Qix-

@Qix- I see the problem and thank you guys for the reply, but how come is the intended behaviour?

Why simply not

if (typeof args[0] === "string"){
  args[0] += " %0";
}

Am I missing anything here?

jfoclpf avatar Dec 14 '18 17:12 jfoclpf

Because this is how console.log previously worked IIRC.

I would be open to changing it.

Qix- avatar Dec 14 '18 18:12 Qix-

You can very well use the arguments array

if (arguments.length > 1 && typeof arguments[0] === "string"){
  arguments[0] += " %O";
}

what do you think @chagris ?

jfoclpf avatar Dec 14 '18 19:12 jfoclpf

Yeah, my guess was that it was this way for historical reasons (util.format() has a %O since v.8.4.0)

It'd be more or less easy to have it colored one way or another (debug uses the ...args rest parameters) and the current code is pretty similar to what you proposed:

	args[0] = createDebug.coerce(args[0]);

	if (typeof args[0] !== 'string') {
		// Anything else let's inspect with %O
		args.unshift('%O');
	}

If it's not starting with a string, this part could add as many %O as there are arguments in one string that is unshifted, but if there's one it'd need to parse the string, count the already-existing (valid) %* placeholders and add more as needed. (also since Node v10.0 there's util.formatWithOptions which could automatically color everything but that'd break a lot of code that isn't running on a recent Node?)


However, there's currently https://github.com/visionmedia/debug/issues/582 which aims to give more control over the output format, and ultimately, if possible, to get rid of DEBUG_COLORS and the use of isatty(), which'd require changes to how the %O and %o decide to (/ are made to) color their output... If you have any idea about that, would be great to hear!


For now, an already supported way would be to simply override the log method to, for example, console.log

const debug = require('debug');
debug.log = console.log.bind(console);
log('All those objects will be colored (if you run on a recent enough Node version)', {foo: 'bar'}, {foo: 'bar'});

chagris avatar Dec 14 '18 21:12 chagris