Fix typo on path

This commit is contained in:
Jesús Ángel González 2018-03-12 13:43:43 +01:00 committed by Javier Castro
parent e8cb3a630c
commit fe23ae9fcc

View File

@ -2,6 +2,9 @@ const winston = require('winston');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
/**
* Checks if /var/log/wazuh exists on linux systems. If it doesn't exist, it will be created.
*/
const initDirectory = () => { const initDirectory = () => {
if (!fs.existsSync('/var/log/wazuh') && process.platform === 'linux') { if (!fs.existsSync('/var/log/wazuh') && process.platform === 'linux') {
fs.mkdirSync('/var/log/wazuh'); fs.mkdirSync('/var/log/wazuh');
@ -9,6 +12,9 @@ const initDirectory = () => {
return; return;
} }
/**
* Here we create the logger
*/
const wazuhlogger = winston.createLogger({ const wazuhlogger = winston.createLogger({
level : 'info', level : 'info',
format : winston.format.json(), format : winston.format.json(),
@ -19,8 +25,15 @@ const wazuhlogger = winston.createLogger({
] ]
}); });
/**
* Prevents from exit on error related to the logger.
*/
wazuhlogger.exitOnError = false; wazuhlogger.exitOnError = false;
/**
* Returns given file size in MB, if the file doesn't exist returns 0
* @param {*} filename Path to the file
*/
const getFilesizeInMegaBytes = filename => { const getFilesizeInMegaBytes = filename => {
if (fs.existsSync(filename)) { if (fs.existsSync(filename)) {
const stats = fs.statSync(filename) const stats = fs.statSync(filename)
@ -31,8 +44,11 @@ const getFilesizeInMegaBytes = filename => {
return 0; return 0;
} }
/**
* Checks if the wazuhapp.log file size is greater than 100MB, if so it rotates the file.
*/
const checkFiles = () => { const checkFiles = () => {
if (getFilesizeInMegaBytes(path.join(__dirname, '../../error.log')) >= 100) { if (getFilesizeInMegaBytes(process.platform === 'linux' ? '/var/log/wazuh/wazuhapp.log' : path.join(__dirname, '../../wazuhapp.log')) >= 100) {
fs.renameSync( fs.renameSync(
process.platform === 'linux' ? '/var/log/wazuh/wazuhapp.log' : path.join(__dirname, '../../wazuhapp.log'), process.platform === 'linux' ? '/var/log/wazuh/wazuhapp.log' : path.join(__dirname, '../../wazuhapp.log'),
process.platform === 'linux' ? '/var/log/wazuh/wazuhapp.log' : path.join(__dirname, `../../wazuhapp.${new Date().getTime()}.log`) process.platform === 'linux' ? '/var/log/wazuh/wazuhapp.log' : path.join(__dirname, `../../wazuhapp.${new Date().getTime()}.log`)
@ -40,6 +56,12 @@ const checkFiles = () => {
} }
}; };
/**
* Main function to add a new log
* @param {*} location File where the log is being thrown
* @param {*} message Message to show
* @param {*} level Optional, default is 'error'
*/
const log = (location, message, level) => { const log = (location, message, level) => {
initDirectory(); initDirectory();
checkFiles(); checkFiles();