fix(logger): improve logging and make sure no data is lost

This commit is contained in:
Matteo Pagliazzi
2020-03-24 20:29:31 +01:00
parent 3458d89c1d
commit 2cd0ed5973
5 changed files with 130 additions and 46 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ if (CORES !== 0 && cluster.isMaster && (IS_DEV || IS_PROD)) {
cluster.on('disconnect', worker => {
const w = cluster.fork(); // replace the dead worker
logger.info('[%s] [master:%s] worker:%s disconnect! new worker:%s fork', new Date(), process.pid, worker.process.pid, w.process.pid);
logger.info(`[${new Date()}] [master:${process.pid}] worker:${worker.process.pid} disconnect! new worker:${w.process.pid} fork`);
});
} else {
module.exports = require('./server.js');
+61 -7
View File
@@ -81,7 +81,7 @@ if (IS_PROD) {
),
}))
.add(new winston.transports.Console({
level: 'info', // info messages as text
level: 'info', // text part
format: winston.format.combine(
// Ignores warn and errors
winston.format(info => {
@@ -93,9 +93,28 @@ if (IS_PROD) {
})(),
winston.format.timestamp(),
winston.format.colorize(),
winston.format.splat(),
winston.format.printf(info => `${info.timestamp} - ${info.level} ${info.message}`),
),
}))
.add(new winston.transports.Console({
level: 'info', // json part
format: winston.format.combine(
// Ignores warn and errors
winston.format(info => {
if (info.level === 'error' || info.level === 'warn') {
return false;
}
// If there are only two keys (message and level) it means there's nothing
// to print as json
if (Object.keys(info).length <= 2) return false;
return info;
})(),
winston.format.prettyPrint({
colorize: true,
}),
),
}));
} else {
_config.loggingEnabled = false;
@@ -106,7 +125,27 @@ const loggerInterface = {
info (...args) {
if (!_config.loggingEnabled) return;
logger.info(...args);
const [_message, _data] = args;
const isMessageString = typeof _message === 'string';
const message = isMessageString ? _message : 'No message provided for log.';
let data;
if (args.length === 1) {
if (isMessageString) {
data = {};
} else {
data = { extraData: _message };
}
} else if (!isMessageString || args.length > 2) {
throw new Error('logger.info accepts up to two arguments: a message and an object with extra data to log.');
} else if (_.isPlainObject(_data)) {
data = _data;
} else {
data = { extraData: _data };
}
logger.info(message, data);
},
// Accepts two argument,
@@ -115,13 +154,27 @@ const loggerInterface = {
// If the first argument isn't an Error, it'll call logger.error with all the arguments supplied
error (...args) {
if (!_config.loggingEnabled) return;
const [err, errorData = {}, ...otherArgs] = args;
const [err, _errorData] = args;
if (args.length > 2) {
throw new Error('logger.error accepts up to two arguments: an error and an object with extra data to log.');
}
let errorData = {};
if (typeof _errorData === 'string') {
errorData = { extraMessage: _errorData };
} else if (_.isPlainObject(_errorData)) {
errorData = _errorData;
} else if (_errorData) {
errorData = { extraData: _errorData };
}
if (err instanceof Error) {
// pass the error stack as the first parameter to logger.error
const stack = err.stack || err.message || err;
if (_.isPlainObject(errorData) && !errorData.fullError) {
if (!errorData.fullError) {
// If the error object has interesting data
// (not only httpCode, message and name from the CustomError class)
// add it to the logs
@@ -136,7 +189,7 @@ const loggerInterface = {
}
}
const loggerArgs = [stack, errorData, ...otherArgs];
const loggerArgs = [stack, errorData];
// Treat 4xx errors that are handled as warnings, 5xx and uncaught errors as serious problems
if (!errorData || !errorData.isHandledError || errorData.httpCode >= 500) {
@@ -145,7 +198,8 @@ const loggerInterface = {
logger.warn(...loggerArgs);
}
} else {
logger.error(...args);
errorData.invalidErr = err;
logger.error('logger.error expects an Error instance', errorData);
}
},
};
+2 -2
View File
@@ -71,9 +71,9 @@ function sendNotification (user, details = {}) {
.then(response => {
response.failed.forEach(failure => {
if (failure.error) {
logger.error('APN error', failure.error);
logger.error(new Error('APN error'), { failure });
} else {
logger.error('APN transmissionError', failure.status, notification, failure.device);
logger.error(new Error('APN transmissionError'), { failure, notification });
}
});
})
+1 -1
View File
@@ -19,7 +19,7 @@ function sendWebhook (webhook, body, user) {
retry: 3, // retry the request up to 3 times
}).catch(webhookErr => {
// Log the error
logger.error(webhookErr);
logger.error(webhookErr, 'Error while sending a webhook request.');
let _failuresReset = false;