Skip to content
Snippets Groups Projects
Commit d8a52802 authored by João Victor Tozatti Risso's avatar João Victor Tozatti Risso
Browse files

Fix runtime error when the logs directory is not found

parent 501b00e6
No related branches found
No related tags found
1 merge request!13Fix runtime error when the logs directory is not present
...@@ -2,6 +2,8 @@ const config = require('./config'); ...@@ -2,6 +2,8 @@ const config = require('./config');
const winston = require('winston'); const winston = require('winston');
const fs = require('fs');
winston.emitErrs = true; winston.emitErrs = true;
function getFilePath(module) { function getFilePath(module) {
...@@ -10,12 +12,22 @@ function getFilePath(module) { ...@@ -10,12 +12,22 @@ function getFilePath(module) {
} }
function logger(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({ const log = new winston.Logger({
transports: [ transports: [
new winston.transports.File({ new winston.transports.File({
name: 'info-log', name: 'info-log',
level: 'info', level: 'info',
filename: `${process.cwd()}/logs/all.log`, filename: `${logPath}/all.log`,
handleException: true, handleException: true,
json: false, json: false,
maxSize: 5242880, // 5MB maxSize: 5242880, // 5MB
...@@ -33,7 +45,10 @@ function logger(module) { ...@@ -33,7 +45,10 @@ function logger(module) {
], ],
exitOnError: false, exitOnError: false,
}); });
if (!config.get('debug')) {
const debugMode = (typeof config.get('debug') === 'undefined') ?
config.get('debug') : false;
if (!debugMode) {
log.remove('debug-log'); log.remove('debug-log');
} }
return log; return log;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment