Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable writing debug logs to file by default if options.path.debug_log is undefined. #54

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 43 additions & 39 deletions modules/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ class Log {
if (typeof options.write === "undefined" || options.write === null) {
options.write = false;
} else if (typeof options.path === "undefined" || options.path === null) {
if (typeof options.debug_log === "undefined" || options.debug_log === null) {
options.debug_log = "./debug.log";
}

if (typeof options.error_log === "undefined" || options.error_log === null) {
options.error_log = "./errors.log";
}
Expand All @@ -132,13 +128,15 @@ class Log {

if (this.config.write === "enabled" || this.config.write === true) {
const pad = num => (num > 9 ? "" : "0") + num;
rfs.createStream((time, index) => {
if (!time) {
return this.config.path.debug_log;
}

return `${path.parse(this.config.path.debug_log).base.split(".")[0]}.${time.getFullYear()}${pad(time.getMonth() + 1)}${pad(time.getDate())}-${index}.${path.parse(this.config.path.debug_log).base.split(".")[1]}`;
}, this.config.rotate);
if (this.config.path.debug_log) {
rfs.createStream((time, index) => {
if (!time) {
return this.config.path.debug_log;
}

return `${path.parse(this.config.path.debug_log).base.split(".")[0]}.${time.getFullYear()}${pad(time.getMonth() + 1)}${pad(time.getDate())}-${index}.${path.parse(this.config.path.debug_log).base.split(".")[1]}`;
}, this.config.rotate);
}

rfs.createStream((time, index) => {
if (!time) {
Expand Down Expand Up @@ -187,19 +185,21 @@ class Log {
* @param {string} tag - func unique tag (optional)
*
*/
appendFile(type = "INFO", tag = "", message = "") {
appendFile(type = "INFO", tag = "", ...message) {
if (this.config.write === "enabled" || this.config.write === true) {
if (this.config.type === "log") {
if (tag !== "") {
tag = `${tag}: `;
}
let log_text = `[${this.currentTime()}] [${type.id}] ${tag}${message}\n`;
let log_text = `[${this.currentTime()}] [${type.id}] ${tag}${message.join(' ')}\n`;

fse.appendFile(this.config.path.debug_log, ansi(log_text), (err) => {
if (err) {
logger.log(err);
}
});
if (this.config.path.debug_log) {
fse.appendFile(this.config.path.debug_log, ansi(log_text), (err) => {
if (err) {
logger.log(err);
}
});
}

if (type.id === "ERROR") {
fse.appendFile(this.config.path.error_log, ansi(log_text), (err) => {
Expand Down Expand Up @@ -256,15 +256,17 @@ class Log {
* @param {string} message - error, warning or info description (mandatory)
*
*/
stdout(type = "INFO", tag = "", message = "") {
stdout(type = "INFO", tag = "", ...message) {
let time = this.TYPES_LOG.TIME;
if (tag !== "") {
if (tag !== "" && message.length < 1) {
tag = ` ${tag}`;
} else if (tag !== "" && message.length > 0) {
tag = ` ${tag}:`;
}
if (this.config.colors === "enabled" || this.config.colors === true) {
logger.log(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.bgcolor(" ")}${type.color(tag)} ${type.color(message)}`);
logger.log(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.color(tag)}`, ...message);
} else {
logger.log(ansi(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.bgcolor(" ")}${type.color(tag)} ${type.color(message)}`));
logger.log(ansi(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.color(tag)}`, ...message));
}
}

Expand All @@ -278,15 +280,17 @@ class Log {
* @param {string} message - error, warning or info description (mandatory)
*
*/
stderr(type = "ERROR", tag = "", message = "") {
stderr(type = "ERROR", tag = "", ...message) {
let time = this.TYPES_LOG.TIME;
if (tag !== "") {
if (tag !== "" && message.length < 1) {
tag = ` ${tag}`;
} else if (tag !== "" && message.length > 0) {
tag = ` ${tag}:`;
}
if (this.config.colors === "enabled" || this.config.colors === true) {
logger.error(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.bgcolor(" ")}${type.color(tag)} ${type.color(message)}`);
logger.error(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.color(tag)}`, ...message);
} else {
logger.error(ansi(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.bgcolor(" ")}${type.color(tag)} ${type.color(message)}`));
logger.error(ansi(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.currentTime()} `)}${type.color(tag)}`, ...message));
}
}

Expand All @@ -299,10 +303,10 @@ class Log {
* @param {string} tag - func unique tag (optional)
*
*/
info(message = "", tag = "") {
info(tag = "", ...message) {
if (this.config.info === "enabled" || this.config.info === true) {
this.stdout(this.TYPES_LOG.INFO, tag, `${message}`);
this.appendFile(this.TYPES_LOG.INFO, tag, message);
this.stdout(this.TYPES_LOG.INFO, tag, ...message);
this.appendFile(this.TYPES_LOG.INFO, tag, ...message);
}
}

Expand All @@ -315,7 +319,7 @@ class Log {
* @param {string} tag - func unique tag (optional)
*
*/
warning(message = "", tag = "") {
warning(tag = "", ...message) {
if (this.config.warning === "enabled" || this.config.warning === true) {
this.stdout(this.TYPES_LOG.WARNING, tag, `${message}`);
this.appendFile(this.TYPES_LOG.WARNING, tag, message);
Expand All @@ -331,10 +335,10 @@ class Log {
* @param {string} tag - func unique tag (optional)
*
*/
error(message = "", tag = "") {
error(tag = "", ...message) {
if (this.config.error === "enabled" || this.config.error === true) {
this.stderr(this.TYPES_LOG.ERROR, tag, `${message}`);
this.appendFile(this.TYPES_LOG.ERROR, tag, message);
this.stderr(this.TYPES_LOG.ERROR, tag, ...message);
this.appendFile(this.TYPES_LOG.ERROR, tag, ...message);
}
}

Expand All @@ -347,7 +351,7 @@ class Log {
* @param {string} tag - func unique tag (optional)
*
*/
debug(message = "", tag = "") {
debug(tag = "", ...message) {
if (this.config.debug === "enabled" || this.config.debug === true) {
this.stdout(this.TYPES_LOG.DEBUG, tag, `${message}`);
this.appendFile(this.TYPES_LOG.DEBUG, tag, message);
Expand Down Expand Up @@ -381,7 +385,7 @@ class Log {
* @param {string} error_message - error message to stackoverflow (optional)
*
*/
stackoverflow(message = "", tag = "", error_message = null) {
stackoverflow(tag = "", error_message = null, ...message) {
if (typeof error_message === "undefined" || error_message === null) {
error_message = message;
}
Expand All @@ -402,11 +406,11 @@ class Log {
* @param {string} tag - func unique tag (optional)
*
*/
sponsor(message = "", tag = "") {
this.stdout(this.TYPES_LOG.SPONSOR, tag, message);
this.appendFile(this.TYPES_LOG.SPONSOR, tag, message);
sponsor(tag = "", ...message) {
this.stdout(this.TYPES_LOG.SPONSOR, tag, ...message);
this.appendFile(this.TYPES_LOG.SPONSOR, tag, ...message);
}
}

module.exports = Log;
module.exports.default = Log;
module.exports.default = Log;