Permalink
Browse files

logger.data

  • Loading branch information...
1 parent bac2016 commit f78aa1160d7e6c4cb81e9d10989861a2860b78e0 Paul Baumgart committed Aug 25, 2012
Showing with 87 additions and 35 deletions.
  1. +8 −7 filters/logger/index.js
  2. +79 −28 filters/logger/logger.js
View
@@ -4,11 +4,12 @@ var _ = require('underscore');
var LOG_LEVELS = {
'off': 0,
- 'error': 1,
- 'warn': 2,
- 'info': 3,
- 'debug': 4,
- 'trace': 5
+ 'data': 1,
+ 'error': 2,
+ 'warn': 3,
+ 'info': 4,
+ 'debug': 5,
+ 'trace': 6
};
function init(options) {
@@ -48,7 +49,7 @@ function init(options) {
src.substring(endPos);
replacementOffset += '/* */'.length;
} else {
- var stringToInject = '"' + logLevel.charAt(0) + '[" + __shortfilename + "]:"';
+ var stringToInject = '"' + logLevel + '[" + __shortfilename + "]: "';
if (this[2].length > 0) {
stringToInject += ', ';
}
@@ -72,7 +73,7 @@ function init(options) {
var loggerType = JSON.stringify(options.loggerUrl ? 'url' : 'console');
var loggerArg = JSON.stringify(options.loggerUrl || '');
return 'var __shortfilename = String(__filename).split("/");\n' +
- '__shortfilename = __shortfilename.slice(__shortfilename.length - 3).join("/");\n' +
+ '__shortfilename = __shortfilename.slice(__shortfilename.length - 2).join("/");\n' +
'var logger = new (require(' + JSON.stringify(loggerFile) + ')(' + loggerType + '))(' + loggerArg + ');\n' +
src;
}
View
@@ -1,51 +1,97 @@
-var isInternetExplorer = (navigator.appName === 'Microsoft Internet Explorer');
+var MAX_DATA_LENGTH = 975;
+
+function _validateDataCall(args) {
+ if (args.length !== 2) {
+ throw new Error('logger.data takes exactly 2 args');
+ } else if (typeof args[0] !== 'string' || args[0].length === 0) {
+ throw new Error('first param to logger.data must be a non-zero-length namespace string');
+ }
+}
function UrlLogger(url) {
- this._url = url || '';
- this.error = this.log;
- this.warn = this.log;
- this.info = this.log;
- this.debug = this.log;
- this.trace = this.log;
- this.log = void 0;
+ if (!url) {
+ throw new Error('url required');
+ }
+
+ this._url = url;
+ this.error = this._log;
+ this.warn = this._log;
+ this.info = this._log;
+ this.debug = this._log;
+ this.trace = this._log;
}
UrlLogger.prototype = {
- log: function() {
+ _send: function(log, type) {
+ if (typeof document !== 'undefined' &&
+ document.createElement &&
+ document.documentElement) { // don't try to send data if we don't have a DOM
+ var body = document.body || document.documentElement;
+
+ var img = document.createElement('img');
+ img.onload = img.onerror = function() {
+ body.removeChild(img);
+ img = null;
+ };
+ img.src = this._url + (this._url.indexOf('?') >= 0 ? '&' : '?') +
+ 'log=' + encodeURIComponent(log) +
+ '&type=' + encodeURIComponent(type);
+
+ body.appendChild(img);
+ }
+ },
+
+ _log: function() {
var args = Array.prototype.slice.call(arguments);
+ var name = JSON.stringify(args.shift().replace(/:\s*$/, ''));
for (var i = 0, len = args.length; i < len; i++) {
var arg = args[i];
if (arg instanceof Error) {
if (arg.stack) {
- if (isInternetExplorer) { // avoid hitting URL length limits in IE
- args[i] = arg.stack.split(/\s*\n\s*/)[1];
- } else {
- args[i] = arg.stack;
- }
+ args[i] = arg.stack;
} else {
args[i] = arg.message;
}
}
}
- this._sendData(args);
- },
+ var encoded = JSON.stringify(args);
+ while (encoded.length > MAX_DATA_LENGTH) {
+ var longestLength = 0;
+ var longestIndex = -1;
+ for (var i = 0, len = args.length; i < len; i++) {
+ var encodedArg = JSON.stringify(args[i]);
+ if (encodedArg.length > longestLength) {
+ longestLength = encodedArg.length;
+ longestIndex = i;
+ }
+ }
- _sendData: function(data) {
- if (typeof document !== 'undefined' &&
- document.createElement &&
- document.documentElement) { // don't try to send data if we don't have a DOM
- var body = document.body || document.documentElement;
+ // try to trim down longest value to make it fit
+ var longestArg = args[longestIndex];
+ if (typeof longestArg === 'string') {
+ longestArg = longestArg.split('\n');
+ longestArg = longestArg.slice(0, Math.round(longestArg.length / 2)).join('\n') + '\n(truncated)';
+ } else if (typeof longestArg.slice === 'function') {
+ longestArg = longestArg.slice(0, Math.round(longestArg.length / 2));
+ longestArg.push('(truncated)');
+ }
- var img = document.createElement('img');
- img.onload = img.onerror = function() {
- body.removeChild(img);
- img = null;
- };
- img.src = this._url + encodeURIComponent(JSON.stringify(data));
+ if (longestArg.length < args[longestIndex].length) {
+ args[longestIndex] = longestArg;
+ } else {
+ args[longestIndex] = '(truncated)';
+ }
- body.appendChild(img);
+ encoded = JSON.stringify(args);
}
+
+ this._send(encoded, name);
+ },
+
+ data: function(namespace, data) {
+ _validateDataCall(Array.prototype.slice.call(arguments, 1));
+ this._log.apply(this, arguments);
}
}
@@ -118,6 +164,11 @@ try {
};
}
+ConsoleLogger.prototype.data = function() {
+ _validateDataCall(Array.prototype.slice.call(arguments, 1));
+ this.debug.apply(this, arguments);
+}
+
function getLogger(type) {
switch (type && type.toLowerCase()) {
case 'console':

0 comments on commit f78aa11

Please sign in to comment.