UNPKG

gypsum

Version:

Simple and easy lightweight typescript server side framework on Node.js.

105 lines 4.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); const fs = require("fs"); const chalk = require("chalk"); let options = null; let outDir; function stringify(data) { try { return JSON.stringify(data, null, 2); } catch (e) { return data.join(" "); } } class Logger { constructor(ns) { this._active = false; this._ns = ns; if (options) { if (options.hasOwnProperty('all') || options.hasOwnProperty(this._ns)) { this._levels = options[this._ns] ? options[this._ns].level : options.all.level; this._active = true; if (outDir) { this._logOptions = options[this._ns] ? options[this._ns].log : options.all.log; this._logDir = this._logOptions && this._logOptions.length ? path.join(outDir, this._ns) : ''; if (this._logDir) { if (!fs.existsSync(this._logDir)) try { fs.mkdirSync(this._logDir); } catch (err) { console.trace(err); } } } } else { this._active = false; } } else { this._active = false; } } static SetOptions(opt, logOutPath = 'server_logs') { outDir = path.join(process.cwd(), logOutPath); options = opt || null; if (!fs.existsSync(outDir)) fs.mkdirSync(outDir); } static Error(...args) { console.trace(chalk.default.red.bold(`[error] ->`, stringify(args))); } static Warn(...args) { console.log(chalk.default.yellow(`[warn] ->`, stringify(args))); } static Info(...args) { console.log(chalk.default.blue(`[info] ->`, stringify(args))); } static Debug(...args) { console.log(chalk.default.gray(`[debug] ->`, stringify(args))); } error(...args) { if (this._active && (this._levels.indexOf('error') > -1 || this._levels.indexOf('all') > -1)) { console.trace(chalk.default.red.bold(`[error] ->`, stringify(args))); if (this._logOptions && (this._logOptions.indexOf('error') > -1 || this._logOptions.indexOf('all') > -1)) this._log('error', stringify(args)); } } warn(...args) { if (this._active && (this._levels.indexOf('warn') > -1 || this._levels.indexOf('all') > -1)) { console.log(chalk.default.yellow(`${this._ns}: [warn] ->`, stringify(args))); if (this._logOptions && (this._logOptions.indexOf('warn') > -1 || this._logOptions.indexOf('all') > -1)) this._log('warn', stringify(args)); } } info(...args) { if (this._active && (this._levels.indexOf('info') > -1 || this._levels.indexOf('all') > -1)) { console.log(chalk.default.blue(`${this._ns}: [info] ->`, stringify(args))); if (this._logOptions && (this._logOptions.indexOf('info') > -1 || this._logOptions.indexOf('all') > -1)) this._log('info', stringify(args)); } } debug(...args) { if (this._active && (this._levels.indexOf('debug') > -1 || this._levels.indexOf('all') > -1)) { console.log(chalk.default.gray(`${this._ns}: [debug] ->`, stringify(args))); if (this._logOptions && (this._logOptions.indexOf('debug') > -1 || this._logOptions.indexOf('all') > -1)) this._log('debug', stringify(args)); } } _log(level = 'error', ...args) { args.unshift(new Date()); args.push('\n'); let result = args.join(", "); fs.appendFile(path.join(this._logDir, level), result, err => { if (err) { console.log('error logging in:', this._ns); console.log(err); } }); } } exports.Logger = Logger; //# sourceMappingURL=logger.js.map