mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
adds logger and starts logging errors
This commit is contained in:
@@ -83,7 +83,7 @@
|
|||||||
"swagger-node-express": "lefnire/swagger-node-express#habitrpg",
|
"swagger-node-express": "lefnire/swagger-node-express#habitrpg",
|
||||||
"universal-analytics": "~0.3.2",
|
"universal-analytics": "~0.3.2",
|
||||||
"validator": "~3.19.0",
|
"validator": "~3.19.0",
|
||||||
"winston": "~0.8.0",
|
"winston": "~2.0.1",
|
||||||
"winston-mail": "~0.2.9",
|
"winston-mail": "~0.2.9",
|
||||||
"winston-newrelic": "~0.1.4"
|
"winston-newrelic": "~0.1.4"
|
||||||
},
|
},
|
||||||
|
|||||||
21
website/src/libs/api-v3/logger.js
Normal file
21
website/src/libs/api-v3/logger.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Logger utility
|
||||||
|
// TODO remove winston-mail and winston-newrelic if not used
|
||||||
|
let winston = require('winston');
|
||||||
|
let nconf = require('nconf');
|
||||||
|
|
||||||
|
// TODO use const?
|
||||||
|
// TODO move isProd to a single location
|
||||||
|
let isProd = nconf.get('NODE_ENV') === 'production';
|
||||||
|
|
||||||
|
let logger = new winston.Logger();
|
||||||
|
|
||||||
|
if (isProd) {
|
||||||
|
// TODO production logging
|
||||||
|
} else {
|
||||||
|
logger
|
||||||
|
.add(winston.transports.Console);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = logger;
|
||||||
@@ -2,13 +2,19 @@
|
|||||||
|
|
||||||
// The error handler middleware that handles all errors
|
// The error handler middleware that handles all errors
|
||||||
// and respond to the client
|
// and respond to the client
|
||||||
|
let logger = require('../../libs/api-v3/logger');
|
||||||
let errors = require('../../libs/api-v3/errors');
|
let errors = require('../../libs/api-v3/errors');
|
||||||
let CustomError = errors.CustomError;
|
let CustomError = errors.CustomError;
|
||||||
let InternalServerError = errors.InternalServerError;
|
let InternalServerError = errors.InternalServerError;
|
||||||
|
|
||||||
module.exports = function (err, req, res, next) {
|
module.exports = function (err, req, res, next) {
|
||||||
// TODO add logging
|
// Log the original error with some metadata
|
||||||
|
let stack = err.stack || err.message || err;
|
||||||
|
logging.error(stack, {
|
||||||
|
originalUrl: req.originalUrl,
|
||||||
|
headers: req.headers,
|
||||||
|
body: req.body
|
||||||
|
});
|
||||||
|
|
||||||
// In case of a CustomError class, use it's data
|
// In case of a CustomError class, use it's data
|
||||||
// Otherwise try to identify the type of error (mongoose validation, mongodb unique, ...)
|
// Otherwise try to identify the type of error (mongoose validation, mongodb unique, ...)
|
||||||
|
|||||||
Reference in New Issue
Block a user