2018-04-22 19:10:29 +00:00
|
|
|
/*
|
|
|
|
* Wazuh app - Module for logging functions
|
2019-01-14 16:36:47 +00:00
|
|
|
* Copyright (C) 2015-2019 Wazuh, Inc.
|
2018-04-22 19:10:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Find more information about this on the LICENSE file.
|
|
|
|
*/
|
2018-04-21 11:31:47 +00:00
|
|
|
import winston from 'winston';
|
2018-09-10 08:32:49 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2019-04-15 10:47:45 +00:00
|
|
|
import { getConfiguration } from './lib/get-configuration';
|
2018-04-21 11:31:47 +00:00
|
|
|
|
|
|
|
let allowed = false;
|
2018-07-09 08:10:47 +00:00
|
|
|
let wazuhlogger = undefined;
|
2019-04-15 10:47:45 +00:00
|
|
|
let wazuhPlainLogger = undefined;
|
2018-07-09 08:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-15 10:47:45 +00:00
|
|
|
* Here we create the loggers
|
2018-07-09 08:10:47 +00:00
|
|
|
*/
|
|
|
|
const initLogger = () => {
|
2019-04-15 10:47:45 +00:00
|
|
|
const configurationFile = getConfiguration();
|
|
|
|
const level =
|
|
|
|
typeof (configurationFile || {})['logs.level'] !== 'undefined' &&
|
|
|
|
['info', 'debug'].includes(configurationFile['logs.level'])
|
|
|
|
? configurationFile['logs.level']
|
|
|
|
: 'info';
|
|
|
|
|
|
|
|
// JSON logger
|
2018-09-10 08:32:49 +00:00
|
|
|
wazuhlogger = winston.createLogger({
|
2019-04-15 10:47:45 +00:00
|
|
|
level,
|
2018-09-10 08:32:49 +00:00
|
|
|
format: winston.format.json(),
|
|
|
|
transports: [
|
|
|
|
new winston.transports.File({
|
|
|
|
filename: path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../optimize/wazuh-logs/wazuhapp.log'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2019-04-15 10:47:45 +00:00
|
|
|
// Prevents from exit on error related to the logger.
|
2018-09-10 08:32:49 +00:00
|
|
|
wazuhlogger.exitOnError = false;
|
2019-04-15 10:47:45 +00:00
|
|
|
|
|
|
|
// Plain text logger
|
|
|
|
wazuhPlainLogger = winston.createLogger({
|
|
|
|
level,
|
|
|
|
format: winston.format.simple(),
|
|
|
|
transports: [
|
|
|
|
new winston.transports.File({
|
|
|
|
filename: path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../optimize/wazuh-logs/wazuhapp-plain.log'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
// Prevents from exit on error related to the logger.
|
|
|
|
wazuhPlainLogger.exitOnError = false;
|
2018-07-09 08:10:47 +00:00
|
|
|
};
|
2018-04-21 11:31:47 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
/**
|
2018-06-12 10:45:53 +00:00
|
|
|
* Checks if wazuh-logs exists. If it doesn't exist, it will be created.
|
2018-03-12 12:43:43 +00:00
|
|
|
*/
|
2018-07-09 08:10:47 +00:00
|
|
|
const initDirectory = async () => {
|
2018-09-10 08:32:49 +00:00
|
|
|
try {
|
|
|
|
if (!fs.existsSync(path.join(__dirname, '../../../optimize/wazuh-logs'))) {
|
|
|
|
fs.mkdirSync(path.join(__dirname, '../../../optimize/wazuh-logs'));
|
2018-03-12 12:40:31 +00:00
|
|
|
}
|
2019-04-15 10:47:45 +00:00
|
|
|
if (
|
|
|
|
typeof wazuhlogger === 'undefined' ||
|
|
|
|
typeof wazuhPlainLogger === 'undefined'
|
|
|
|
) {
|
|
|
|
initLogger();
|
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
allowed = true;
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
allowed = false;
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
};
|
2018-03-12 11:29:24 +00:00
|
|
|
|
2018-03-12 12:43:43 +00:00
|
|
|
/**
|
|
|
|
* Returns given file size in MB, if the file doesn't exist returns 0
|
|
|
|
* @param {*} filename Path to the file
|
|
|
|
*/
|
2018-03-12 11:29:24 +00:00
|
|
|
const getFilesizeInMegaBytes = filename => {
|
2018-09-10 08:32:49 +00:00
|
|
|
if (allowed) {
|
|
|
|
if (fs.existsSync(filename)) {
|
|
|
|
const stats = fs.statSync(filename);
|
|
|
|
const fileSizeInMegaBytes = stats.size;
|
2018-03-12 11:57:14 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
return fileSizeInMegaBytes / 1000000.0;
|
2018-03-12 11:29:24 +00:00
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
2018-03-12 11:29:24 +00:00
|
|
|
|
2018-04-22 19:10:29 +00:00
|
|
|
/**
|
2018-03-12 12:43:43 +00:00
|
|
|
* Checks if the wazuhapp.log file size is greater than 100MB, if so it rotates the file.
|
|
|
|
*/
|
2018-03-12 11:29:24 +00:00
|
|
|
const checkFiles = () => {
|
2018-09-10 08:32:49 +00:00
|
|
|
if (allowed) {
|
|
|
|
if (
|
|
|
|
getFilesizeInMegaBytes(
|
|
|
|
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp.log')
|
|
|
|
) >= 100
|
|
|
|
) {
|
|
|
|
fs.renameSync(
|
|
|
|
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp.log'),
|
|
|
|
path.join(
|
|
|
|
__dirname,
|
|
|
|
`../../../optimize/wazuh-logs/wazuhapp.${new Date().getTime()}.log`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp.log'),
|
|
|
|
JSON.stringify({
|
|
|
|
date: new Date(),
|
|
|
|
level: 'info',
|
|
|
|
location: 'logger',
|
|
|
|
message: 'Rotated log file'
|
|
|
|
}) + '\n'
|
|
|
|
);
|
2018-03-12 11:29:24 +00:00
|
|
|
}
|
2019-04-15 10:47:45 +00:00
|
|
|
if (
|
|
|
|
getFilesizeInMegaBytes(
|
|
|
|
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp-plain.log')
|
|
|
|
) >= 100
|
|
|
|
) {
|
|
|
|
fs.renameSync(
|
|
|
|
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp-plain.log'),
|
|
|
|
path.join(
|
|
|
|
__dirname,
|
|
|
|
`../../../optimize/wazuh-logs/wazuhapp-plain.${new Date().getTime()}.log`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2018-03-12 11:29:24 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 10:47:45 +00:00
|
|
|
const yyyymmdd = () => {
|
|
|
|
const now = new Date();
|
|
|
|
const y = now.getFullYear();
|
|
|
|
const m = now.getMonth() + 1;
|
|
|
|
const d = now.getDate();
|
|
|
|
const seconds = now.getSeconds();
|
|
|
|
const minutes = now.getMinutes();
|
|
|
|
const hour = now.getHours();
|
|
|
|
return `${y}/${m < 10 ? '0' : ''}${m}/${
|
|
|
|
d < 10 ? '0' : ''
|
|
|
|
}${d} ${hour}:${minutes}:${seconds}`;
|
|
|
|
};
|
|
|
|
|
2018-03-12 12:43:43 +00:00
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
*/
|
2018-09-03 09:46:55 +00:00
|
|
|
export function log(location, message, level) {
|
2018-09-10 08:32:49 +00:00
|
|
|
initDirectory()
|
2018-07-09 08:10:47 +00:00
|
|
|
.then(() => {
|
2018-09-10 08:32:49 +00:00
|
|
|
if (allowed) {
|
|
|
|
checkFiles();
|
|
|
|
wazuhlogger.log({
|
|
|
|
date: new Date(),
|
|
|
|
level: level || 'error',
|
2019-03-27 00:02:12 +00:00
|
|
|
location: location || 'Unknown origin',
|
2018-09-10 08:32:49 +00:00
|
|
|
message: message || 'An error occurred'
|
|
|
|
});
|
2019-04-15 10:47:45 +00:00
|
|
|
try {
|
|
|
|
wazuhPlainLogger.log({
|
|
|
|
level: level || 'error',
|
|
|
|
message: `${yyyymmdd()}: ${location ||
|
|
|
|
'Unknown origin'}: ${message || 'An error occurred'}`
|
|
|
|
});
|
|
|
|
} catch (error) {} // eslint-disable-line
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2018-07-09 08:10:47 +00:00
|
|
|
})
|
2018-09-10 08:32:49 +00:00
|
|
|
.catch(error =>
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.error(
|
|
|
|
`Cannot create the logs directory due to:\n${error.message || error}`
|
|
|
|
)
|
2018-09-10 08:42:05 +00:00
|
|
|
);
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|