-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
54 lines (45 loc) · 1.39 KB
/
logger.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
var config = require("./logger_config"),
talker = require("./lib/talker"),
sys = require("sys"),
file = require('file'),
path = require('path')
repl = require('repl');
//
// Logging component was adapted from http://github.com/felixge/nodelog/
//
var logFile, day;
function writeLog(text) {
var date = new Date;
var today = [
date.getFullYear(),
// Poor man's zero padding FTW
('0'+(date.getMonth()+1)).substr(-2),
('0'+date.getDate()).substr(-2),
].join('-');
if (!logFile || day !== today) {
logFile = new file.File(path.join('log', today+'.txt'), 'a+', {encoding: 'utf8'});
day = today;
}
var time = [
('0'+date.getHours()).substr(-2),
('0'+date.getMinutes()).substr(-2)
].join(':');
logFile.write('['+time+'] '+text+"\n");
}
// Leave in the global space so the REPL can access if need be.
// Streaming Hot Logging FTW.
log_client = new talker.Client(config);
log_client.connect();
log_client.addListener("message", function(msg) {
writeLog(obj.user.name+': '+obj.content);
});
log_client.addListener("join", function(obj) {
writeLog(obj.user.name+' <'+obj.user.email+'> joined the room.');
});
log_client.addListener("connected", function(obj) {
writeLog('Started Logging.');
});
log_client.addListener("leave", function(obj) {
writeLog(obj.user.name+' <'+obj.user.email+'> left the room.');
});
repl.start("");