Skip to content

Commit 2c6efe8

Browse files
committed
Added no-jquery version WIP
1 parent b9fdf9e commit 2c6efe8

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

clientSideLogging.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Title: jQuery Client Side Logging Plugin
3+
* Author: Rémy Bach
4+
* Version: 1.1.2
5+
* License: http://remybach.mit-license.org
6+
* Url: http://github.com/remybach/jQuery.clientSideLogging
7+
* Description:
8+
* This plugin allows you to store front end log/info/error messages on the server (note: you will need to have something set up on your server to handle the actual logging).
9+
* The idea was born after reading the following article: http://openmymind.net/2012/4/4/You-Really-Should-Log-Client-Side-Error/
10+
*/
11+
12+
13+
(function($) {
14+
15+
var defaults = {
16+
error_url: '/log/?type=error', // The url to which errors logs are sent
17+
query_var: 'msg', // The variable to send the log message through as.
18+
client_info: {
19+
location: true, // The url to the page on which the error occurred.
20+
screen_size: true, // The size of the user's screen (different to the window size because the window might not be maximized)
21+
user_agent: true, // The user agent string.
22+
window_size: true // The window size.
23+
}
24+
}
25+
26+
// Polyfill older browsers
27+
if (!window.console) {
28+
console = {};
29+
}
30+
31+
var JSON;
32+
if (!JSON) {
33+
JSON = {};
34+
}
35+
JSON.stringify = JSON.stringify ||
36+
function(obj) {
37+
var t = typeof(obj);
38+
if (t != "object" || obj === null) {
39+
// simple data type
40+
if (t == "string") obj = '"' + obj + '"';
41+
return String(obj);
42+
} else {
43+
// recurse array or object
44+
var n, v, json = [],
45+
arr = (obj && obj.constructor == Array);
46+
for (n in obj) {
47+
v = obj[n];
48+
t = typeof(v);
49+
if (t == "string") v = '"' + v + '"';
50+
else if (t == "object" && v !== null) v = JSON.stringify(v);
51+
json.push((arr ? "" : '"' + n + '":') + String(v));
52+
}
53+
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
54+
}
55+
};
56+
57+
_merge = function(obj1, obj2){
58+
var obj3 = {};
59+
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
60+
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
61+
return obj3;
62+
}
63+
64+
_buildClientInfo = function() {
65+
var _info = {};
66+
67+
if (defaults.client_info.user_agent) {
68+
_info.user_agent = navigator.userAgent;
69+
}
70+
if (defaults.client_info.window_size) {
71+
_info.window_size = $(window).width() + ' x ' + $(window).height();
72+
}
73+
if (defaults.client_info.screen_size) {
74+
_info.screen_size = window.screen.availWidth + ' x ' + window.screen.availHeight;
75+
}
76+
if (defaults.client_info.location) {
77+
_info.location = window.location.href;
78+
}
79+
return _info;
80+
};
81+
82+
// Send the log information to the server.
83+
_send = function(url, what) {
84+
url += url.match(/\?.+$/) ? '&' : '?';
85+
86+
if (typeof what === 'object') {
87+
// Let's grab the additional logging info before we send this off.
88+
89+
_merge(what, _buildClientInfo());
90+
91+
_data = {};
92+
_data[defaults.query_var] = JSON.stringify(what);
93+
94+
$.ajax({
95+
type: 'POST',
96+
url: url + 'format=json',
97+
data: _data
98+
});
99+
} else {
100+
$.post(url + 'format=text&' + defaults.query_var + '=' + what);
101+
}
102+
};
103+
104+
// Log errors whenever there's a generic js error on the page.
105+
window.onerror = function(message, file, line) {
106+
_send(defaults.error_url, {
107+
message: message,
108+
file: file,
109+
line: line
110+
});
111+
};
112+
113+
})(jQuery);

0 commit comments

Comments
 (0)