UNPKG

@szegedsw/lib-node

Version:

A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.

196 lines 8.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = exports.LogLevel = void 0; /* eslint-disable no-console */ const chalk_1 = __importDefault(require("chalk")); const node_notifier_1 = __importDefault(require("node-notifier")); const path_1 = __importDefault(require("path")); const util_1 = require("util"); const config_1 = require("../config/config"); const email_service_1 = require("../email/email.service"); // eslint-disable-next-line no-shadow var LogLevel; (function (LogLevel) { LogLevel[LogLevel["none"] = 0] = "none"; LogLevel[LogLevel["error"] = 1] = "error"; LogLevel[LogLevel["warning"] = 2] = "warning"; LogLevel[LogLevel["success"] = 3] = "success"; LogLevel[LogLevel["info"] = 4] = "info"; LogLevel[LogLevel["trace"] = 5] = "trace"; })(LogLevel = exports.LogLevel || (exports.LogLevel = {})); let Logger = /** @class */ (() => { class Logger { // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types static error(msg, code, spaceous = true) { if (Logger.level >= LogLevel.error) { if (Logger.writeconsole) { // tslint:disable-next-line: no-console if (spaceous) { console.log(); } // tslint:disable-next-line: no-console console.log((this.timestamp ? `${chalk_1.default.redBright(new Date().toISOString())} ` : "") + chalk_1.default.redBright(`#${process.pid} `) + chalk_1.default.redBright("[Error]") + (code ? chalk_1.default.redBright("(") + chalk_1.default.cyanBright(code) + chalk_1.default.redBright(")") : "") + chalk_1.default.redBright(`: ${util_1.inspect(msg, true, null)}`)); // tslint:disable-next-line: no-console if (spaceous) { console.log(); } if (Logger.enableNotifier) { node_notifier_1.default.notify({ title: `Error[${code}]`, message: util_1.inspect(msg, true, null), sound: true, wait: true, icon: path_1.default.join(__dirname, "/../../Icons/error.png"), }); } } if (Logger.sendmail) { const content = `<html lang="en-GB"> <head> <title>Error${code ? `[${code}]` : ""}</title> </head> <body> <h1 style="text-align: center; color:red;">Error${code ? `[${code}]` : ""}</h1> <table align="center" style="width:100%;"> <tr> <td align="center" style="width: 50%; border-width:5px; border-color: red; border-style: solid; padding: 5px;"> <b>${util_1.inspect(msg, true, null)}</b> </td> </tr> </table> </body> </html>`; email_service_1.EmailService.sendMail(config_1.env.emailUser, config_1.env.emailAdmins, "Error", content, email_service_1.MailPriority.high, true); } if (Logger.errorCallbackFunction !== null && Logger.errorCallbackFunction !== undefined) { Logger.errorCallbackFunction(msg, code); } } } static warning(msg, spaceous = true) { if (Logger.level >= LogLevel.warning) { if (Logger.writeconsole) { // tslint:disable-next-line: no-console if (spaceous) { console.log(); } // tslint:disable-next-line: no-console console.log((this.timestamp ? `${chalk_1.default.yellowBright(new Date().toISOString())} ` : "") + chalk_1.default.yellowBright(`#${process.pid} `) + chalk_1.default.blackBright.yellowBright("[Warning]: ") + chalk_1.default.blackBright.yellowBright(util_1.inspect(msg, true, null))); // tslint:disable-next-line: no-console if (spaceous) { console.log(); } if (Logger.enableNotifier) { node_notifier_1.default.notify({ title: "Warning", message: util_1.inspect(msg, true, null), sound: true, wait: true, icon: path_1.default.join(__dirname, "/../../Icons/warning.png"), }); } } } } static info(msg, spaceous = false) { if (Logger.level >= LogLevel.info) { if (Logger.writeconsole) { // tslint:disable-next-line: no-console if (spaceous) { console.log(); } // tslint:disable-next-line: no-console console.log((this.timestamp ? `${chalk_1.default.bgBlack(new Date().toISOString())} ` : "") + chalk_1.default.bgBlack(`#${process.pid} `) + chalk_1.default.cyanBright.bgBlack("[Info]: ") + chalk_1.default.cyanBright(util_1.inspect(msg, true, null))); // tslint:disable-next-line: no-console if (spaceous) { console.log(); } } } } static trace(msg, spaceous = false) { if (Logger.level >= LogLevel.trace) { if (Logger.writeconsole) { // tslint:disable-next-line: no-console if (spaceous) { console.log(); } // tslint:disable-next-line: no-console console.log((this.timestamp ? `${chalk_1.default.blackBright(new Date().toISOString())} ` : "") + chalk_1.default.blackBright(`#${process.pid} `) + chalk_1.default.blackBright("[Trace]: ") + chalk_1.default.blackBright(util_1.inspect(msg, true, null))); // tslint:disable-next-line: no-console if (spaceous) { console.log(); } } } } static success(msg, spaceous = false) { if (Logger.level >= LogLevel.success) { if (Logger.writeconsole) { // tslint:disable-next-line: no-console if (spaceous) { console.log(); } // tslint:disable-next-line: no-console console.log((this.timestamp ? `${chalk_1.default.greenBright(new Date().toISOString())} ` : "") + chalk_1.default.greenBright(`#${process.pid} `) + chalk_1.default.greenBright("[Success]: ") + chalk_1.default.greenBright(util_1.inspect(msg, true, null))); // tslint:disable-next-line: no-console if (spaceous) { console.log(); } } } } } Object.defineProperty(Logger, "writeconsole", { enumerable: true, configurable: true, writable: true, value: true }); Object.defineProperty(Logger, "level", { enumerable: true, configurable: true, writable: true, value: LogLevel.trace }); Object.defineProperty(Logger, "sendmail", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(Logger, "enableNotifier", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(Logger, "timestamp", { enumerable: true, configurable: true, writable: true, value: true }); return Logger; })(); exports.Logger = Logger; //# sourceMappingURL=logger.js.map