1 |
|
2 |
|
3 | const Logger = function (config) {
|
4 | this.config = config;
|
5 | this.backend = this.config.backend || 'stdout';
|
6 | this.level = this.config.level || "LOG_INFO";
|
7 | if (this.backend == 'stdout') {
|
8 | this.util = require('util');
|
9 | } else {
|
10 | if (this.backend == 'syslog') {
|
11 | this.util = require('modern-syslog');
|
12 | this.util.init(config.application || 'statsd', this.util.LOG_PID | this.util.LOG_ODELAY, this.util.LOG_LOCAL0);
|
13 | } else {
|
14 | throw "Logger: Should be 'stdout' or 'syslog'.";
|
15 | }
|
16 | }
|
17 | };
|
18 |
|
19 | Logger.prototype = {
|
20 | log: function (msg, type) {
|
21 | if (this.backend == 'stdout') {
|
22 | if (!type) {
|
23 | type = 'DEBUG';
|
24 | }
|
25 | this.util.log(type + ": " + msg);
|
26 | } else {
|
27 | let level;
|
28 | if (!type) {
|
29 | level = this.level;
|
30 | } else {
|
31 | level = "LOG_" + type.toUpperCase();
|
32 | }
|
33 |
|
34 | if (!this.util[level]) {
|
35 | throw "Undefined log level: " + level;
|
36 | }
|
37 |
|
38 | this.util.log(this.util[level], msg);
|
39 | }
|
40 | }
|
41 | };
|
42 |
|
43 | exports.Logger = Logger;
|