Created
June 16, 2016 00:02
-
-
Save rmcvey/6da37a3b85f216ff5166ecf040b3ef68 to your computer and use it in GitHub Desktop.
Example of ExpressJS based server-side logging of client-side errors
This file contains hidden or 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
// need to load https://cdnjs.cloudflare.com/ajax/libs/UAParser.js/0.7.10/ua-parser.min.js or something similar | |
// to be used with window.onerror (e.g. window.onerror = logError); | |
const logError = (message, file, line, col, error) => { | |
try { | |
var parser = new UAParser(); | |
var userInfo = parser.getResult(); | |
var http = new XMLHttpRequest(); | |
var params = JSON.stringify({ | |
userInfo: { | |
os: `${userInfo.os.name} ${userInfo.os.version}`, | |
browser: `${userInfo.browser.name} ${userInfo.browser.version}`, | |
device: userInfo.device.name ? `${userInfo.device.name} ${userInfo.device.version}` : 'unknown', | |
}, | |
message, | |
file, | |
line, | |
col, | |
error | |
}); | |
http.open("POST", "logservice", true); | |
http.setRequestHeader("Content-type", "application/json;charset=UTF-8"); | |
http.send(params); | |
} catch (e) { | |
console.error("EPIC FAILURE, can't even log error: ", error); | |
} | |
}; |
This file contains hidden or 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
app.post('/clienterrors', (req, res) => { | |
var fs = require('fs'); | |
var body = req.body; | |
var logStream = fs.createWriteStream('/var/log/mas-react/client.log', {'flags': 'a'}); | |
req.body.time = new Date(); | |
logStream.write(JSON.stringify(body)); | |
logStream.end('\n'); | |
res.json({ logged: true }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment