forked from prezi/plotserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
130 lines (109 loc) · 3.42 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
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
var fs = require("fs");
var dateformat = require("./dateformat");
var scribe = require('scribe').Scribe;
//
// ===============================================
//
// Logger
//
// ===============================================
//
var Logger = function() {
// public:
this.addEndpoint = function(endpoint) {
_endpoints.push(endpoint);
};
this.error = function(message) {
_log("error", message);
};
this.warn = function(message) {
_log("warn", message);
};
this.info = function(message) {
_log("info", message);
};
this.debug = function(message) {
_log("debug", message);
};
// private:
var _log = function(level, message) {
var now, i, max;
for (i = 0, max = _endpoints.length; i < max; i += 1) {
var endpoint = _endpoints[i];
endpoint.log(new Date(), level, message);
}
}
var _endpoints = [];
};
Logger.levelMap = {"error": 0, "warn": 1, "info": 2, "debug": 3};
module.exports.create = function() {
return new Logger();
};
module.exports.endpoints = {};
//
// ===============================================
//
// Console logger
//
// ===============================================
//
module.exports.endpoints.console = function(settings) {
var _settings = settings;
return new function() {
this.log = function(date, level, message) {
if (Logger.levelMap[level] <= Logger.levelMap[_settings["level"]]) {
var line = date.format("yyyy-mm-dd HH:MM:ss") + " " + level.toUpperCase() + " " + message;
console.log(line);
}
}
}
};
//
// ===============================================
//
// Daily file logger
//
// ===============================================
//
module.exports.endpoints.dailyLogFile = function(settings) {
var _settings = settings;
var _previousFile = "";
var _symlink = function(file) {
var symlink = settings.directory + "/" + settings.file + "_current";
if (file != _previousFile) {
fs.unlink(symlink);
fs.symlink(file, symlink);
_previousFile = file;
}
}
return new function() {
this.log = function(date, level, message) {
if (Logger.levelMap[level] <= Logger.levelMap[_settings["level"]]) {
var file = settings.file + "-" + date.format("yyyy-mm-dd");
var line = date.format("yyyy-mm-dd HH:MM:ss") + " " + level.toUpperCase() + " " + message + "\n";
fs.appendFile(settings.directory + "/" + file, line, function (err) {});
_symlink(file);
}
}
}
};
//
// ===============================================
//
// Scribe logger
//
// ===============================================
//
module.exports.endpoints.scribe = function(settings) {
var _settings = settings;
var _client = new scribe(settings["host"], settings["port"], { autoReconnect:true });
_client.open( function() {} );
return new function() {
this.log = function(date, level, message) {
if (Logger.levelMap[level] <= Logger.levelMap[_settings["level"]]) {
var line = date.format("yyyy-mm-dd HH:MM:ss") + " " + level.toUpperCase() + " " + message + "\n";
_client.send(_settings["category"], line);
}
}
}
};