mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-14 07:39:29 -07:00
30 lines
773 B
JavaScript
30 lines
773 B
JavaScript
import chalk from 'chalk';
|
|
|
|
const describe = (jsHandle) => jsHandle.evaluate(
|
|
// serialize |obj| however you want
|
|
(obj) => `OBJ: ${typeof obj}, ${obj}`,
|
|
jsHandle,
|
|
);
|
|
|
|
const colors = {
|
|
LOG: chalk.grey,
|
|
ERR: chalk.red,
|
|
WAR: chalk.yellow,
|
|
INF: chalk.cyan,
|
|
};
|
|
|
|
const formatter = async (message) => {
|
|
const args = await Promise.all(message.args().map((arg) => describe(arg)));
|
|
// make ability to paint different console[types]
|
|
const type = message.type().substr(0, 3).toUpperCase();
|
|
const color = colors[type] || chalk.blue;
|
|
let text = '';
|
|
for (let i = 0; i < args.length; i += 1) {
|
|
text += `[${i}] ${args[i]} `;
|
|
}
|
|
text += message?.stackTrace()?.[0]?.url ?? '';
|
|
console.log(color(`CONSOLE.${type}: ${message.text()}\n${text} `));
|
|
};
|
|
|
|
export default formatter;
|