jlog-rails 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ // var buster = require("buster");
2
+
3
+ var id = 0;
4
+ buster.testCase("Check logger main possibilities", {
5
+ setUp: function() {
6
+ JLog.handleError = function(error) { throw error };
7
+ },
8
+
9
+ tearDown: function() {
10
+ },
11
+
12
+ "Check inheritance": function() {
13
+ var hitGirl = JLog.getLogger("bigDaddy.hitGirl");
14
+ var bigDaddy = JLog.getLogger("bigDaddy");
15
+ var rootLogger = JLog.getRootLogger();
16
+
17
+ assert.equals(hitGirl.parent, bigDaddy, "Named parent must match");
18
+ assert.equals(bigDaddy.parent, rootLogger, "Root logger has to be parent of base loggers");
19
+ },
20
+
21
+ "Logger should evaluate levels": function() {
22
+ var logger = JLog.getLogger("kick-ass");
23
+ logger.setLevel(JLog.Level.WARN);
24
+
25
+ var appender = new JLog.ConsoleAppender();
26
+ var stub = sinon.stub(appender, "append");
27
+ logger.addAppender(appender);
28
+
29
+ logger.info("hello");
30
+ assert(!stub.called, "INFO is lower then WARN");
31
+ logger.warn("hello");
32
+ assert(stub.called, "WARN should be logged");
33
+ },
34
+
35
+ "Check log message propagation": function() {
36
+ var hitGirl = JLog.getLogger("bigDaddy.hitGirl");
37
+ var bigDaddy = JLog.getLogger("bigDaddy");
38
+ hitGirl.setLevel(JLog.Level.ALL);
39
+ bigDaddy.setLevel(JLog.Level.ALL);
40
+
41
+ var appender = new JLog.ConsoleAppender();
42
+ var stub = sinon.stub(appender, "append");
43
+ bigDaddy.addAppender(appender);
44
+ hitGirl.error("hello");
45
+
46
+ assert(stub.called, "Log message should rich appender");
47
+ stub.restore();
48
+ }
49
+ });
@@ -0,0 +1,37 @@
1
+ var id = 0;
2
+
3
+ buster.testCase("Check pattern layout", {
4
+ setUp: function() {
5
+ JLog.errorHandler = function(error) { throw error };
6
+ },
7
+
8
+ tearDown: function() {
9
+ },
10
+
11
+ "Basic layout": function() {
12
+ var pl = new JLog.PatternLayout("%m%n");
13
+ var res = pl.format(new JLog.LoggingEvent(null, new Date(), JLog.Level.INFO, ["hello"], null));
14
+ assert.equals("hello\n", res, "just message with new line");
15
+ },
16
+
17
+ "Custom fields": function() {
18
+ var pl = new JLog.PatternLayout("%f{left} %m %f{right}");
19
+ pl.setCustomField("left", "<<");
20
+ pl.setCustomField("right", ">>");
21
+ var res = pl.format(new JLog.LoggingEvent(null, new Date(), JLog.Level.INFO, ["whats up?"], null));
22
+ assert.equals("<< whats up? >>", res, "message with custom fields");
23
+ },
24
+
25
+ "Logger name and Level": function() {
26
+ var pl = new JLog.PatternLayout("%c [%p] %m");
27
+ var res = pl.format(new JLog.LoggingEvent(JLog.getLogger("hi-log"), new Date(),
28
+ JLog.Level.INFO, ["msg-msg"], null));
29
+ assert.equals("hi-log [INFO] msg-msg", res, "Logger name and Level has to be printed");
30
+ },
31
+
32
+ "Startup time": function() {
33
+ var pl = new JLog.PatternLayout("%r");
34
+ var res = pl.format(new JLog.LoggingEvent(null, new Date(), JLog.Level.INFO, ["msg-msg"], null));
35
+ assert(/\d+/.test(res), "Startup time has to be printed");
36
+ }
37
+ });
@@ -0,0 +1,93 @@
1
+ /*
2
+ Class: Appender
3
+
4
+ Base class for sending/outputting of logging event.
5
+ */
6
+ JLog.Appender = function() {};
7
+
8
+ JLog.Appender.prototype = {
9
+ /*
10
+ Property: layout
11
+
12
+ Layout to use with this appender.
13
+ */
14
+ layout: new JLog.PatternLayout(),
15
+
16
+ /*
17
+ Property: threshold
18
+
19
+ Appender's internal <Level>.
20
+ */
21
+ threshold: JLog.Level.ALL,
22
+
23
+ /*
24
+ Method: doAppend
25
+
26
+ Checks and navigates logging events. Logger uses this method as gateway.
27
+
28
+ Parameters:
29
+ loggingEvent - <LoggingEvent> to append
30
+ */
31
+ doAppend: function(loggingEvent) {
32
+ if(loggingEvent.level.isGreaterOrEqual(this.threshold)) {
33
+ this.append(loggingEvent);
34
+ }
35
+ },
36
+
37
+ /*
38
+ Method: append
39
+
40
+ Virtual method, has to be overloaded by derivative classes to perform actual appending.
41
+
42
+ Parameters:
43
+ loggingEvent - <LoggingEvent> to append
44
+ */
45
+ append: function(loggingEvent) {
46
+ JLog.handleError("JLog.Appender: This method has to be implemented by derivative classes!");
47
+ },
48
+
49
+ /*
50
+ Method: setLayout
51
+
52
+ Set appender's <Layout>
53
+
54
+ Parameters:
55
+ layout - <Layout> for appender.
56
+ */
57
+ setLayout: function(layout) {
58
+ if (layout instanceof JLog.Layout) {
59
+ this.layout = layout;
60
+ } else {
61
+ JLog.handleError("Appender.setLayout: layout supplied to " +
62
+ this.toString() + " is not a subclass of Layout");
63
+ }
64
+ },
65
+
66
+ /*
67
+ Method: getLayout
68
+
69
+ Get appender's <Layout>
70
+ */
71
+ getLayout: function() { return this.layout; },
72
+
73
+ /*
74
+ Method: setThreshold
75
+
76
+ Set appender's threshold <Level>
77
+
78
+ Parameters:
79
+ threshold - <Level> for appender.
80
+ */
81
+ setThreshold: function(threshold) { this.threshold = threshold; },
82
+
83
+ /*
84
+ Method: getThreshold
85
+
86
+ Get appender's threshold <Level>
87
+ */
88
+ getThreshold: function() { return this.threshold; },
89
+
90
+ toString: function() {
91
+ JLog.handleError("Appender.toString: all appenders must override this method");
92
+ }
93
+ };
@@ -0,0 +1,151 @@
1
+ /*
2
+ Class: AjaxAppender
3
+
4
+ Ajax calls based appender.
5
+ */
6
+ JLog.AjaxAppender = function(url) {
7
+ this.name = 'AjaxAppender';
8
+
9
+ // Do we make more then 1 call at a time?
10
+ var waitForResponse = true;
11
+ // Current buffer of messages
12
+ var queuedLoggingEvents = [];
13
+ // Messages which should be sent
14
+ var queuedRequests = [];
15
+ // Maximum count of messages sent at a one time
16
+ var batchSize = 10;
17
+ // Are we currently sending something
18
+ var sending = false;
19
+ // Timeout between sending data
20
+ var timerInterval = 1000;
21
+
22
+ function scheduleSending() {
23
+ window.setTimeout(sendAllRemaining, timerInterval);
24
+ }
25
+
26
+ function sendRequest(postData, callback) {
27
+ $.ajax({
28
+ type: 'POST',
29
+ url: url,
30
+ data: postData,
31
+ dataType: 'json',
32
+ global: false,
33
+ complete: function() {
34
+ if (waitForResponse) sending = false;
35
+ if (callback) callback(true);
36
+ }
37
+ });
38
+ }
39
+
40
+ function sendAllRemaining() {
41
+ if (queuedLoggingEvents.length === 0) return;
42
+ var eventCopy = queuedLoggingEvents;
43
+ queuedLoggingEvents = [];
44
+ queuedRequests.push(eventCopy);
45
+ sendAll();
46
+ }
47
+
48
+ function preparePostData(data) {
49
+ return { message:data };
50
+ }
51
+
52
+ function sendAll() {
53
+ if (waitForResponse && sending) return;
54
+ sending = true;
55
+ var currentRequestBatch;
56
+ if (waitForResponse) {
57
+ if (queuedRequests.length > 0) {
58
+ currentRequestBatch = queuedRequests.shift();
59
+ sendRequest(preparePostData(currentRequestBatch), sendAll);
60
+ } else {
61
+ sending = false;
62
+ scheduleSending();
63
+ }
64
+ } else {
65
+ // Rattle off all the requests without waiting to see the response
66
+ while ((currentRequestBatch = queuedRequests.shift())) {
67
+ sendRequest(preparePostData(currentRequestBatch));
68
+ }
69
+ sending = false;
70
+ scheduleSending();
71
+ }
72
+ }
73
+
74
+ scheduleSending();
75
+
76
+ /*
77
+ Method: setWaitForResponce
78
+
79
+ If ajax appender has to make calls one by one, or it can do then concurrently.
80
+
81
+ Parameters:
82
+ waitForResponseParam - if should wait
83
+ */
84
+ this.setWaitForResponse = function(waitForResponseParam) {
85
+ waitForResponse = waitForResponseParam;
86
+ }
87
+
88
+ /*
89
+ Method: isWaitForResponse
90
+
91
+ Returns if ajax makes calls one at a time.
92
+ */
93
+ this.isWaitForResponse = function() {
94
+ return waitForResponse;
95
+ }
96
+
97
+ /*
98
+ Method: setBatchSize
99
+
100
+ Set single batch send size.
101
+
102
+ Parameters:
103
+ batchSizeParam - new size of batch
104
+ */
105
+ this.setBatchSize = function(batchSizeParam) {
106
+ batchSize = batchSizeParam;
107
+ }
108
+
109
+ /*
110
+ Method: getBatchSize
111
+
112
+ Return count of messages in batch.
113
+ */
114
+ this.getBatchSize = function() {
115
+ return batchSize;
116
+ }
117
+
118
+ /*
119
+ Method: setTimeout
120
+
121
+ Set buffering time before sending unfinished batch.
122
+
123
+ Parameters:
124
+ timeout - milliseconds
125
+ */
126
+ this.setBatchSize = function(timeout) {
127
+ timerInterval = timeout
128
+ }
129
+
130
+ /*
131
+ Method: setTimeout
132
+
133
+ Return timeout.
134
+ */
135
+ this.getBatchSize = function() {
136
+ return timerInterval;
137
+ }
138
+
139
+ /*
140
+ Method: append
141
+
142
+ Attempts to send message.
143
+ */
144
+ this.append = function(msg) {
145
+ queuedLoggingEvents.push({level:msg.level.name, message:this.getLayout().format(msg)});
146
+ if(queuedLoggingEvents.length >= batchSize) sendAllRemaining();
147
+ else if(queuedLoggingEvents.length == 1) scheduleSending();
148
+ }
149
+ };
150
+
151
+ JLog.AjaxAppender.prototype = new JLog.Appender();
@@ -0,0 +1,20 @@
1
+ /*
2
+ Class: ConsoleAppender
3
+
4
+ Simple console appender.
5
+ */
6
+ JLog.ConsoleAppender = function() {
7
+ this.name = 'ConsoleAppender';
8
+
9
+ /*
10
+ Method: append
11
+
12
+ Attempts to append message into console.
13
+ */
14
+ if (window.console && window.console.log)
15
+ this.append = function(loggingEvent) {
16
+ window.console.log(this.getLayout().format(loggingEvent));
17
+ };
18
+ };
19
+
20
+ JLog.ConsoleAppender.prototype = new JLog.Appender();
@@ -1,3 +1,14 @@
1
+ //= include mount
2
+
3
+ //= include levels
4
+ //= include logger
5
+ //= include logging_event
6
+ //= include layout
7
+ //= include layouts/pattern
8
+ //= include appender
9
+ //= include appenders/ajax
10
+ //= include appenders/console
11
+
1
12
  /**
2
13
  *
3
14
  * @author Marcus R Breese mailto:mbreese@users.sourceforge.net
@@ -30,239 +41,3 @@
30
41
  *
31
42
  **/
