-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.js
39 lines (32 loc) · 1.07 KB
/
syslog.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
const udp = require('dgram');
class SysLogServer {
constructor(
logger,
doSysLogMessage = () => {}
) {
this.logger = logger;
this.doSysLogMessage = doSysLogMessage;
}
start(port) {
const server = udp.createSocket('udp4');
server.on('error', error => {
this.logger.error('Syslog Error', error);
server.close();
});
server.on('message', (msg, info) => {
this.logger.log('Data received from client : ' + msg.toString(), 'A');
this.logger.log(`Received ${msg.length} bytes from ${info.address}:${info.port}`, 'A');
this.doSysLogMessage(msg, info);
});
server.on('listening', () => {
const address = server.address();
this.logger.log('SysLog Server is listening at port: ' + address.port);
});
server.on('close', () => {
console.log('Socket is closed !');
});
server.bind(port);
return server;
}
}
module.exports.SysLogServer = SysLogServer;