forked from polotek/log4js-syslog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (57 loc) · 1.72 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
var log4js = require('log4js')
, syslog = require('node-syslog');
var levels = {}
levels[log4js.levels.ALL] = syslog.LOG_DEBUG;
levels[log4js.levels.TRACE] = syslog.LOG_DEBUG;
levels[log4js.levels.DEBUG] = syslog.LOG_DEBUG;
levels[log4js.levels.INFO] = syslog.LOG_INFO;
levels[log4js.levels.WARN] = syslog.LOG_WARNING;
levels[log4js.levels.ERROR] = syslog.LOG_ERR;
levels[log4js.levels.FATAL] = syslog.LOG_CRIT;
function getOptions(flags) {
var opts = 0;
if(Array.isArray(flags)) {
for(var i = 0, len = flags.length; i < len; i++) {
opts = opts | flags[i];
}
}
return opts;
}
function getSyslogLevel(level) {
return level && levels[level] ? levels[level] : null;
}
function open(config) {
config = config || {}
var name = (config.ident || config.name || 'node-syslog') + ''
, optsVal = (syslog.LOG_PID | syslog.LOG_CONS | syslog.LOG_ODELAY)
, facility = syslog.LOG_LOCAL0;
if(config.flags) {
optsVal = getOptions(config.flags);
}
if(config.facility && syslog[config.facility]) {
facility = syslog[config.facility];
}
// no need to check if it's already open, the lib does that
syslog.init(name, optsVal, facility);
return syslogAppender();
}
function syslogAppender (config) {
//open(config);
return function(loggingEvent) {
var level = getSyslogLevel(loggingEvent.level)
, data = loggingEvent.data
, layout
if(level) {
layout = config && config.layout ? config.layout : log4js.layouts.basicLayout;
data = layout(loggingEvent);
syslog.log(level, data);
}
};
}
// This is a binding so no context needed for close function
process.on('exit', syslog.close);
exports.name = "syslog";
exports.appender = syslogAppender;
exports.configure = open;
exports.open = open;
exports.close = syslog.close;