If you pass an object instead of a string, what gets dumped is [object Object] instead of the actual dump of that object. One solution could be to possibly check if it's of type 'object' and if so, either just return the object unstyled, or JSON.stringify() it, but I'm not sure that's ideal.
Not interested in changing that behavior. It's been like that for years and you're the first to complain. If I had done it again I would have typechecked the input and thrown a TypeError, but that's not worth doing a breaking change over.
@HillTravis for the record, you could always do
console.log(require('util').inspect(obj, {colors:true, depth:null}));
@Qix- thanks, I didn't know about that!
Not outputting the contents of an object argument is basically why I cannot use chalk. I'm using console methods log, info, debug, warn, error extensively throughout my code, and I'm almost never concatenating the individual parts, but passing them as separate arguments, as you can never be sure what's the actual value is if it is converted to String, and it is also simpler. Outputting objects on regular bases.
What I wanted with Chalk is making the different levels more obvious. However, even if Chalk outputs all the arguments (as opposed to color.js, that outputs just the first one, although it displays its content), it outputs objects as object [Object], which is almost pointless info to be seen in the log.
@JanisE not to be rude but that sound like laziness on your part. Chalk outputting contents of objects is opinionated - meaning it breaks the basic JavaScript contract of .toString() showing us the string equivalent of a particular value.
If you want your output in another format, that's up to you as it's your application and thus your opinion. Chalk isn't a formatting library, it's a coloring library - node modules tend to be one-trick-ponies and thus chalk doesn't do anything more or less than color terminal output. It attempts to have no opinions on how the data is actually dislalyed.
You can always wrap chalk to do what you want, or even use template literals or make your own wrapped template tag handler. There are a variety of options to do what you want.
Hope this helps.
@Qix- , thanks for the polite reply! :)
Yes, I'm lazy. That's why I'm searching for a library, not using '\x1b[36m%s\x1b[0m'-like prefixes for outputting messages.
My use-case is that I do not want any fancy colouring options, like boldness, different colours in the same message and the like – which Chalk offers.
I just want the console.log output to be coloured depending on the log level (debug, info, warn, error), that's it.
It seems simple enough for me to hope for an existing Node module that does this. Apparently, I just came across this library that turned out to be meant for something else.
@JanisE write a helper script:
// log.js
const chalk = require('chalk');
module.exports = {
info: (...args) => console.log(chalk.cyan(...args)),
warning: (...args) => console.log(chalk.yellow(...args)),
error: (...args) => console.log(chalk.red(...args))
};
const log = require('./log');
log.info('This is an info in Cyan');
log.warning('This is a warning in Yellow');
log.error('This is an error in Red');
Then you can customize the output format how you like in the log.js file.
Thank you! :)
Most helpful comment
@HillTravis for the record, you could always do