forked from statsd/statsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
43 lines (38 loc) · 1.02 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
/*jshint node:true, laxcomma:true */
var Logger = function (config) {
this.config = config;
this.backend = this.config.backend || 'stdout';
this.level = this.config.level || "LOG_INFO";
if (this.backend == 'stdout') {
this.util = require('util');
} else {
if (this.backend == 'syslog') {
this.util = require('modern-syslog');
this.util.init(config.application || 'statsd', this.util.LOG_PID | this.util.LOG_ODELAY, this.util.LOG_LOCAL0);
} else {
throw "Logger: Should be 'stdout' or 'syslog'.";
}
}
};
Logger.prototype = {
log: function (msg, type) {
if (this.backend == 'stdout') {
if (!type) {
type = 'DEBUG';
}
this.util.log(type + ": " + msg);
} else {
var level;
if (!type) {
level = this.level;
} else {
level = "LOG_" + type.toUpperCase();
}
if (!this.util[level]) {
throw "Undefined log level: " + level;
}
this.util.log(this.util[level], msg);
}
}
};
exports.Logger = Logger;