Created
May 28, 2015 12:48
-
-
Save totya24/6d83848faa0727ee30a9 to your computer and use it in GitHub Desktop.
log js errors on server side
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
js: place in the head of html | |
<script type="text/javascript"> | |
window.onerror = function (errorMsg, url, lineNumber, column, errorObj) { | |
if (errorMsg.indexOf('Script error.') > -1) { | |
return; | |
} | |
var text = 'Err: ' + errorMsg + ' | Script: ' + url + ' | Ln: ' + lineNumber + (column ? ' | Clmn: ' + column : '') + (errorObj ? ' | Stacktrc: ' + errorObj : ''); | |
var r = new XMLHttpRequest(); | |
var params = "d=" + encodeURIComponent(text); | |
r.open("POST", "jslogger.php", true); | |
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
r.setRequestHeader("Content-length", params.length); | |
r.setRequestHeader("Connection", "close"); | |
r.send(params); | |
} | |
</script> | |
-------------------- | |
jslogger.php: | |
<?php | |
if($d = addslashes(strip_tags(trim($_POST['d'])))){ | |
file_put_contents('jserror.log',date('y-m-d H:i : ').$d.' | UA: '.$_SERVER['HTTP_USER_AGENT']."\n",FILE_APPEND); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment