Logging.js
9.64 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/***
MochiKit.Logging 0.80
See <http://mochikit.com/> for documentation, downloads, license, etc.
(c) 2005 Bob Ippolito. All rights Reserved.
***/
if (typeof(dojo) != 'undefined') {
dojo.provide('MochiKit.Logging');
dojo.require('MochiKit.Base');
}
if (typeof(JSAN) != 'undefined') {
JSAN.use("MochiKit.Base", []);
}
try {
if (typeof(MochiKit.Base) == 'undefined') {
throw "";
}
} catch (e) {
throw "MochiKit.Logging depends on MochiKit.Base!";
}
if (typeof(MochiKit.Logging) == 'undefined') {
MochiKit.Logging = {};
}
MochiKit.Logging.NAME = "MochiKit.Logging";
MochiKit.Logging.VERSION = "0.80";
MochiKit.Logging.__repr__ = function () {
return "[" + this.NAME + " " + this.VERSION + "]";
};
MochiKit.Logging.toString = function () {
return this.__repr__();
};
MochiKit.Logging.EXPORT = [
"LogLevel",
"LogMessage",
"Logger",
"alertListener",
"logger",
"log",
"logError",
"logDebug",
"logFatal",
"logWarning"
];
MochiKit.Logging.EXPORT_OK = [
"logLevelAtLeast",
"isLogMessage",
"compareLogMessage"
];
MochiKit.Logging.logLevelAtLeast = function (minLevel) {
/***
Return a function that will match log messages whose level
is at least minLevel
***/
if (typeof(minLevel) == 'string') {
minLevel = MochiKit.Logging.LogLevel[minLevel];
}
return function (msg) {
var msgLevel = msg.level;
if (typeof(msgLevel) == 'string') {
msgLevel = MochiKit.Logging.LogLevel[msgLevel];
}
return msgLevel >= minLevel;
}
};
MochiKit.Logging.LogMessage = function (num, level, info) {
this.num = num;
this.level = level;
this.info = info;
this.timestamp = new Date();
};
MochiKit.Logging.LogMessage.prototype.repr = function () {
return 'LogMessage(' +
MochiKit.Base.map(
MochiKit.Base.repr,
[this.num, this.level, this.info]
).join(', ') + ')';
};
MochiKit.Logging.LogMessage.prototype.toString = function () {
return this.repr();
};
MochiKit.Logging.isLogMessage = function (/* ... */) {
var LogMessage = MochiKit.Logging.LogMessage;
for (var i = 0; i < arguments.length; i++) {
if (!(arguments[i] instanceof LogMessage)) {
return false;
}
}
return true;
};
MochiKit.Logging.compareLogMessage = function (a, b) {
return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]);
};
MochiKit.Logging.Logger = function (/* optional */maxSize) {
/***
A basic logger object that has a buffer of recent messages
plus a listener dispatch mechanism for "real-time" logging
of important messages
maxSize is the maximum number of entries in the log.
If maxSize >= 0, then the log will not buffer more than that
many messages.
There is a default logger available named "logger", and several
of its methods are also global functions:
logger.log -> log
logger.debug -> logDebug
logger.warning -> logWarning
logger.error -> logError
logger.fatal -> logFatal
***/
this.counter = 0;
if (typeof(maxSize) == 'undefined' || maxSize == null) {
maxSize = -1;
}
this.maxSize = maxSize;
this._messages = [];
this.listeners = {};
};
MochiKit.Logging.Logger.prototype.clear = function () {
/***
Clear all messages from the message buffer.
***/
this._messages.splice(0, this._messages.length);
};
MochiKit.Logging.Logger.prototype.dispatchListeners = function (msg) {
/***
Dispatch a log message to all listeners.
***/
for (var k in this.listeners) {
var pair = this.listeners[k];
if (pair[0] && !pair[0](msg)) {
continue;
}
pair[1](msg);
}
};
MochiKit.Logging.Logger.prototype.addListener = function (ident, filter, listener) {
/***
Add a listener for log messages.
ident is a unique identifier that may be used to remove the listener
later on.
filter can be one of the following:
null:
listener(msg) will be called for every log message
received.
string:
logLevelAtLeast(filter) will be used as the function
(see below).
function:
filter(msg) will be called for every msg, if it returns
true then listener(msg) will be called.
listener is a function that takes one argument, a log message. A log
message has three properties:
num:
A counter that uniquely identifies a log message (per-logger)
level:
A string or number representing the log level. If string, you
may want to use LogLevel[level] for comparison.
info:
A list of objects passed as arguments to the log function.
***/
if (typeof(filter) == 'string') {
filter = MochiKit.Logging.logLevelAtLeast(filter);
}
this.listeners[ident] = [filter, listener];
};
MochiKit.Logging.Logger.prototype.removeListener = function (ident) {
/***
Remove a listener using the ident given to addListener
***/
delete this.listeners[ident];
};
MochiKit.Logging.Logger.prototype.baseLog = function (level, message/*, ...*/) {
/***
The base functionality behind all of the log functions.
The first argument is the log level as a string or number,
and all other arguments are used as the info list.
This function is available partially applied as:
Logger.debug 'DEBUG'
Logger.log 'INFO'
Logger.error 'ERROR'
Logger.fatal 'FATAL'
Logger.warning 'WARNING'
For the default logger, these are also available as global functions,
see the Logger constructor documentation for more info.
***/
var msg = new MochiKit.Logging.LogMessage(
this.counter,
level,
MochiKit.Base.extend(null, arguments, 1)
);
this._messages.push(msg);
this.dispatchListeners(msg);
this.counter += 1;
while (this.maxSize >= 0 && this._messages.length > this.maxSize) {
this._messges.shift();
}
};
MochiKit.Logging.Logger.prototype.getMessages = function (howMany) {
/***
Return a list of up to howMany messages from the message buffer.
***/
var firstMsg = 0;
if (!(typeof(howMany) == 'undefined' || howMany == null)) {
firstMsg = Math.max(0, this._messages.length - howMany);
}
return this._messages.slice(firstMsg);
};
MochiKit.Logging.Logger.prototype.getMessageText = function (howMany) {
/***
Get a string representing up to the last howMany messages in the
message buffer. The default is 30.
The message looks like this:
LAST {messages.length} MESSAGES:
[{msg.num}] {msg.level}: {m.info.join(' ')}
[{msg.num}] {msg.level}: {m.info.join(' ')}
...
If you want some other format, use Logger.getMessages and do it
yourself.
***/
if (typeof(howMany) == 'undefined' || howMany == null) {
howMany = 30;
}
var messages = this.getMessages(howMany);
if (messages.length) {
var lst = map(function (m) {
return '\n [' + m.num + '] ' + m.level + ': ' + m.info.join(' ');
}, messages);
lst.unshift('LAST ' + messages.length + ' MESSAGES:');
return lst.join('');
}
return '';
};
MochiKit.Logging.Logger.prototype.debuggingBookmarklet = function () {
alert(this.getMessageText());
};
MochiKit.Logging.alertListener = function (msg) {
/***
Ultra-obnoxious alert(...) listener
***/
alert(
"num: " + msg.num +
"\nlevel: " + msg.level +
"\ninfo: " + msg.info.join(" ")
);
};
MochiKit.Logging.__new__ = function () {
this.LogLevel = {
'ERROR': 40,
'FATAL': 50,
'WARNING': 30,
'INFO': 20,
'DEBUG': 10
};
MochiKit.Base.registerComparator("LogMessage",
this.isLogMessage,
this.compareLogMessage
);
var partial = MochiKit.Base.partial;
var Logger = this.Logger;
Logger.prototype.debug = partial(Logger.prototype.baseLog, 'DEBUG');
Logger.prototype.log = partial(Logger.prototype.baseLog, 'INFO');
Logger.prototype.error = partial(Logger.prototype.baseLog, 'ERROR');
Logger.prototype.fatal = partial(Logger.prototype.baseLog, 'FATAL');
Logger.prototype.warning = partial(Logger.prototype.baseLog, 'WARNING');
// indirectly find logger so it can be replaced
var self = this;
var connectLog = function (name) {
return function () {
self.logger[name].apply(self.logger, arguments);
}
};
this.log = connectLog('log');
this.logError = connectLog('error');
this.logDebug = connectLog('debug');
this.logFatal = connectLog('fatal');
this.logWarning = connectLog('warning');
this.logger = new Logger();
this.EXPORT_TAGS = {
":common": this.EXPORT,
":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
};
MochiKit.Base.nameFunctions(this);
};
MochiKit.Logging.__new__();
if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
|| (typeof(MochiKit.__compat__) == 'boolean' && MochiKit.__compat__)) {
(function (self) {
var all = self.EXPORT_TAGS[":all"];
for (var i = 0; i < all.length; i++) {
this[all[i]] = self[all[i]];
}
})(MochiKit.Logging);
}