Last active
August 29, 2015 14:01
-
-
Save pgaertig/64ebfb41b25fa4a15eb1 to your computer and use it in GitHub Desktop.
Records log messages and sends report to server in case of any error or warning.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TopLogger(url, sessionId) { | |
var self = this; | |
var sid = sessionId; | |
var counter = 0; | |
var entries = []; | |
function consoleLog(type, msg) { | |
if(consoleChain && consoleChain[type]) { | |
try { | |
consoleChain[type].call(consoleChain.oldConsole, msg); | |
} catch(e) {} | |
} | |
} | |
function fillTemplate(subject, obj) { | |
return subject.replace(/{([^{}]*)}/g, | |
function (a, b) { | |
return params[b]; | |
} | |
); | |
} | |
function internalLog(level, sid, ar) { | |
reloadSid(); | |
var args = Array.prototype.slice.call(ar); | |
var message = args.shift(); | |
// message in a form ["test {1} and {2}", "par1", 123] | |
if(typeof message === 'array' && message.length > 0) { | |
message = message[0].template(message); | |
} else { | |
message = JSON.stringify(message); | |
} | |
if(args.length > 0) { | |
for(var i = 0; i < args.length; i++) { | |
message += ", " + JSON.stringify(args[i]); | |
} | |
} | |
var entry = {level:level, sid:sid, counter: ++counter, message:message}; | |
entries.push(entry); | |
return ""+level+" "+sid+":"+entry.counter+" "+message; | |
} | |
function flush() { | |
if(entries.length > 0) { | |
var local_ent = entries; | |
entries = []; | |
try { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("POST", url, true); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.send(JSON.stringify(local_ent)); | |
} catch(xhrerr) { | |
//nothing can do here | |
consoleLog('error', "Logger XHR failed"); | |
} | |
} | |
} | |
function error() { | |
try { | |
consoleLog('error', internalLog('E', sid, arguments)); | |
flush(); | |
} catch (e) { | |
//shit | |
} | |
} | |
function log() { | |
try { | |
consoleLog('log', internalLog('D', sid, arguments)); | |
} catch (e) { | |
//shit | |
} | |
} | |
function warn() { | |
try { | |
consoleLog('warn', internalLog('W', sid, arguments)); | |
flush(); | |
} catch (e) { | |
//shit | |
} | |
} | |
function info() { | |
try { | |
consoleLog('info', internalLog('I', sid, arguments)); | |
} catch (e) { | |
//shit | |
} | |
} | |
this.e = error; | |
this.w = warn; | |
this.i = info; | |
this.d = log; | |
this.reset = function() { | |
counter = 0; | |
entries = []; | |
}; | |
function onErrorHandler(old, args) { | |
error(['ERR00:{1} ({2}:{3}:{4})',args[0],args[1],args[2],args[3], args[4]]); | |
if (old) { | |
old.apply(window, args); | |
} | |
} | |
function reloadSid() { | |
if(sid == null) { | |
try { | |
sid = window.localStorage.getItem("user.email"); | |
if(sid != null) { | |
var el = entries.length; | |
for (var i = 0; i < el; i++) { | |
entries[i].sid = sid; | |
} | |
} | |
} catch (e) { | |
//local storage failed | |
} | |
} | |
} | |
//override onerror | |
var oldOnError = window.onerror; | |
window.onerror = function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
onErrorHandler(oldOnError, args); | |
}; | |
//override console | |
var consoleChain = null; | |
if(window.console) { | |
consoleChain = { | |
info: window.console.info, | |
error: window.console.error, | |
warn: window.console.warn, | |
log: window.console.log, | |
oldConsole: window.console | |
}; | |
window.console.error = function(msg) { self.e(msg); }; | |
window.console.warn = function(msg) { self.w(msg); }; | |
window.console.info = function(msg) { self.i(msg); }; | |
window.console.log = function(msg) { self.d(msg); }; | |
} else { | |
} | |
log("KPLogger: " + window.location.pathname + ", " + navigator.userAgent); | |
} | |
if(typeof LOG === 'undefined') { | |
var LOG = new TopLogger('/errors'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment