forked from brikteknologier/logginator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (102 loc) · 3.43 KB
/
index.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
var fs = require('fs');
var os = require('os');
var winston = require('winston');
var winstonSyslog = require('winston-syslog').Syslog;
var winstonPapertrail = require('winston-papertrail').Papertrail;
var TaggedConsoleTarget = require('tagged-console-target');
var TaggedLogger = require('tagged-logger');
var moduleName = require('./module-name');
var util = require('util');
var _ = require('underscore');
// List of syslog levels, because the one present in winston is
// incorrect and makes logging fail.
var syslogLevels = {
debug: 0,
info: 1,
notice: 2,
warning: 3,
warn: 3, // Keep warn for API compatibility
error: 4,
crit: 5,
alert: 6,
emerg: 7
};
// Monkey-patch winston-syslog to treat "warn" as "warning", to
// maintain API compatibility.
var originalSyslogLog = winstonSyslog.prototype.log;
winstonSyslog.prototype.log = function (level, msg, meta, callback) {
if (level === "warn") level = "warning";
return originalSyslogLog.call(this, level, msg, meta, callback);
};
var engines = {
"console": function () { return new TaggedConsoleTarget(); },
"syslog": function (spec) {
var options = {};
var knownLogTargets = [
"/dev/log", // Linux
"/var/run/syslog", // OS X
"/var/run/log" // BSD
];
function isValidTarget(name) {
return fs.existsSync(name) && fs.statSync(name).isSocket();
}
options.protocol = spec.protocol || "unix";
if (options.protocol === "unix") {
options.path = spec.path;
if (!options.path) {
for (var i = 0; i < knownLogTargets.length; ++i) {
if (isValidTarget(knownLogTargets[i])) {
options.path = knownLogTargets[i];
break;
}
}
if (!options.path) {
throw new Error("Failed to find log socket path, and no such " +
"path was configured in \"path\" for the syslog logger backend");
}
}
}
// For TCP or UDP:
options.host = spec.host;
options.port = spec.port;
options.app_name = spec.appname || moduleName(module);
options.facility = spec.facility;
options.localhost = spec.localhost || os.hostname();
return new winstonSyslog(options);
},
"papertrail": function(spec) {
if (!spec.host || !spec.port) throw new Error("Host and port are required" +
"for papertrail log target");
spec = _.extend({ program: process.title }, spec);
return new winstonPapertrail(spec);
}
};
var defaultConfig = [ { "transport": "console" } ];
module.exports = function (identifiers, config) {
if (typeof config == 'undefined') {
config = identifiers;
identifiers = [ moduleName(module) ];
}
if (typeof identifiers == 'string')
identifiers = [ identifiers ];
config = config || defaultConfig;
if (!Array.isArray(config)) config = []; // Handle {} as no output
var winstonConfig = {
transports: []
};
config.forEach(function (spec) {
if (engines.hasOwnProperty(spec.transport)) {
winstonConfig.transports.push(engines[spec.transport](spec));
}
});
var winstonLogger = new (winston.Logger)(winstonConfig);
winstonLogger.setLevels(syslogLevels);
var logger = new TaggedLogger(winstonLogger, identifiers);
logger.info('created logger');
logger.info('using the following transports:');
config.forEach(function(transport, i) {
logger.info(util.format('%d: [%s], with config %s',
i + 1, transport.transport, JSON.stringify(transport)));
});
return logger;
};