Issue
As shown near the green arrows, the default console.log
colorizes values based on whether they are string
or number
.
As shown near the red arrows, my attempt at using Winston to format logs a certain way unfortunately abandons the colorizing of those values.
function getArgumentsPreserved(providedArguments: Arguments): string {
// https://nodejs.org/en/knowledge/getting-started/how-to-use-util-inspect/
// https://github.com/chalk/chalk/issues/118#issuecomment-1221385194
return util.inspect(providedArguments, { colors: false, depth: null });
}
const simpleConsoleLogging = winston.format.combine(
// Simple console logging for local environment.
winston.format.timestamp(),
winston.format.printf((info) => {
const { level, message, timestamp, ...rest } = info;
const coloredTimestampAndLevel = colorizer.colorize(level, `${timestamp} ${level}:`);
//const syntaxHighlightedObjects = Object.keys(rest).length > 0 ? getArgumentsPreserved(rest) : '';
const syntaxHighlightedObjects = Object.keys(rest).length > 0 ? JSON.stringify(rest) : ''; // TODO: How can we reenable the syntax coloring that console.log had by default?
return `${coloredTimestampAndLevel} ${message} ${syntaxHighlightedObjects}`; // https://github.com/winstonjs/winston/issues/1388#issuecomment-432932959
}),
);
I've experimented with util.inspect
but haven't gotten it to work.
UPDATE
Now see the new version here.
It's very close to working!
But as you can see in the screenshot, keys like 'abc' and 'def' are missing.
function getArgumentsPreserved(providedArguments: Arguments): string {
if (Object.keys(providedArguments).length > 0) {
const copiedArr = getRealKeys(providedArguments);
// https://nodejs.org/en/knowledge/getting-started/how-to-use-util-inspect/
// https://github.com/chalk/chalk/issues/118#issuecomment-1221385194
return util.inspect(copiedArr, { colors: true, depth: null });
} else {
return '';
}
}
function getRealKeys(rest: Arguments) {
const result: string[] = [];
Object.keys(rest).forEach(key => {
result.push(rest[key])
})
return result;
}
const simpleConsoleLogging = winston.format.combine(
// Simple console logging for local environment.
winston.format.timestamp(),
winston.format.printf((info) => {
const { level, message, timestamp, ...rest } = info;
const coloredTimestampAndLevel = colorizer.colorize(level, `${timestamp} ${level}:`);
//const syntaxHighlightedObjects = Object.keys(rest).length > 0 ? util.inspect(copiedArr, undefined, undefined, true) : ''; // https://stackoverflow.com/questions/74186705/how-to-preserve-default-syntax-highlighting-colors-in-javascript-console
const syntaxHighlightedObjects = getArgumentsPreserved(rest); // https://stackoverflow.com/questions/74186705/how-to-preserve-default-syntax-highlighting-colors-in-javascript-console
return `${coloredTimestampAndLevel} ${message} ${syntaxHighlightedObjects}`; // https://github.com/winstonjs/winston/issues/1388#issuecomment-432932959
}),
);
Solution
console.log
in Node.js actually uses the inspect
function from the built-in util
module, so you could just use that as well:
const syntaxHighlightedObjects = util.inspect(rest, undefined, undefined, true);
The last argument is if we want color or not (spoiler alert: we do). The second and third are "show hidden properties" and "depth to display", respectively.
If you only want to display the "real" keys, you can do that like this:
function getArgumentsPreserved(providedArguments: Arguments): string {
if (Object.keys(providedArguments).length > 0) {
const copied = Object.fromEntries(Object.entries(providedArguments));
return util.inspect(copied, { colors: true, depth: null, showHidden: false });
} else {
return '';
}
}
This works because Object.entries
only gets the object's own enumerable keys, which is what you were originally after. Then we can use Object.fromEntries
to quickly get back an object.
Answered By - caTS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.