From 482f3964819ca430b2aafba42f865f7419d27d10 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Thu, 18 May 2017 15:44:07 -0300 Subject: [PATCH] Allows to disable console logging --- README.md | 10 ++++++++++ tattletale.js | 9 ++++++--- tattletale.min.js | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 61b531c..f70e259 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,13 @@ var tattletale = new Tattletale('/log', { token: window.xsrft }); ``` + +A third argument is allowed for general configuration, such as disabling console logging: + +```javascript +var tattletale = new Tattletale('/log', {}, { + consoleLoggingDisabled: true +}); +``` + + diff --git a/tattletale.js b/tattletale.js index 8ac40c5..48218ea 100644 --- a/tattletale.js +++ b/tattletale.js @@ -12,8 +12,10 @@ * @param {string} url The XHR path used to send the console logs. * @param {Object} static_request_data Static data to send along with each * request, such as a cross-site request forgery token. + * @param {Object} options Customizable params, not attached to the + * request, such as disabling console logging. */ - function Tattletale(url, static_request_data) { + function Tattletale(url, static_request_data, options) { var self = this; if (typeof url !== 'string') { @@ -23,6 +25,7 @@ self.url = url; self.request_data = (typeof static_request_data === 'object') ? static_request_data : {}; self.logs = []; + self.options = options || {}; } // Internal Methods __________________________________________________________ @@ -118,7 +121,7 @@ logs.push(arguments_string); - if (typeof window.console !== 'undefined' && typeof window.console.log === 'function') { + if (!this.options.consoleLoggingDisabled && typeof window.console !== 'undefined' && typeof window.console.log === 'function') { window.console.log.apply(window.console, arguments); } }, @@ -165,4 +168,4 @@ window.Tattletale = Tattletale; -})(window); \ No newline at end of file +})(window); diff --git a/tattletale.min.js b/tattletale.min.js index 74c13a2..7f6f2ba 100644 --- a/tattletale.min.js +++ b/tattletale.min.js @@ -6,4 +6,4 @@ * @version 0.1.0 * @license Apache 2.0 */ -!function(a){"use strict";function b(a,b){var c=this;if("string"!=typeof a)throw new Error("Tattletale instances require a URL for log submission over XHR.");c.url=a,c.request_data="object"==typeof b?b:{},c.logs=[]}function c(a,b){var d,e,f,g,h,i=[],j={};for(d in a)if(a.hasOwnProperty(d)){switch(e=a[d],b&&(d=b+"["+d+"]"),Object.prototype.toString.call(e)){case"[object Object]":f=c(e,d);break;case"[object Array]":for(g=0,h=e.length;h>g;g++)j[g]=e[g];f=c(j,d);break;default:f=d+"="+encodeURIComponent(e)}null!==e&&i.push(f)}return i.join("&")}b.prototype={log:function(){var b,c,d,e,f=this.logs,g="",h=arguments.length;if(0!==h){for(b=[].slice.call(arguments),c=0;h>c;c++)d=b[c],e=typeof d,g+="string"===e||"number"===e||"boolean"===e?d:"[Object object]",h-1>c&&(g+=", ");f.length>100&&f.splice(0,1),f.push(g),"undefined"!=typeof a.console&&"function"==typeof a.console.log&&a.console.log.apply(a.console,arguments)}},empty:function(){this.logs=[]},send:function(a){var b=this,d=new XMLHttpRequest;b.request_data.console_logs=b.logs,b.request_data=c(b.request_data),d.onreadystatechange=function(){4===d.readyState&&(b.empty(),"function"==typeof a&&a())},d.open("POST",b.url,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.setRequestHeader("Content-type","application/x-www-form-urlencoded"),d.send(b.request_data)}},a.Tattletale=b}(window); \ No newline at end of file +!function(a){"use strict";function b(a,b,c){var d=this;if("string"!=typeof a)throw new Error("Tattletale instances require a URL for log submission over XHR.");d.url=a,d.request_data="object"==typeof b?b:{},d.logs=[],d.options=c||{}}function c(a,b){var d,e,f,g,h,i=[],j={};for(d in a)if(a.hasOwnProperty(d)){switch(e=a[d],b&&(d=b+"["+d+"]"),Object.prototype.toString.call(e)){case"[object Object]":f=c(e,d);break;case"[object Array]":for(g=0,h=e.length;h>g;g++)j[g]=e[g];f=c(j,d);break;default:f=d+"="+encodeURIComponent(e)}null!==e&&i.push(f)}return i.join("&")}b.prototype={log:function(){var b,c,d,e,f=this.logs,g="",h=arguments.length;if(0!==h){for(b=[].slice.call(arguments),c=0;h>c;c++)d=b[c],e=typeof d,g+="string"===e||"number"===e||"boolean"===e?d:"[Object object]",h-1>c&&(g+=", ");f.length>100&&f.splice(0,1),f.push(g),this.options.consoleLoggingDisabled||"undefined"==typeof a.console||"function"!=typeof a.console.log||a.console.log.apply(a.console,arguments)}},empty:function(){this.logs=[]},send:function(a){var b=this,d=new XMLHttpRequest,e=null;b.request_data.console_logs=b.logs,e=c(b.request_data),b.empty(),d.onreadystatechange=function(){4===d.readyState&&"function"==typeof a&&a()},d.open("POST",b.url,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.setRequestHeader("Content-type","application/x-www-form-urlencoded"),d.send(e)}},a.Tattletale=b}(window); \ No newline at end of file