Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const colorize = process.env.NODE_ENV !== 'production';
// Logger to capture all requests and output them to the console.
const requestLogger = expressWinston.logger({
transports: [
new winston.transports.Console({
json: false,
colorize: colorize,
}),
],
expressFormat: true,
meta: true,
});
// Logger to capture any top-level errors and output json diagnostic info.
const errorLogger = expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: true,
colorize: colorize,
}),
],
});
export default {
requestLogger,
errorLogger,
error: winston.error,
warn: winston.warn,
info: winston.info,
log: winston.log,
verbose: winston.verbose,
});
// 开发环境中发送错误堆栈到前端并打印到控制台
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err,
});
console.error(err);
});
}
// 生产环境中保留错误日志
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: true,
colorize: true,
}),
new winston.transports.File({
filename: 'logs/error.log',
}),
],
}));
// 错误时发送邮件提醒
app.use(function(err, req, res, next) {
sendErrMailFn(err);
next(err);
});
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new APIError('API not found', httpStatus.NOT_FOUND);
return next(err);
});
// log error in winston transports except when executing test suite
if (config.env !== 'test') {
app.use(expressWinston.errorLogger({
winstonInstance
}));
}
// error handler, send stacktrace only during development
app.use((
err,
req,
res,
next // eslint-disable-line no-unused-vars
) =>
res.status(err.status).json({
message: err.isPublic ? err.message : httpStatus[err.status],
stack: config.env === 'development' ? err.stack : {}
}));
// set APP_SETTINGS=c:\dev\vpdb-backend\src\config\settings-sauce.js
// set NODE_ENV=test
// set WEBAPP=vpdb-website/dist
if (runningTest) {
if (staticWebApp && fs.existsSync(staticWebApp)) {
logger.info('[express] Serving static webapp at %s...', staticWebApp);
app.use(express.static(staticWebApp));
app.use('/*', (req, res) => res.sendfile(staticWebApp + '/index.html'));
} else if (staticWebApp) {
logger.warn('[express] Ignoring WEBAPP parameter "%", "%" does not exist.', process.env.WEBAPP, staticWebApp);
}
}
// error logger comes at the very last
app.use(expressWinston.errorLogger({
transports: [
new logger.transports.Console({ json: false, colorize: true })
]
}));
/* istanbul ignore if */
// production only
if (!runningDev) {
http.globalAgent.maxSockets = 500; // set this high, if you use httpClient or request anywhere (defaults to 5)
}
// development only
if (runningDev) {
app.use(expressErrorhandler());
app.locals.pretty = false;
}
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new APIError('API not found', httpStatus.NOT_FOUND);
return next(err);
});
// log error in winston transports except when executing test suite
if (config.env !== 'test') {
app.use(expressWinston.errorLogger({
winstonInstance
}));
}
// error handler, send stacktrace only during development
app.use((err, req, res, next) => // eslint-disable-line no-unused-vars
res.status(err.status).json({
message: err.isPublic ? err.message : httpStatus[err.status],
stack: config.env === 'development' ? err.stack : {}
})
);
export default app;
router.get("/api/v1/health", health);
router.all("/api/*", (req, res, next) => {
res.status(404).send();
});
app.use(config.publicPath, router);
/**
* Register endpoints
*/
app.use(renderer(config));
/**
* Setup express error handling
*/
app.use(
expressWinston.errorLogger({
transports: [new winston.transports.Console()],
}),
);
app.use(errorHandler(config));
}
error: function(app) {
app.use(expressWinston.errorLogger({
transports: transports
}));
},
app.use(expressWinston.logger({
transports: [
new (winston.transports.Console)({
json: true,
colorize: true
}),
new winston.transports.File({
filename: 'logs/success.log'
})
]
}));
/**
* 错误日志
*/
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
}),
new winston.transports.File({
filename: 'logs/error.log'
})
]
}));
}
}
exports.withIdentifier = (identifier) => {
if (!identifier) {
throw new Error('Identifier is required while setting up a logger. For example, pass the module name that will use this logger.');
}
return new winston.Logger({
transports: [new winston.transports.Console(_.merge({}, CONSOLE_TRANSPORT_OPTIONS, getLogFormatterOptionsWithIdentifier(identifier)))]
});
};
exports.REQUEST_LOGGER = expressWinston.logger({
transports: [new winston.transports.Console(CONSOLE_TRANSPORT_OPTIONS)]
});
exports.ERROR_LOGGER = expressWinston.errorLogger({
transports: [new winston.transports.Console(CONSOLE_TRANSPORT_OPTIONS)]
});
app.on('middleware:after:router', function addExpressWinston() {
app.use(expressWinston.errorLogger({
winstonInstance: logger._impl
}));
});