<commit_before>'use strict';

/**
 * ButtressJS - Realtime datastore for software
 *
 * @file logging.js
 * @description Logging helpers
 * @module System
 * @author <NAME>
 *
 * @todo Centralise this logging across the app
 * @todo Add other log targets ie to datastore
 *
 */

const cluster = require('cluster');
const proxyquire = require('proxyquire');
const winston = require('winston');
proxyquire('winston-logrotate', {
	winston: winston,
});
const Config = require('node-env-obj')('../');

/**
 *
 * @type {{ERR: string, WARN: string, INFO: string, VERBOSE: string, DEBUG: string, SILLY: string, DEFAULT: string}}
 */
const LogLevel = {
	ERR: 'error',
	WARN: 'warn',
	INFO: 'info',
	VERBOSE: 'verbose',
	DEBUG: 'debug',
	SILLY: 'silly',
	DEFAULT: 'info',
};

module.exports.Constants = {
	LogLevel: LogLevel,
};

let _logApp = 'app';
let _logLabel = 'MASTER';
const setLogApp = (app) => {
	_logApp = app;
	if (cluster.isWorker) {
		_logLabel = `${cluster.worker.id}`;
	}
};
module.exports.setLogApp = setLogApp;

/**
 * @param {String} logApp - Log applcation label (rest / socket)
 */
module.exports.init = (logApp) => {
	setLogApp(logApp);

	winston.remove(winston.transports.Console);
	winston.add(winston.transports.Console, {
		name: 'console',
		colorize: 'all',
		timestamp: true,
		level: 'info',
	});

	winston.add(winston.transports.Rotate, {
		name: 'debug-file',
		json: false,
		file: `${Config.paths.logs}/log-${_logApp}-debug.log`,
		level: 'debug',
		size: '1m',
		keep: 2,
		colorize: 'all',
		timestamp: true,
	});
	winston.add(winston.transports.Rotate, {
		name: 'info-file',
		json: false,
		file: `${Config.paths.logs}/log-${_logApp}-info.log`,
		size: '1m',
		keep: 5,
		colorize: 'all',
		level: 'info',
		timestamp: true,
	});
	winston.add(winston.transports.Rotate, {
		name: 'error-file',
		json: false,
		file: `${Config.paths.logs}/log-${_logApp}-err.log`,
		size: '1m',
		keep: 10,
		level: 'error',
		colorize: 'none',
		timestamp: true,
	});
	winston.add(winston.transports.Rotate, {
		name: 'silly-file',
		json: false,
		file: `${Config.paths.logs}/log-${_logApp}-silly.log`,
		level: 'silly',
		size: '1m',
		keep: 1,
		colorize: 'all',
		timestamp: true,
	});
	winston.addColors({
		info: 'white',
		error: 'red',
		warn: 'yellow',
		verbose: 'white',
		debug: 'white',
	});
};

/**
 *
 * @param {string} log - log entry
 * @param {string} level - level to log at
 * @private
 */
function _log(log, level) {
	winston.log(level, `[${_logLabel}]: ${log}`);
	// if (typeof log === 'string') {
	//   winston.log(level, log);
	// } else {
	//   winston.log(level, '', log);
	// }
}

/**
 * STANDARD LOGGING
 */

module.exports.setLogLevel = (level) => {
	winston.level = level;
	// _logLevel = level;
};

/**
 * @param {string} log - Text to log<commit_msg>PART: Upgraded from winston 2 -> 3, the logging needs to be refactored<commit_after>'use strict';

/**
 * ButtressJS - Realtime datastore for software
 *
 * @file logging.js
 * @description Logging helpers
 * @module System
 * @author <NAME>
 *
 * @todo Centralise this logging across the app
 * @todo Add other log targets ie to datastore
 *
 */

const cluster = require('cluster');
const winston = require('winston');
const Config = require('node-env-obj')('../');

/**
 *
 * @type {{ERR: string, WARN: string, INFO: string, VERBOSE: string, DEBUG: string, SILLY: string, DEFAULT: string}}
 */
const LogLevel = {
	ERR: 'error',
	WARN: 'warn',
	INFO: 'info',
	VERBOSE: 'verbose',
	DEBUG: 'debug',
	SILLY: 'silly',
	DEFAULT: 'info',
};

module.exports.Constants = {
	LogLevel: LogLevel,
};

let _logApp = 'app';
let _logProcess = 'MASTER';
const setLogApp = (app) => {
	_logApp = app;
	if (cluster.isWorker) {
		_logProcess = `${cluster.worker.id}`;
	}
};
module.exports.setLogApp = setLogApp;

/**
 * @param {String} logApp - Log applcation label (rest / socket)
 */
module.exports.init = (logApp) => {
	setLogApp(logApp);

	// winston.remove(winston.transports.Console);
	winston.add(new winston.transports.Console({
		level: 'debug',
		format: winston.format.combine(
			winston.format.colorize(),
			winston.format.timestamp(),
			winston.format.errors({stack: true}),
			winston.format.printf((info) => {
				if (info.stack) {
					return `${info.timestamp} [${_logProcess}] ${info.level}: ${info.message}\n${info.stack}`;
				}

				return `${info.timestamp} [${_logProcess}] ${info.level}: ${info.message}`;
			})
		),
	}));

	// winston.remove(winston.transports.Console);
	// winston.add(winston.transports.Console, {
	// 	name: 'console',
	// 	colorize: 'all',
	// 	timestamp: true,
	// 	level: 'info',
	// });

	// winston.add(winston.transports.Rotate, {
	// 	name: 'debug-file',
	// 	json: false,
	// 	file: `${Config.paths.logs}/log-${_logApp}-debug.log`,
	// 	level: 'debug',
	// 	size: '1m',
	// 	keep: 2,
	// 	colorize: 'all',
	// 	timestamp: true,
	// });
	// winston.add(winston.transports.Rotate, {
	// 	name: 'info-file',
	// 	json: false,
	// 	file: `${Config.paths.logs}/log-${_logApp}-info.log`,
	// 	size: '1m',
	// 	keep: 5,
	// 	colorize: 'all',
	// 	level: 'info',
	// 	timestamp: true,
	// });
	// winston.add(winston.transports.Rotate, {
	// 	name: 'error-file',
	// 	json: false,
	// 	file: `${Config.paths.logs}/log-${_logApp}-err.log`,
	// 	size: '1m',
	// 	keep: 10,
	// 	level: 'error',
	// 	colorize: 'none',
	// 	timestamp: true,
	// });
	// winston.add(winston.transports.Rotate, {
	// 	name: 'silly-file',
	// 	json: false,
	// 	file: `${Config.paths.logs}/log-${_logApp}-silly.log`,
	// 	level: 'silly',
	// 	size: '1m',
	// 	keep: 1,
	// 	colorize: 'all',
	// 	timestamp: true,
	// });
	// winston.addColors({
	// 	info: 'white',
	// 	error: 'red',
	// 	warn: 'yellow',
	// 	verbose: 'white',
	// 	debug: 'white',
	// });
};

/**
 *
 * @param {string} log - log entry
 * @param {string} level - level to log at
 * @private
 */
function _log(log, level) {
	winston.log({
		level: level,
		message: log,
	});
}

/**
 * STANDARD LOGGING
 */

module.exports.setLogLevel = (level) => {
	winston.level = level;
	// _logLevel = level;
};

/**
 * @param {string} log - Text to log
{'fraction_non_alphanumeric': 0.20772630545097398, 'fraction_numerical': 0.003273858241938124, 'mean_word_length': 3.3333333333333335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460665', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2400, 'n_words': 499}