32
43
 
33
- function JLog(name) {
34
- this._appenders.push(new JLog.ConsoleAppender());
35
- this.setName(name);
36
- }
37
-
38
- JLog.ALL = 0;
39
- JLog.DEBUG = 1;
40
- JLog.INFO = 2;
41
- JLog.WARN = 3;
42
- JLog.ERROR = 4;
43
- JLog.FATAL = 5;
44
- JLog.NONE = 6;
45
-
46
- JLog.prototype = {
47
- _currentLevel: JLog.ALL,
48
- _appenders: [],
49
- _enabled: true,
50
- _name: null,
51
-
52
- debug: function() {
53
- if (this.getLevel() <= JLog.DEBUG) {
54
- this._log("DEBUG", arguments);
55
- }
56
- },
57
-
58
- info: function() {
59
- if (this.getLevel() <= JLog.INFO) {
60
- this._log("INFO", arguments);
61
- }
62
- },
63
-
64
- warn: function() {
65
- if (this.getLevel() <= JLog.WARN) {
66
- this._log("WARN", arguments);
67
- }
68
- },
69
-
70
- error: function() {
71
- if (this.getLevel() <= JLog.ERROR) {
72
- this._log("ERROR", arguments);
73
- }
74
- },
75
-
76
- fatal: function() {
77
- if (this.getLevel() <= JLog.FATAL) {
78
- this._log("FATAL", arguments);
79
- }
80
- },
81
-
82
- _log: function() {
83
- if (this.isOn()) {
84
- var level = arguments[0],
85
- args = Array.prototype.slice.call(arguments[1] || []),
86
- namePrefix = this.getName() ? '[' + this.getName() + ']' : '',
87
- msgString = level + namePrefix + ': ',
88
- appenders = this.getAppenders();
89
-
90
- for (var i in args) {
91
- if (typeof args[i] === 'object') {
92
- args[i] = JSON.stringify(args[i]);
93
- }
94
- }
95
-
96
- msgString += args.join(', ');
97
- for(i = 0; i < appenders.length; ++i) {
98
- appenders[i].log(msgString);
99
- }
100
- }
101
- },
102
-
103
- getName: function() {
104
- return _name;
105
- },
106
-
107
- setName: function(name) {
108
- _name = name || null;
109
- },
110
-
111
- addAppender: function(appender) {
112
- if (appender) {
113
- this._appenders.push(appender);
114
- }
115
- },
116
-
117
- removeAppender: function(name) {
118
- for (var i in _appenders) {
119
- if (this._appenders[i].name === name) {
120
- this._appenders.splice(i, 1);
121
- }
122
- }
123
- return null;
124
- },
125
-
126
- turnOn: function() {
127
- this._enabled = true;
128
- },
129
-
130
- turnOff: function() {
131
- this._enabled = false;
132
- },
133
-
134
- isOn: function() {
135
- return this._enabled;
136
- },
137
-
138
- //
139
- // Sets the current threshold log level for this Log instance.
140
- // Only events that have a priority of this level or greater are logged.
141
- //
142
- setLevel: function(level) {
143
- if (typeof level === 'number') {
144
- if (level >= JLog.ALL && level <= JLog.NONE) {
145
- this._currentLevel = level;
146
- } else {
147
- this._currentLevel = JLog.NONE;
148
- }
149
- } else if (level) {
150
- switch(level) {
151
- case 'all': this._currentLevel = JLog.ALL; break;
152
- case 'debug': this._currentLevel = JLog.DEBUG; break;
153
- case 'info': this._currentLevel = JLog.INFO; break;
154
- case 'warn': this._currentLevel = JLog.WARN; break;
155
- case 'error': this._currentLevel = JLog.ERROR; break;
156
- case 'fatal': this._currentLevel = JLog.FATAL; break;
157
- default: this._currentLevel = JLog.NONE;
158
- }
159
- } else {
160
- this._currentLevel = JLog.NONE;
161
- }
162
- },
163
-
164
- getAppender: function(name) {
165
- for (var i in this._appenders) {
166
- if (this._appenders[i].name === name) {
167
- return this._appenders[i];
168
- }
169
- }
170
- return null;
171
- },
172
-
173
- getAppenders: function() {
174
- return this._appenders;
175
- },
176
-
177
- getLevel: function() {
178
- return this._currentLevel;
179
- }
180
- }
181
-
182
- JLog.ConsoleAppender = function() {
183
- this.name = 'ConsoleAppender';
184
-
185
- if (window.console && window.console.log)
186
- this.log = function(msg) { window.console.log(msg); };
187
- else
188
- this.log = function(msg) {};
189
- };
190
-
191
- JLog.AjaxAppender = function(url) {
192
- // Do we make more then 1 call at a time?
193
- var waitForResponse = true;
194
- // Current buffer of messages
195
- var queuedLoggingEvents = [];
196
- // Messages which should be sent
197
- var queuedRequests = [];
198
- // Maximum count of messages sent at a one time
199
- var batchSize = 10;
200
- // Are we currently sending something
201
- var sending = false;
202
- // Timeout between sending data
203
- var timerInterval = 1000;
204
-
205
- function scheduleSending() {
206
- window.setTimeout(sendAllRemaining, timerInterval);
207
- }
208
-
209
- function sendRequest(postData, callback) {
210
- $.ajax({
211
- type: 'POST',
212
- url: url,
213
- data: postData,
214
- dataType: 'json',
215
- global: false,
216
- complete: function() {
217
- if (waitForResponse) sending = false;
218
- if (callback) callback(true);
219
- }
220
- });
221
- }
222
-
223
- function sendAllRemaining() {
224
- if (queuedLoggingEvents.length === 0) return;
225
- var eventCopy = queuedLoggingEvents;
226
- queuedLoggingEvents = [];
227
- queuedRequests.push(eventCopy);
228
- sendAll();
229
- }
230
-
231
- function preparePostData(data) {
232
- return { message:data };
233
- }
234
-
235
- function sendAll() {
236
- if (waitForResponse && sending) return;
237
- sending = true;
238
- var currentRequestBatch;
239
- if (waitForResponse) {
240
- if (queuedRequests.length > 0) {
241
- currentRequestBatch = queuedRequests.shift();
242
- sendRequest(preparePostData(currentRequestBatch), sendAll);
243
- } else {
244
- sending = false;
245
- scheduleSending();
246
- }
247
- } else {
248
- // Rattle off all the requests without waiting to see the response
249
- while ((currentRequestBatch = queuedRequests.shift())) {
250
- sendRequest(preparePostData(currentRequestBatch));
251
- }
252
- sending = false;
253
- scheduleSending();
254
- }
255
- }
256
-
257
- scheduleSending();
258
-
259
- return {
260
- name: 'AjaxAppender',
261
-
262
- log: function(msg) {
263
- queuedLoggingEvents.push(msg);
264
- if (queuedLoggingEvents.length >= batchSize) sendAllRemaining();
265
- else if (queuedLoggingEvents.length == 1) scheduleSending();
266
- }
267
- };
268
- };