Upon closer inspection near the green arrows, you can see that the default console.log
function colorizes values based on their type, distinguishing between string
and number
.
https://i.sstatic.net/MtO8l.png
In contrast, highlighted near the red arrows is my attempt at using Winston to format logs in a specific manner; unfortunately, this approach abandoned the colorization of those values.
Check out the Replit demo here
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
}),
);
Despite trying with util.inspect
, I have yet to achieve the desired outcome.
UPDATE
Take a look at the updated version here.
We are very close to solving the issue!
However, as seen in the screenshot, keys like 'abc' and 'def' seem to be missing.
https://i.sstatic.net/rAAnz.png
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
}),
);