From 790dc3b31ecf571a124e4360af097c34430b3cc3 Mon Sep 17 00:00:00 2001 From: apostolof Date: Sun, 14 Feb 2021 17:23:07 +0200 Subject: [PATCH] refactor: add console logging for exceptions --- .../src/logging/node/winstonLogUtils.js | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/packages/concordia-shared/src/logging/node/winstonLogUtils.js b/packages/concordia-shared/src/logging/node/winstonLogUtils.js index 46163ab..e637748 100644 --- a/packages/concordia-shared/src/logging/node/winstonLogUtils.js +++ b/packages/concordia-shared/src/logging/node/winstonLogUtils.js @@ -1,29 +1,30 @@ const path = require('path'); const getLogger = (winston, logsDirectory, serviceName) => { - const logger = winston.createLogger({ - level: process.env.NODE_ENV === 'production' ? 'info' : 'silly', - format: winston.format.combine( - winston.format.timestamp(), - winston.format.json(), - ), - defaultMeta: { service: serviceName }, - transports: [ - new winston.transports.File({ - filename: path.join(logsDirectory, 'error.log'), - level: 'error', - }), - new winston.transports.File({ - filename: path.join(logsDirectory, 'combined.log'), - }), - ], - exceptionHandlers: [ - new winston.transports.File({ filename: path.join(logsDirectory, 'exceptions.log') }), - ], - }); + const transports = [ + new winston.transports.File({ + filename: path.join(logsDirectory, 'error.log'), + level: 'error', + }), + new winston.transports.File({ + filename: path.join(logsDirectory, 'combined.log'), + }), + ]; + + const exceptionHandlers = [ + new winston.transports.File({ filename: path.join(logsDirectory, 'exceptions.log') }), + ]; if (process.env.NODE_ENV !== 'production') { - logger.add(new winston.transports.Console({ + transports.push(new winston.transports.Console({ + format: winston.format.combine( + winston.format.colorize(), + winston.format.timestamp(), + winston.format.simple(), + ), + })); + + exceptionHandlers.push(new winston.transports.Console({ format: winston.format.combine( winston.format.colorize(), winston.format.timestamp(), @@ -32,7 +33,16 @@ const getLogger = (winston, logsDirectory, serviceName) => { })); } - return logger; + return winston.createLogger({ + level: process.env.NODE_ENV === 'production' ? 'info' : 'silly', + format: winston.format.combine( + winston.format.timestamp(), + winston.format.json(), + ), + defaultMeta: { service: serviceName }, + transports, + exceptionHandlers, + }); }; module.exports = getLogger;