Closed p3x-robot closed 5 years ago
How did you solve this?
I'm interested, too. How to do this properly? @p3x-robot @starak
require('console-stamp')(console, {
format: ':date(yyyy/mm/dd HH:MM:ss.l).cyan :p3x.yellow :myLabel',
tokens:{
p3x: () => {
let base;
if (cluster.isWorker) {
const baseName = process.env.NGIVR_SERVER_COMMAND.toUpperCase();
//console.log(process.env)
switch (process.env.NGIVR_SERVER_COMMAND) {
case 'worker':
base = `[${baseName} ${_.padStart(cluster.worker.id, 3, 0)}]`
break;
case 'singleton':
base = `[${baseName}]`
break;
default:
throw new Error(`Unknown fork command: ${process.env.NGIVR_SERVER_COMMAND}`)
}
} else {
base = `[MASTER]`;
}
return chalk`{black.grey ${base.padStart(12, ' ')}}` + ` [PID: ${(String(process.pid).padStart(6, 0))}] `;
},
myLabel: ( arg ) => {
const { method, defaultTokens } = arg;
let label = defaultTokens.label( arg );
switch(method) {
case 'error':
label = chalk`{bold.red ${label}}`;
break;
case 'warn':
label = chalk`{bold.blue ${label}}`;
break;
default:
label = chalk`{green ${label}}`;
}
return label;
}
},
});
@starak thanks for pointing me to this. It seems to work only on version 3. Is there a way for doing that in the latest stable version?
I know this is a few years old, but I searched for this for a while and came up with a solution which I believe is neater than the ones above...
export function formatConsole() {
// Custom console methods
// @ts-ignore
console.ok = () => console.log.apply(console, arguments);
consoleStamp(console, {
format: ":date(HH:MM:ss.l).grey :colorLabel()",
extend: {
ok: 4
},
include: ["debug","info","ok","warn","error","log"],
tokens: {
colorLabel: ({method}) => {
switch(method) {
case 'debug': return chalk.gray('[DEBUG]'.padEnd(7, ' '));
case 'info': return chalk.cyan('[INFO]'.padEnd(7, ' '));
case 'ok': return chalk.green('[OK]'.padEnd(7, ' '));
case 'warn': return chalk.yellow('[WARN]'.padEnd(7, ' '));
case 'error': return chalk.red('[ERROR]'.padEnd(7, ' '));
case 'log':
default: return chalk.white('[LOG]'.padEnd(7, ' '));
}
}
}
});
console.ok("Console formatting applied.");
}
Now I'll go back to searching how to add a custom method like ok()
that can still be 'parsed' this way (through consoleStamp)... the example on the readme/doc only explains how to override it completely... but that's outside the topic of this issue :)
Edit: Found it! Just need to add console.ok = () => console.log.apply(console, arguments);
and the other already documented stuff. I've gone ahead and edited my example to include this too.
with the previous version, i could set the color of the type of log