-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlumberjack.js
161 lines (137 loc) · 4.56 KB
/
lumberjack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Lumberjack.js
*
* Client side logger that can send logs to remote servers
*
* TODO:
* - Group
* - Profile?
*/
function Lumberjack(configs){
configs = configs || {};
/**
* Configurations
* @type {boolean|*}
*/
configs.passthrough = configs.passthrough || true;
configs.history_length = configs.history_length || 0;
configs.remote_logging = configs.remote_logging || false;
configs.remote_url = configs.remote_url || undefined;
/**
* Private Variables
* @type {string[]}
*/
var history = []; //record history
var history_index = 0;//Keeps track of how many items are in the history. This should only be incremented. Modulus should have on "display"
//Simple Function - pretty much passthrough. All handle by simple handler
var simple = ['log', 'table', 'error', 'warn'];
/*
* Mapping and Exporting of Functions
*/
for(var i = 0; i < simple.length; i++){
this[simple[i]] = simpleHandler(simple[i]);
}
/*
* Handlers
*/
function simpleHandler(type){
//handler functionality
return function(){
if(configs.passthrough === true){
console[type].apply(console,arguments);
}
record(type, arguments);
send(type, arguments);
}
}
/*
* History Management
*/
/**
* Records a new item in the history.
* This must obey config.history_length
*/
function record(type, args){
var insert_index = history_index;
//when history_length is not zero, enforce ring history
if(configs.history_length !== 0){
insert_index = history_index % configs.history_length;
}
history[insert_index] = {
type: type
,args: args
};
history_index++;
}
/**
* Playback a certain amount of the history to the console
* @param number Number of items to playback. Undefined and 0 default to all
*/
function playback(number){
//This guarantees we will never playback more element
//than what exists in the history
var playback_length = Math.min(history_index, configs.history_length);
//If we have a non-zero number, prefer that
if(!(number === 0 || number === undefined)){
playback_length = Math.min(number, playback_length);
}
var start_index = 0;
//when the index is longer than the length, we've made a ring
//handle this appropriately;
if(history_index >= configs.history_length) {
start_index = history_index % configs.history_length;
}
for(var i = start_index; i < playback_length; i++){
console[history[i][type]].apply(console,history[i][args]); //output to console
}
}
/*
* Remote Logging
*/
function send(type, arguments){
//Do nothing is we don't have logging enabled
if(!(configs.remote_logging !== false || configs.remote_url !== undefined)) return;
var data = {};
data[type] = arguments;
var request;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
request = new XMLHttpRequest();
} else {
// code for IE6, IE5
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('POST', configs.remote_url, true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
request.send(JSON.stringify(data));
}
/*
* Exports
*/
this.playback = playback;
this.record = record;
/**
* Protect window.console method calls, e.g. console is not defined on IE
* unless dev tools are open, and IE doesn't define console.debug
*
* Special thanks to Peter Tseng: http://stackoverflow.com/a/13817235/2132305
*/
(function () {
if (!window.console) {
window.console = {};
}
// union of Chrome, FF, IE, and Safari console methods
var m = [
"log", "info", "warn", "error", "debug", "trace", "dir", "group",
"groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
"dirxml", "assert", "count", "markTimeline", "timeStamp", "clear"
];
// define undefined methods as noops to prevent errors
for (var i = 0; i < m.length; i++) {
if (!window.console[m[i]]) {
window.console[m[i]] = function () {
};
}
}
})();
};