From d8a52802e6b62e093854ec084029b00014e3befd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Tozatti=20Risso?= <jvtr12@inf.ufpr.br> Date: Wed, 14 Sep 2016 13:27:02 -0300 Subject: [PATCH] Fix runtime error when the logs directory is not found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Victor Tozatti Risso <jvtr12@inf.ufpr.br> --- src/libs/log.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/libs/log.js b/src/libs/log.js index ecf6df02..befd5ad3 100644 --- a/src/libs/log.js +++ b/src/libs/log.js @@ -2,6 +2,8 @@ const config = require('./config'); const winston = require('winston'); +const fs = require('fs'); + winston.emitErrs = true; function getFilePath(module) { @@ -10,12 +12,22 @@ function getFilePath(module) { } function logger(module) { + const logPath = `${process.cwd()}/logs`; + /* Check if logs directory exists */ + try { + fs.accessSync(logPath, fs.F_OK, () => {}); + } catch (err) { + // if logs directory does not exist, create it. + fs.mkdirSync(logPath, 0o700, (dirErr) => { + throw new Error(`Failed to create logs directory!\n\tError: ${dirErr}`); + }); + } const log = new winston.Logger({ transports: [ new winston.transports.File({ name: 'info-log', level: 'info', - filename: `${process.cwd()}/logs/all.log`, + filename: `${logPath}/all.log`, handleException: true, json: false, maxSize: 5242880, // 5MB @@ -33,7 +45,10 @@ function logger(module) { ], exitOnError: false, }); - if (!config.get('debug')) { + + const debugMode = (typeof config.get('debug') === 'undefined') ? + config.get('debug') : false; + if (!debugMode) { log.remove('debug-log'); } return log; -- GitLab