-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.js
73 lines (73 loc) · 2.03 KB
/
log.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
(function() {
var mongo, sys;
require.paths.unshift('./vendor');
mongo = require('./mongo');
sys = require('sys');
mongo.mongoose.model('Log', {
properties: ['created_at', 'level', 'category', 'message', 'payload'],
indexes: [
'category', 'level', {
'created_at': -1
}
],
getters: {
xmlDate: function() {
var d;
d = new Date(Date.parse(this.created_at));
return "" + (d.getUTCFullYear()) + "-" + (d.getUTCMonth()) + "-" + (d.getUTCDate()) + "T" + (d.getUTCHours()) + ":" + (d.getUTCMinutes()) + ":" + (d.getUTCSeconds());
}
},
static: {
log: function(level, category, message, payload, fn) {
var l;
if (!(process.env.RACK_ENV === 'production')) {
sys.puts(("[" + (category) + "] " + (message)));
}
l = new this.constructor();
l.level = level;
l.category = category;
l.message = message;
l.created_at = new Date();
l.payload = payload;
return l.save(function() {
if (fn) {
return fn(l);
}
});
},
info: function(cat, msg, ld, fn) {
return this.log('info', cat, msg, ld, fn);
},
debug: function(cat, msg, ld, fn) {
return this.log('debug', cat, msg, ld, fn);
},
warn: function(cat, msg, ld, fn) {
return this.log('warn', cat, msg, ld, fn);
},
err: function(cat, msg, ld, fn) {
return this.log('error', cat, msg, ld, fn);
},
fetch: function(level, category, fn) {
var q;
if (!fn && !category) {
fn = level;
} else if (!fn) {
fn = category;
}
q = this.find();
if (level) {
q.where('level', level);
}
if (category) {
q.where('category', category);
}
q.limit(100);
q.sort([['$natural', -1]]);
return q.all(function(logs) {
return fn(logs);
});
}
}
});
exports.Logger = mongo.db.model('Log');
})();