forked from rauchg/node.websocket.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.js
68 lines (58 loc) · 1.43 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
/*
---
name: log.js
description: <
Very simple logging based on [redis](http://code.google.com/p/redis/) so that:
* We don't have to implement a piped logging mechanism / log rotation (no file size concerns)
* We can perform common operations like accessing the tail or a RANGE of the logs efficiently
todo:
- Support for multiple loggers, and not a single global one. (Logger as a protypical class)
author: [Guillermo Rauch](http://devthought.com)
...
*/
var sys = require('sys'),
redis = require('./lib/redis'),
ready = null,
busy = false,
client = new redis.Client(),
stack = [],
key = 'logs',
/*
* Called when the redis client is connected. Pushes stack messages
*
*/
onConnect = function(){
sys.puts('[log.js] Connected to redis');
ready = true;
processStack();
},
/*
* Logging method. If server is not ready or a message hasn't been delivered yet, stack the logs.
*
*/
log = function(message){
sys.puts(message);
stack.push(message);
if (ready && !busy) processStack();
},
/*
* Does the logging processing in a non-blocking but ordered way.
*
*/
processStack = function(){
if (stack.length){
client.rpush(key, stack.shift()).addCallback(processStack);
} else {
busy = false;
}
};
this.setKey = function(name){
key = name;
};
this.store = function(message, type){
log(message);
};
// connect to redis
client.connect(onConnect, function(){
ready = false;
});