-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.coffee
48 lines (43 loc) · 1.39 KB
/
log.coffee
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
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:->
d = new Date(Date.parse(this.created_at))
"#{d.getUTCFullYear()}-#{d.getUTCMonth()}-#{d.getUTCDate()}T#{d.getUTCHours()}:#{d.getUTCMinutes()}:#{d.getUTCSeconds()}"
static:
log:(level,category,message,payload,fn)->
unless 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
l.save ->
fn(l) if fn
info: (cat,msg,ld,fn)->
this.log('info', cat, msg, ld, fn)
debug: (cat,msg,ld,fn)->
this.log 'debug',cat,msg,ld,fn
warn: (cat,msg,ld,fn)->
this.log 'warn',cat,msg,ld,fn
err: (cat,msg,ld,fn)->
this.log 'error',cat,msg,ld,fn
fetch:(level,category,fn)->
if !fn and !category
fn = level
else if !fn
fn = category
q = this.find()
q.where('level',level) if level
q.where('category',category) if category
q.limit(100)
q.sort([['$natural',-1]])
q.all (logs)->
fn(logs)
exports.Logger = mongo.db.model 'Log'