mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
feat(logging): improve logging removing not useful data and distinguishing between user and server errors
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
import winston from 'winston';
|
||||
import nconf from 'nconf';
|
||||
import _ from 'lodash';
|
||||
import {
|
||||
CustomError,
|
||||
} from './errors';
|
||||
|
||||
const IS_PROD = nconf.get('IS_PROD');
|
||||
const IS_TEST = nconf.get('IS_TEST');
|
||||
@@ -42,8 +45,27 @@ let loggerInterface = {
|
||||
// pass the error stack as the first parameter to logger.error
|
||||
let stack = err.stack || err.message || err;
|
||||
|
||||
if (_.isPlainObject(errorData) && !errorData.fullError) errorData.fullError = err;
|
||||
logger.error(stack, errorData, ...otherArgs);
|
||||
if (_.isPlainObject(errorData) && !errorData.fullError) {
|
||||
// If the error object has interesting data (not only httpCode, message and name from the CustomError class)
|
||||
// add it to the logs
|
||||
if (err instanceof CustomError) {
|
||||
let errWithoutCommonProps = _.omit(err, ['name', 'httpCode', 'message']);
|
||||
|
||||
if (Object.keys(errWithoutCommonProps).length > 0) {
|
||||
errorData.fullError = errWithoutCommonProps;
|
||||
}
|
||||
} else {
|
||||
errorData.fullError = err;
|
||||
}
|
||||
}
|
||||
|
||||
let loggerArgs = [stack, errorData, ...otherArgs];
|
||||
// Treat 4xx errors as warnings, 5xx and uncaught errors as serious problems
|
||||
if (!errorData || !errorData.statusCode || errorData.statusCode > 499) {
|
||||
logger.error(...loggerArgs);
|
||||
} else {
|
||||
logger.warn(stack, errorData, ...otherArgs);
|
||||
}
|
||||
} else {
|
||||
logger.error(...args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user