diff --git a/lib/logger.js b/lib/logger.js index 25f54be..32600e9 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -9,16 +9,23 @@ const configuration = require("./configuration"); const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/; function defaultParseCallStack(data, skipIdx = 4) { - const stacklines = data.stack.split("\n").slice(skipIdx); - const lineMatch = stackReg.exec(stacklines[0]); - if (lineMatch && lineMatch.length === 6) { - return { - functionName: lineMatch[1], - fileName: lineMatch[2], - lineNumber: parseInt(lineMatch[3], 10), - columnNumber: parseInt(lineMatch[4], 10), - callStack: stacklines.join("\n") - }; + try { + const stacklines = data.stack.split("\n").slice(skipIdx); + const lineMatch = stackReg.exec(stacklines[0]); + /* istanbul ignore else */ + if (lineMatch && lineMatch.length === 6) { + return { + functionName: lineMatch[1], + fileName: lineMatch[2], + lineNumber: parseInt(lineMatch[3], 10), + columnNumber: parseInt(lineMatch[4], 10), + callStack: stacklines.join("\n") + }; + } + } + catch (err) { + // will never get error unless nodejs has breaking changes to Error + console.error('log4js.logger - defaultParseCallStack error', err); // eslint-disable-line no-console } return null; } diff --git a/test/tap/logger-test.js b/test/tap/logger-test.js index 476f965..1a16710 100644 --- a/test/tap/logger-test.js +++ b/test/tap/logger-test.js @@ -5,6 +5,7 @@ const callsites = require("callsites"); const levels = require("../../lib/levels"); const events = []; +const messages = []; const Logger = sandbox.require("../../lib/logger", { requires: { "./levels": levels, @@ -16,6 +17,14 @@ const Logger = sandbox.require("../../lib/logger", { events.push(evt); } } + }, + globals: { + console: { + ...console, + error(msg) { + messages.push(msg); + } + } } }); @@ -219,6 +228,23 @@ test("../../lib/logger", batch => { } ); + batch.test("parseCallStack function coverage", t => { + const logger = new Logger("stack"); + logger.useCallStack = true; + + let results; + + results = logger.parseCallStack(new Error()); + t.ok(results); + t.equal(messages.length, 0, "should not have error"); + + results = logger.parseCallStack(""); + t.notOk(results); + t.equal(messages.length, 1, "should have error"); + + t.end(); + }); + batch.test("should correctly change the parseCallStack function", t => { const logger = new Logger("stack"); const parseFunction = function() {