jam-cloud/web/app/assets/javascripts/AAA_Log.js

98 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2014-03-11 04:52:39 +00:00
(function (context, $) {
"use strict";
/*
internal logger with no-ops when console is missing.
*/
context.JK = context.JK || {};
var console_methods = [
'log', 'debug', 'info', 'warn', 'error', 'assert',
'clear', 'dir', 'dirxml', 'trace', 'group',
'groupCollapsed', 'groupEnd', 'time', 'timeEnd',
'timeStamp', 'profile', 'profileEnd', 'count',
'exception', 'table'
];
2014-04-30 03:01:28 +00:00
var log_methods = {
'log':null, 'debug':null, 'info':null, 'warn':null, 'error':null, 'assert':null, 'trace':null, 'exception':null
}
2014-05-19 13:46:03 +00:00
var backend_methods = {
"log" : 4,
"debug" : 4,
"info" : 3,
"warn" : 2,
"error" : 1
}
2014-04-30 03:01:28 +00:00
var logCache = [];
if ('undefined' === typeof(context.console)) {
context.console = {};
$.each(console_methods, function(index, value) {
context.console[value] = $.noop;
});
}
2014-03-11 04:52:39 +00:00
if (!console.debug) {
console.log("No console.debug found - defining...");
context.console.debug = function() { console.log(arguments); }
}
2014-05-19 13:46:03 +00:00
console.proxy_logs_to_backend = false;
2014-04-30 03:01:28 +00:00
// http://tobyho.com/2012/07/27/taking-over-console-log/
function takeOverConsole(){
var console = window.console
2014-05-19 13:46:03 +00:00
if (!console) return;
var i = null;
2014-04-30 03:01:28 +00:00
function intercept(method){
var original = console[method]
console[method] = function(){
2014-05-19 13:46:03 +00:00
var logAsString = [];
for(i in arguments) {
var arg = arguments[i];
try {
logAsString.push(JSON.stringify(arg));
}
catch(e) {
logAsString.push("unable to parse node: " + e.toString());
}
}
logCache.push([method].concat(logAsString));
2014-04-30 03:01:28 +00:00
if(logCache.length > 50) {
// keep the cache size 50 or lower
logCache.shift();
2014-04-30 03:01:28 +00:00
}
if (original.apply){
// Do this for normal browsers
original.apply(console, arguments)
}else{
// Do this for IE
var message = Array.prototype.slice.apply(arguments).join(' ')
original(message)
}
2014-05-19 13:46:03 +00:00
if(console.proxy_logs_to_backend && context.jamClient) {
var backendLevel = backend_methods[method];
if(backendLevel) {
context.jamClient.log(backendLevel, logAsString.join(', '));
}
}
2014-04-30 03:01:28 +00:00
}
}
2014-05-19 13:46:03 +00:00
var methods = ['log', 'warn', 'error', 'debug', 'info']
2014-04-30 03:01:28 +00:00
for (var i = 0; i < methods.length; i++)
intercept(methods[i])
}
takeOverConsole();
2014-04-30 03:01:28 +00:00
context.JK.logger = context.console;
context.JK.logger.logCache = logCache;
})(window, jQuery);