From 328e607d32d7f1e960f8966a70097b030e386ab4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 31 Aug 2018 11:43:46 +0200 Subject: [PATCH] Add sql trace in error log on sequelize error --- server.ts | 5 ++++- server/helpers/logger.ts | 8 +++++++- server/middlewares/cache.ts | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/server.ts b/server.ts index eccd19919..2db39ab06 100644 --- a/server.ts +++ b/server.ts @@ -166,7 +166,10 @@ app.use(function (err, req, res, next) { error = err.stack || err.message || err } - logger.error('Error in controller.', { err: error }) + // Sequelize error + const sql = err.parent ? err.parent.sql : undefined + + logger.error('Error in controller.', { err: error, sql }) return res.status(err.status || 500).end() }) diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index ce6e38f15..e1e3a1fe2 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts @@ -22,7 +22,13 @@ function loggerReplacer (key: string, value: any) { } const consoleLoggerFormat = winston.format.printf(info => { - let additionalInfos = JSON.stringify(info.meta || info.err, loggerReplacer, 2) + const obj = { + meta: info.meta, + err: info.err, + sql: info.sql + } + + let additionalInfos = JSON.stringify(obj, loggerReplacer, 2) if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' else additionalInfos = ' ' + additionalInfos diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index b40486e4b..1b44957d3 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts @@ -31,6 +31,8 @@ function cacheRoute (lifetimeArg: string | number) { logger.error('Cannot cache route.', { err }) return done(err) }) + } else { + done() } return sendSave(body) @@ -52,7 +54,7 @@ function cacheRoute (lifetimeArg: string | number) { return done() }) } catch (err) { - logger.error('Cannot serve cached route.', err) + logger.error('Cannot serve cached route.', { err }) return next() } }