0

(node.js) if i try to color an object on console.log like so

console.log('\x1b[32m', {a:1,b:2,c:3}, '\x1b[0m');

it looks like this:

enter image description here

how do I make it so the text actually all gets colored?

I know i could stringify the arguments manually then join them to a single string and color them, but that ruins the nice multiline formatting console.log usually employs:

enter image description here

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
stackers
  • 2,701
  • 4
  • 34
  • 66
  • 2
    Perhaps this will help: [colors-in-javascript-console](https://stackoverflow.com/questions/7505623/colors-in-javascript-console) – Ryan Wilson Dec 12 '22 at 16:59
  • Looks like you are fighting against the console styling an object. Maybe convert the object to string if you really want it green. – epascarello Dec 12 '22 at 17:00
  • I tried converting it to a string, but that puts the objects all on one line instead of how they're normally printed in a nice formatted way. – stackers Dec 12 '22 at 17:06
  • What `console` implementation are you asking about? It looks a bit like a nodejs environment or something else that writes to a terminal, but it will work very differently e.g. on the web. – Bergi Dec 12 '22 at 17:24
  • 1
    yeah sorry should have said it's node.js in the vscode terminal – stackers Dec 12 '22 at 17:24
  • @RyanWilson Probably not, that's asking about Chrome console – Bergi Dec 12 '22 at 17:25
  • Probably better to use a library like [Chalk](https://www.npmjs.com/package/chalk) than bash around with raw ANSI. – tadman Dec 12 '22 at 17:26
  • See https://nodejs.org/api/util.html#customizing-utilinspect-colors – Bergi Dec 12 '22 at 17:31
  • yeah im actually using chalk, but it doesn't solve this problem. passing in an object still only colors the first line/ section. – stackers Dec 12 '22 at 17:34
  • @RyanWilson The accepted answer (and the other answers with the same approach) work *only* in browsers, not in the terminal, which is what the OP is asking about – Bergi Dec 12 '22 at 17:35

2 Answers2

1

In order to convert an object to a pretty json string you may pass a 3rd param as the indent size for beautify-ing the input.

var obj = {
  some: "thing",
  values: {
    arr: [12, 15, 24],
    size: null
  }
}

console.log("this is a string: " + JSON.stringify(obj, null, 4))

Then of course you can colorize it using one color. See comments above for techniques.

IT goldman
  • 14,885
  • 2
  • 14
  • 28
  • How does this even remotely answer the question? The question is about coloring the text printed to the console. `"how do I make it so the text actually all gets colored?"` – Ryan Wilson Dec 12 '22 at 17:27
  • It answers one of the comments about convert to string creates one line string – IT goldman Dec 12 '22 at 17:29
  • 1
    he's right actually! – stackers Dec 12 '22 at 17:29
  • 1
    `JSON.stringify` is **not** the "*the nice multiline formatting `console.log` usually employs*". [`util.inspect`](https://nodejs.org/api/util.html#utilinspectobject-options) is much more elaborate. – Bergi Dec 12 '22 at 17:32
0

You may have to set FORCE_COLOR environment variable to 1 / 2 or 3. See docs for more details

Node JS has built in support for colors so simply: console.log('Some text', someObject) will work just fine.

Azure pipelines supports 2.

Mateusz Budzisz
  • 598
  • 7
  • 15