server: Use a log4js logger object for logging

pull/4740/head
Richard Hansen 2021-02-08 23:38:26 -05:00 committed by John McLear
parent cb1116607e
commit 105f8b0ccb
1 changed files with 20 additions and 18 deletions

View File

@ -49,6 +49,8 @@ const plugins = require('../static/js/pluginfw/plugins');
const settings = require('./utils/Settings'); const settings = require('./utils/Settings');
const util = require('util'); const util = require('util');
const logger = log4js.getLogger('server');
const State = { const State = {
INITIAL: 1, INITIAL: 1,
STARTING: 2, STARTING: 2,
@ -62,9 +64,9 @@ const State = {
let state = State.INITIAL; let state = State.INITIAL;
const removeSignalListener = (signal, listener) => { const removeSignalListener = (signal, listener) => {
console.debug(`Removing ${signal} listener because it might interfere with shutdown tasks. ` + logger.debug(`Removing ${signal} listener because it might interfere with shutdown tasks. ` +
`Function code:\n${listener.toString()}\n` + `Function code:\n${listener.toString()}\n` +
`Current stack:\n${(new Error()).stack.split('\n').slice(1).join('\n')}`); `Current stack:\n${(new Error()).stack.split('\n').slice(1).join('\n')}`);
process.off(signal, listener); process.off(signal, listener);
}; };
@ -86,7 +88,7 @@ exports.start = async () => {
default: default:
throw new Error(`unknown State: ${state.toString()}`); throw new Error(`unknown State: ${state.toString()}`);
} }
console.log('Starting Etherpad...'); logger.info('Starting Etherpad...');
state = State.STARTING; state = State.STARTING;
// Check if Etherpad version is up-to-date // Check if Etherpad version is up-to-date
@ -125,13 +127,13 @@ exports.start = async () => {
.filter((plugin) => plugin.package.name !== 'ep_etherpad-lite') .filter((plugin) => plugin.package.name !== 'ep_etherpad-lite')
.map((plugin) => `${plugin.package.name}@${plugin.package.version}`) .map((plugin) => `${plugin.package.name}@${plugin.package.version}`)
.join(', '); .join(', ');
console.info(`Installed plugins: ${installedPlugins}`); logger.info(`Installed plugins: ${installedPlugins}`);
console.debug(`Installed parts:\n${plugins.formatParts()}`); logger.debug(`Installed parts:\n${plugins.formatParts()}`);
console.debug(`Installed hooks:\n${plugins.formatHooks()}`); logger.debug(`Installed hooks:\n${plugins.formatHooks()}`);
await hooks.aCallAll('loadSettings', {settings}); await hooks.aCallAll('loadSettings', {settings});
await hooks.aCallAll('createServer'); await hooks.aCallAll('createServer');
console.log('Etherpad is running'); logger.info('Etherpad is running');
state = State.RUNNING; state = State.RUNNING;
while (runningCallbacks.length > 0) setImmediate(runningCallbacks.pop()); while (runningCallbacks.length > 0) setImmediate(runningCallbacks.pop());
@ -159,7 +161,7 @@ exports.stop = async () => {
default: default:
throw new Error(`unknown State: ${state.toString()}`); throw new Error(`unknown State: ${state.toString()}`);
} }
console.log('Stopping Etherpad...'); logger.info('Stopping Etherpad...');
state = State.STOPPING; state = State.STOPPING;
let timeout = null; let timeout = null;
await Promise.race([ await Promise.race([
@ -169,7 +171,7 @@ exports.stop = async () => {
}), }),
]); ]);
clearTimeout(timeout); clearTimeout(timeout);
console.log('Etherpad stopped'); logger.info('Etherpad stopped');
state = State.STOPPED; state = State.STOPPED;
while (stoppedCallbacks.length > 0) setImmediate(stoppedCallbacks.pop()); while (stoppedCallbacks.length > 0) setImmediate(stoppedCallbacks.pop());
}; };
@ -180,13 +182,13 @@ exports.exit = async (err = null) => {
/* eslint-disable no-process-exit */ /* eslint-disable no-process-exit */
if (err === 'SIGTERM') { if (err === 'SIGTERM') {
// Termination from SIGTERM is not treated as an abnormal termination. // Termination from SIGTERM is not treated as an abnormal termination.
console.log('Received SIGTERM signal'); logger.info('Received SIGTERM signal');
err = null; err = null;
} else if (err != null) { } else if (err != null) {
console.error(err.stack || err.toString()); logger.error(err.stack || err.toString());
process.exitCode = 1; process.exitCode = 1;
if (exitCalled) { if (exitCalled) {
console.error('Error occurred while waiting to exit. Forcing an immediate unclean exit...'); logger.error('Error occurred while waiting to exit. Forcing an immediate unclean exit...');
process.exit(1); process.exit(1);
} }
} }
@ -211,20 +213,20 @@ exports.exit = async (err = null) => {
default: default:
throw new Error(`unknown State: ${state.toString()}`); throw new Error(`unknown State: ${state.toString()}`);
} }
console.log('Exiting...'); logger.info('Exiting...');
state = State.EXITING; state = State.EXITING;
while (exitCallbacks.length > 0) setImmediate(exitCallbacks.pop()); while (exitCallbacks.length > 0) setImmediate(exitCallbacks.pop());
// Node.js should exit on its own without further action. Add a timeout to force Node.js to exit // Node.js should exit on its own without further action. Add a timeout to force Node.js to exit
// just in case something failed to get cleaned up during the shutdown hook. unref() is called on // just in case something failed to get cleaned up during the shutdown hook. unref() is called on
// the timeout so that the timeout itself does not prevent Node.js from exiting. // the timeout so that the timeout itself does not prevent Node.js from exiting.
setTimeout(() => { setTimeout(() => {
console.error('Something that should have been cleaned up during the shutdown hook (such as ' + logger.error('Something that should have been cleaned up during the shutdown hook (such as ' +
'a timer, worker thread, or open connection) is preventing Node.js from exiting'); 'a timer, worker thread, or open connection) is preventing Node.js from exiting');
wtfnode.dump(); wtfnode.dump();
console.error('Forcing an unclean exit...'); logger.error('Forcing an unclean exit...');
process.exit(1); process.exit(1);
}, 5000).unref(); }, 5000).unref();
console.log('Waiting for Node.js to exit...'); logger.info('Waiting for Node.js to exit...');
state = State.WAITING_FOR_EXIT; state = State.WAITING_FOR_EXIT;
/* eslint-enable no-process-exit */ /* eslint-enable no-process-exit */
}; };