-
Notifications
You must be signed in to change notification settings - Fork 10
/
jog.js
146 lines (121 loc) · 2.63 KB
/
jog.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
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
/**
* Module dependencies.
*/
var FileStore = require('./stores/FileStore')
, RedisStore = require('./stores/RedisStore');
/**
* Expose `Jog`.
*/
exports = module.exports = Jog;
/**
* Library version.
*/
exports.version = '0.1.0';
/**
* Default log levels.
*/
exports.levels = ['debug', 'info', 'warn', 'error'];
/**
* Expose stores.
*/
exports.FileStore = FileStore;
exports.RedisStore = RedisStore;
/**
* Initialize a new `Jog` log with
* the given `store`. The store must
* implement:
*
* - `add(obj)` to add a log object
* - `stream() => EventEmitter` to stream data
* - `stream({ end: false }) => EventEmitter` to stream data indefinitely
* - `clear(fn)` to clear the logs
*
* @param {Object} store
* @param {Object} defaults
* @api public
*/
function Jog(store, defaults) {
if (!(this instanceof Jog)) return new Jog(store, defaults);
this.store = store;
this.defaults = defaults;
}
/**
* Namespace with `obj`, returning a new `Jog`.
*
* Example:
*
* var log = new Jog(store);
*
* // use for all user related logs
* log = log.ns({ uid: user.id });
*
* // use for all user video related logs (inherits uid from above)
* log = log.ns({ vid: video.id });
*
* @param {Object} obj
* @return {Jog}
* @api public
*/
Jog.prototype.ns = function(obj){
var defaults = this.defaults;
for (var key in defaults) {
if (obj.hasOwnProperty(key)) continue;
obj[key] = defaults[key];
}
return new Jog(this.store, obj);
};
/**
* Write the given log `level`, `msg`, and `attrs` object
* to the store.
*
* @param {String} level
* @param {String} msg
* @param {Object} attrs
* @api public
*/
Jog.prototype.write = function(level, msg, attrs){
var defaults = this.defaults;
attrs = attrs || {};
attrs.level = level;
attrs.msg = msg;
// implicit timestamp
attrs.timestamp = attrs.timestamp || Date.now();
// merge defaults
if (defaults) {
Object.keys(defaults).forEach(function(key){
attrs[key] = defaults[key];
});
}
// add it to the store
this.store.add(attrs);
return this;
};
/**
* Stream data from the store.
*
* @param {Object} options
* @return {EventEmitter}
* @api public
*/
Jog.prototype.stream = function(options){
options = options || {};
options.interval = options.interval || 2000;
return this.store.stream(options);
};
/**
* Clear the logging data.
*
* @param {Function} fn
* @api public
*/
Jog.prototype.clear = function(fn){
return this.store.clear(fn);
};
/**
* Log level methods.
*/
exports.levels.forEach(function(level){
Jog.prototype[level] = function(msg, attrs){
this.write(level, msg, attrs);
};
})