📅  最后修改于: 2022-03-11 14:58:29.557000             🧑  作者: Mango
const STACK_LINE_REGEX = /(\d+):(\d+)\)?$/;
function lineLogger(...log) {
let err;
try {
throw new Error();
} catch (error) {
err = error;
}
try {
const stacks = err.stack.split('\\n');
const [, line] = STACK_LINE_REGEX.exec(stacks[2]);
return this(`[${line}]`, ...log);
} catch (err) {
return this(...log);
}
}
lineLogger.call(console.log, 'foobar');
lineLogger.call(console.error, 42);