|
1 | 1 | /* |
2 | 2 | * Title: jQuery Client Side Logging Plugin |
3 | 3 | * Author: Rémy Bach |
4 | | - * Version: 0.0.2 |
| 4 | + * Version: 0.1.0 |
5 | 5 | * License: http://remybach.mit-license.org |
6 | 6 | * Url: http://github.com/remybach/jQuery.clientSideLogging |
7 | 7 | * Description: |
|
25 | 25 | } |
26 | 26 | }; |
27 | 27 |
|
| 28 | + /** |
| 29 | + * Initializing with custom options. Not strictly necessary, but recommended. |
| 30 | + * @param options The custom options. |
| 31 | + */ |
28 | 32 | $.clientSideLogging = function(options) { |
29 | 33 | $.extend(defaults, options || {}); |
30 | 34 | }; |
31 | 35 |
|
| 36 | + /** |
| 37 | + * The function that will send error logs to the server. Also logs to the console using console.error() (if available and requested by the user) |
| 38 | + * @param what What you want to be logged (String, or JSON object) |
| 39 | + */ |
32 | 40 | $.error = function(what) { |
33 | 41 | if (defaults.log_level >= 1) { |
34 | 42 | _send(defaults.error_url, what); |
|
37 | 45 | if(window.console&&window.console.error&&defaults.use_console)console.error(what); |
38 | 46 | }; |
39 | 47 |
|
| 48 | + /** |
| 49 | + * The function that will send info logs to the server. Also logs to the console using console.info() (if available and requested by the user) |
| 50 | + * @param what What you want to be logged (String, or JSON object) |
| 51 | + */ |
40 | 52 | $.info = function(what) { |
41 | 53 | if (defaults.log_level >= 3) { |
42 | 54 | _send(defaults.info_url, what); |
|
45 | 57 | if(window.console&&window.console.info&&defaults.use_console)console.info(what); |
46 | 58 | }; |
47 | 59 |
|
| 60 | + /** |
| 61 | + * The function that will send standard logs to the server. Also logs to the console using console.log() (if available and requested by the user) |
| 62 | + * @param what What you want to be logged (String, or JSON object) |
| 63 | + */ |
48 | 64 | $.log = function(what) { |
49 | 65 | if (defaults.log_level >= 2) { |
50 | 66 | _send(defaults.log_url, what); |
|
53 | 69 | if(window.console&&window.console.log&&defaults.use_console)console.log(what); |
54 | 70 | }; |
55 | 71 |
|
| 72 | + // Log errors whenever there's a generic js error on the page. |
56 | 73 | window.onerror = function(message, file, line) { |
57 | 74 | if (defaults.native_error) { |
58 | 75 | _send(defaults.error_url, { |
|
64 | 81 | }; |
65 | 82 |
|
66 | 83 | /*===== Private Functions =====*/ |
| 84 | + /** |
| 85 | + * Send the log information to the server. |
| 86 | + * @param url The url to submit the information to. |
| 87 | + * @param what The information to be logged. |
| 88 | + */ |
67 | 89 | _send = function(url, what) { |
| 90 | + // If the url already has a ? in it. |
68 | 91 | if (url.match(/\?.+$/)) { |
69 | 92 | url += '&'; |
70 | 93 | } else { |
|
73 | 96 |
|
74 | 97 | format = 'text'; |
75 | 98 | if (typeof what === 'object') { |
76 | | - // Let's grab the additional logging info before we send this off. |
77 | 99 | format = 'json'; |
78 | 100 |
|
| 101 | + // Let's grab the additional logging info before we send this off. |
79 | 102 | $.extend(what, _buildClientInfo()); |
80 | 103 | what = JSON.stringify(what); |
81 | 104 | } else { |
|
89 | 112 | $.post(url); |
90 | 113 | }; |
91 | 114 |
|
| 115 | + /** |
| 116 | + * Build up an object containing the requested information about the client (as specified in defaults). |
| 117 | + * @return _info The object containing the requested information. |
| 118 | + */ |
92 | 119 | _buildClientInfo = function() { |
93 | 120 | var _info = {}; |
94 | 121 |
|
|
108 | 135 | return _info; |
109 | 136 | }; |
110 | 137 |
|
111 | | - // Fallback for older browsers that don't implement JSON.stringify |
| 138 | + /** |
| 139 | + * Fallback for older browsers that don't implement JSON.stringify |
| 140 | + * @param obj The JSON object to turn into a string. |
| 141 | + * @return A string representation of the JSON object. |
| 142 | + */ |
112 | 143 | JSON.stringify = JSON.stringify || function (obj) { |
113 | 144 | var t = typeof (obj); |
114 | 145 | if (t != "object" || obj === null) { |
|
0 commit comments