Browse files

greatly improved logger

  • Loading branch information...
1 parent b68a18e commit 43a3073fe6c2d0c4a074a4dfc240af70d4f51f18 @paulbaumgart paulbaumgart committed Aug 14, 2012
Showing with 86 additions and 65 deletions.
  1. +24 −10 filters/logger/index.js
  2. +59 −53 filters/logger/logger.js
  3. +1 −1 package.json
  4. +2 −1 test/fixtures/abc.js
View
34 filters/logger/index.js
@@ -3,10 +3,10 @@ var uglify = require('uglify-js').uglify;
var _ = require('underscore');
var LOG_LEVELS = {
- 'off': 0,
+ 'off': 0,
'error': 1,
- 'warn': 2,
- 'info': 3,
+ 'warn': 2,
+ 'info': 3,
'debug': 4,
'trace': 5
};
@@ -38,31 +38,45 @@ function init(options) {
this[1][1][0] === 'name' &&
this[1][1][1] === 'logger') {
var logLevel = this[1][2];
+ var token = this[0];
+ var startPos = token.start.pos + replacementOffset;
+ var endPos = token.end.endpos + replacementOffset;
+
if (allowedLogLevelsSet[logLevel] !== true) {
- var token = this[0];
- var startPos = token.start.pos + replacementOffset;
- var endPos = token.end.endpos + replacementOffset;
src = src.substring(0, startPos) + '/* ' +
src.substring(startPos, endPos) + ' */' +
src.substring(endPos);
replacementOffset += '/* */'.length;
+ } else {
+ var stringToInject = '"' + logLevel.charAt(0) + '[" + __shortfilename + "]:"';
+ if (this[2].length > 0) {
+ stringToInject += ', ';
+ }
+ var logStatement = src.substring(startPos, endPos);
+ var logFnRegex = new RegExp('(^\\s*logger\\s*\\.\\s*' + logLevel + '\\s*\\(\s*)');
+ logStatement = logStatement.replace(logFnRegex, '$1' + stringToInject);
+ src = src.substring(0, startPos) +
+ logStatement +
+ src.substring(endPos);
+ replacementOffset += stringToInject.length;
}
}
}
}, function() {
return walker.walk(parser.parse(src, false, true));
});
- if (logLevel === "off") {
+ if (logLevel === 'off') {
return src;
} else {
- return 'var logger = new (require(' + JSON.stringify(loggerFile) + '))(__filename);\n' + src;
+ return 'var __shortfilename = String(__filename).split("/");\n\
+ __shortfilename = __shortfilename.slice(__shortfilename.length - 3).join("/");\n\
+ var logger = new (require(' + JSON.stringify(loggerFile) + '));\n' + src;
}
},
requires: logLevel === "off" ? [] : [ loggerFile ], // must be absolute paths
- lineDelta: logLevel === "off" ? 0 : 1
+ lineDelta: logLevel === "off" ? 0 : 3
}
-
}
exports.init = init;
View
112 filters/logger/logger.js
@@ -1,61 +1,67 @@
-function Logger(name) {
- var nameParts = String(name).split('/');
- this._name = nameParts.slice(nameParts.length - 3).join('/');
+function Logger() {
+ if (this.log) {
+ this.trace = this.log;
+ this.log = void 0;
+ }
}
-var logMethods = ['error', 'warn', 'info', 'debug', 'trace'];
-
-for (var index = 0, len = logMethods.length; index < len; ++index) {
- (function(index, funcName) {
- Logger.prototype[funcName] = function() {
- var message = [funcName[0], '[', this._name, ']'].join('');
- var args = Array.prototype.slice.call(arguments);
- var stack_traces = [];
-
- // logger.trace -> console.log
- funcName = funcName === 'trace' ? 'log' : funcName;
-
- if (!console) {
- return;
- }
-
- if (typeof args[0] === 'string') {
- message += ': ' + args.shift();
- }
-
- for (var i = 0, len = args.length; i < len; ++i) {
- if (args[i] && args[i].stack) {
- stack_traces.push(args[i].stack);
- }
- }
-
- if (console.firebug) {
- args.unshift(message);
- console[funcName].apply(self, args);
- } else {
- if (args.length <= 0) {
- console[funcName] ? console[funcName](message) :
- console.log(message);
- } else if (args.length === 1) {
- console[funcName] ? console[funcName](message, args[0]) :
- console.log(message, args[0]);
- } else {
- console[funcName] ? console[funcName](message, args) :
- console.log(message, args);
- }
- }
-
- var len = stack_traces.length;
-
- if (len > 0) {
- console.log('Listing exception stack traces individually:');
- for (var i = 0; i < len; ++i) {
- console.log(stack_traces[i]); // why? because in Google Chrome,
- // this will make clickable links
+if (typeof console !== 'undefined' && typeof console.log === 'function') {
+ Logger.prototype = console;
+} else { // for the dreaded IE, which doesn't allow console.log.apply
+ function getLogFunction(name) {
+ return function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
+ if (typeof console !== 'undefined') {
+ switch (arguments.length) {
+ case 0:
+ console[name]();
+ break;
+ case 1:
+ console[name](arg0);
+ break;
+ case 2:
+ console[name](arg0, arg1);
+ break;
+ case 3:
+ console[name](arg0, arg1, arg2);
+ break
+ case 4:
+ console[name](arg0, arg1, arg2, arg3);
+ break;
+ case 5:
+ console[name](arg0, arg1, arg2, arg3, arg4);
+ break;
+ case 6:
+ console[name](arg0, arg1, arg2, arg3, arg4, arg5);
+ break;
+ case 7:
+ console[name](arg0, arg1, arg2, arg3, arg4, arg5, arg6);
+ break;
+ case 7:
+ console[name](arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+ break;
+ case 8:
+ console[name](arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
+ break;
+ case 9:
+ console[name](arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
+ break;
+ case 10:
+ console[name](arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
+ break;
+ default:
+ break;
}
}
};
- })(index, logMethods[index]);
+ }
+
+ Logger.prototype = {
+ error: getLogFunction('error'),
+ warn: getLogFunction('warn'),
+ info: getLogFunction('info'),
+ debug: getLogFunction('debug'),
+ trace: getLogFunction('log')
+ };
}
module.exports = Logger;
View
2 package.json
@@ -3,7 +3,7 @@
"name": "jsbundle",
"description": "Simple, clean, and automatic bundling of your Node modules and packages for use in the browser.",
"keywords": "browser require bundle module package static-analysis",
- "version": "0.13.2",
+ "version": "0.13.3",
"repository": {
"type": "git",
"url": "git://github.com/proxv/jsbundle.git"
View
3 test/fixtures/abc.js
@@ -1,6 +1,7 @@
#!/usr/bin/env node
-logger.warn(
+logger.
+ warn(
'this code executed!'
);

0 comments on commit 43a3073

Please sign in to comment.