diff --git a/src/libs/log.js b/src/libs/log.js index ecf6df026314105b02628ef0ac2736a23950374a..befd5ad326ade75fcbe0e0e1749d0d5360aa9da7 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;