fcs
2025-04-10 0163870d662dc5b7d69ceff977490ede594f7d82
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
const debug = require('debug')('log4js:tcp-server');
const net = require('net');
const clustering = require('../clustering');
const LoggingEvent = require('../LoggingEvent');
 
const DELIMITER = '__LOG4JS__';
 
exports.configure = (config) => {
  debug('configure called with ', config);
  // dummy shutdown if we're not master
  let shutdown = (cb) => { cb(); };
 
  clustering.onlyOnMaster(() => {
    const server = net.createServer((socket) => {
      let dataSoFar = '';
      const send = (data) => {
        if (data) {
          dataSoFar += data;
          if (dataSoFar.indexOf(DELIMITER)) {
            const events = dataSoFar.split(DELIMITER);
            if (!dataSoFar.endsWith(DELIMITER)) {
              dataSoFar = events.pop();
            } else {
              dataSoFar = '';
            }
            events.filter(e => e.length).forEach((e) => {
              clustering.send(LoggingEvent.deserialise(e));
            });
          }
        }
      };
 
      socket.setEncoding('utf8');
      socket.on('data', send);
      socket.on('end', send);
    });
 
    server.listen(config.port || 5000, config.host || 'localhost', () => {
      debug(`listening on ${config.host || 'localhost'}:${config.port || 5000}`);
      server.unref();
    });
 
    shutdown = (cb) => {
      debug('shutdown called.');
      server.close(cb);
    };
  });
 
  return {
    shutdown
  };
};