UNPKG

flexiblelog

Version:

Powerfull log system, with email, file saving, level managment

132 lines (115 loc) 3.84 kB
var fs = require("fs"); class logger { constructor(data) { this.consoleLog = data.consoleLog || true; this.fileLog = data.fileLog || false; this.emailNotif = data.emailNotif || false; if (this.emailNotif) { if (typeof data.email != undefined && typeof data.senderEmail != undefined && data.senderEmail.constructor.name === "Mail") { this.senderEmail = data.senderEmail; this.email = data.email; this.title = data.title || "Your server has an error"; } else { throw new Error("email provided is not an nodemailer object"); } } var dateWithoutTime = new Date().toISOString().split('T')[0] var defaultFile = process.cwd() + "\\logs\\" + dateWithoutTime + ".txt"; console.log('typeof data.logFile = ' + typeof data.logFile); if (typeof data.logFile !== 'undefined') { this.logFile = data.logFile; } else { this.logFile = defaultFile fs.mkdir(process.cwd() + "\\logs\\", { recursive: true }, function(err) { if (err && err.code !== 'EEXIST') throw err; }); } console.log('path = ' + this.logFile) this.emailOnWarn = data.emailOnWarn || false; this.debugMode = data.debugMode || false; } warn(message) { console.log("\x1b[33m\x1b[1m[LOG - WARN] "+message+"\x1b[0m"); if(this.fileLog) { fs.open(this.logFile, 'a', 666, function(e, id) { console.log(e) fs.write(id, "["+formatDate(new Date())+"] [LOG - WARN] "+message+"\r\n", null, 'utf8', function(){ fs.close(id, function(){ console.log('file is updated'); }); }); }); } } info(message) { console.log("[LOG - INFO] "+message+"\x1b[0m"); if(this.fileLog) { fs.open(this.logFile, 'a', 666, function(e, id) { console.log(e) fs.write(id, "["+formatDate(new Date())+"] [LOG - INFO] "+message+"\r\n", null, 'utf8', function(){ fs.close(id, function(){ console.log('file is updated'); }); }); }); } } debug(message) { console.log("\x1b[34m\x1b[1m[LOG - DEBUG] "+message+"\x1b[0m"); } error(message) { if (typeof message === "object" && message.stack) { message = message.stack; // If messge is error print callstack } console.log("\x1b[31m\x1b[1m[LOG - ERROR] "+message+"\x1b[0m"); if(this.fileLog) { fs.open(this.logFile, 'a', 666, function(e, id) { console.log(e) fs.write(id, "["+formatDate(new Date())+"] [LOG - ERROR] "+message+"\r\n", null, 'utf8', function(){ fs.close(id, function(){ console.log('file is updated'); }); }); }); } if (this.emailNotif) { var mailOptions = { to: this.email, subject: this.title, text: message }; var logInstance = this; this.senderEmail.sendMail(mailOptions, function(err, info){ if (err) { logInstance.emailNotif = false; logInstance.error(err); } logInstance.stopScript(); }); } else { this.stopScript(); } } stopScript(message = "Server occured an error and stop himself") { if (this.stopOnError) { console.log("\x1b[31m\x1b[1m[LOG - FATAL ERROR] " +message+"\x1b[0m"); process.exit(1); } } } function formatDate(dateObj) { hours = dateObj.getHours(); minutes = dateObj.getMinutes(); seconds = dateObj.getSeconds(); if (hours < 10) { hours = '0' + hours; } if (minutes < 10) { minutes = '0' + minutes; } if (seconds < 10) { seconds = '0' + seconds; } return hours + ':' + minutes + ':' + seconds; } exports.logger = logger;