From 8db6438fb59acee7bcab7664e3cbb54aec65ec9c Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Thu, 23 May 2013 11:56:08 -0700 Subject: [PATCH] Log methods add file name and line number. --- packages/logging/logging.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/logging/logging.js b/packages/logging/logging.js index 776db798f3..80ecf02fc3 100644 --- a/packages/logging/logging.js +++ b/packages/logging/logging.js @@ -56,6 +56,32 @@ var logInBrowser = function (obj) { } }; +var getCallerDetails = function () { + var e = new Error(); + // now magic will happen: get line number from callstack + var lines = e.stack.split('\n'); + var line = lines[2]; + var index = 0; + + // Pick the first line outside logging package + while (line.indexOf('/packages/logging.js') !== -1) + line = lines[++index]; + + var details = {}; + details.line = line.split(':')[1]; + + // line can be in two formats depending on function description availability: + // 0) at functionNumber (/filePath/file.js:line:position) + // 1) at /filePath/file.js:line:position + details.fileName = line.indexOf('(') === -1 ? + line.split('at ')[1] : + line.split('(')[1]; + details.fileName = details.fileName.split(':')[0]; // get rid of line number + details.fileName = details.fileName.split('/').slice(-1)[0]; + + return details; +}; + _.each(['debug', 'info', 'warn', 'error'], function (level) { // @param arg {String|Object} Log[level] = function (arg) { @@ -65,7 +91,8 @@ _.each(['debug', 'info', 'warn', 'error'], function (level) { intercepted = true; } - var obj = (typeof arg === 'string') ? {message: arg} : arg; + var obj = (typeof arg === 'string') ? + _.extend(getCallerDetails(), {message: arg}): arg; _.each(RESTRICTED_KEYS, function (key) { if (obj[key])