source: trip-planner-front/node_modules/log4js/lib/appenders/tcp.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.1 KB
Line 
1
2
3const debug = require('debug')('log4js:tcp');
4const net = require('net');
5
6function appender(config, layout) {
7 let canWrite = false;
8 const buffer = [];
9 let socket;
10 let shutdownAttempts = 3;
11 let endMsg = '__LOG4JS__';
12
13 function write(loggingEvent) {
14 debug('Writing log event to socket');
15 canWrite = socket.write(`${layout(loggingEvent)}${endMsg}`, 'utf8');
16 }
17
18 function emptyBuffer() {
19 let evt;
20 debug('emptying buffer');
21 /* eslint no-cond-assign:0 */
22 while ((evt = buffer.shift())) {
23 write(evt);
24 }
25 }
26
27 function createSocket() {
28 debug(`appender creating socket to ${config.host || 'localhost'}:${config.port || 5000}`);
29 endMsg = `${config.endMsg || '__LOG4JS__'}`;
30 socket = net.createConnection(config.port || 5000, config.host || 'localhost');
31 socket.on('connect', () => {
32 debug('socket connected');
33 emptyBuffer();
34 canWrite = true;
35 });
36 socket.on('drain', () => {
37 debug('drain event received, emptying buffer');
38 canWrite = true;
39 emptyBuffer();
40 });
41 socket.on('timeout', socket.end.bind(socket));
42 // don't bother listening for 'error', 'close' gets called after that anyway
43 socket.on('close', createSocket);
44 }
45
46 createSocket();
47
48 function log(loggingEvent) {
49 if (canWrite) {
50 write(loggingEvent);
51 } else {
52 debug('buffering log event because it cannot write at the moment');
53 buffer.push(loggingEvent);
54 }
55 }
56
57 log.shutdown = function (cb) {
58 debug('shutdown called');
59 if (buffer.length && shutdownAttempts) {
60 debug('buffer has items, waiting 100ms to empty');
61 shutdownAttempts -= 1;
62 setTimeout(() => {
63 log.shutdown(cb);
64 }, 100);
65 } else {
66 socket.removeAllListeners('close');
67 socket.end(cb);
68 }
69 };
70 return log;
71}
72
73function configure(config, layouts) {
74 debug(`configure with config = ${config}`);
75 let layout = function (loggingEvent) {
76 return loggingEvent.serialise();
77 };
78 if (config.layout) {
79 layout = layouts.layout(config.layout.type, config.layout);
80 }
81 return appender(config, layout);
82}
83
84module.exports.configure = configure;
Note: See TracBrowser for help on using the repository browser.