📜  行记录器 - 任何代码示例

📅  最后修改于: 2022-03-11 14:58:29.557000             🧑  作者: Mango

代码示例1
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);