UNPKG

fock-logger

Version:

Simple logger for your pet-project

149 lines (148 loc) 5.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Log = exports.LogStrategy = exports.errorAffix = exports.errorAffixText = void 0; const configurator_1 = __importDefault(require("../config/configurator")); const { config } = new configurator_1.default(); const path_1 = __importDefault(require("path")); const fs_1 = require("fs"); const deleter_logger_1 = __importDefault(require("./deleter.logger")); const data_1 = require("../data/data"); const MAX_CACHE_SIZE = 100; const CACHE_TTL = 1000 * 60 * 60; const cache = new Map(); const pathFormat = (...p) => path_1.default.resolve(path_1.default.join(...p)); function cleanupCache() { const now = Date.now(); for (const [key, entry] of cache.entries()) { if (now - entry.timestamp > CACHE_TTL) { cache.delete(key); } } if (cache.size > MAX_CACHE_SIZE) { const entries = Array.from(cache.entries()) .sort((a, b) => a[1].timestamp - b[1].timestamp); const toDelete = entries.slice(0, entries.length - MAX_CACHE_SIZE); toDelete.forEach(([key]) => cache.delete(key)); } } exports.errorAffixText = "------------------ ERROR ------------------"; const errorAffix = (error) => `${exports.errorAffixText}\n${error}\n${exports.errorAffixText}`; exports.errorAffix = errorAffix; class LogStrategy { _date; _date_string; _dir; _deleter; _hello; _init; _file_name; _config; _cache = ""; constructor(dir, data, init = true) { const prefix = data.prefix ? data.prefix + "-" : ""; this._date = new Date(); this._date_string = this._date .toLocaleDateString() .split(".") .reverse() .join("."); this._config = { ...config, ...data, prefix, file_path: data.filePath ? data.filePath : pathFormat(dir, data_1.LOG_DIR_NAME, prefix + this._date_string) + data_1.LOG_FILE_EXTENSION, }; this._file_name = this._config.prefix + this._date_string + data_1.LOG_FILE_EXTENSION; this._init = init; this._dir = pathFormat(dir); this._deleter = new deleter_logger_1.default(dir); this._hello = `====---- Hello! This is log file of ${this._date_string} ! ----====`; if (init) { this.init(); const file = this.readFile(); this._cache = file; cache.set(this._config.file_path, { content: this._cache, timestamp: Date.now() }); } } } exports.LogStrategy = LogStrategy; class Log extends LogStrategy { constructor(dir, data, init = true) { super(dir, data, init); } execute(text) { if (!this._init) { this.init(); this._cache = this.readFile(); cache.set(this._config.file_path, { content: this._cache, timestamp: Date.now() }); } cleanupCache(); const existingCache = cache.get(this._config.file_path); const content = (existingCache?.content || this._hello) + `\n[${this._date.toISOString()}]: ` + text; cache.set(this._config.file_path, { content, timestamp: Date.now() }); return this.writeFile(); } error(error) { const errorText = (Array.isArray(error) ? error : [error]) .map((err) => this.parseError(err)) .join("\n"); const text = (0, exports.errorAffix)(errorText); return this.execute(text); } parseError(error) { return error.stack || `${error.name} ${error.message}`; } init() { this.createFolder(); return this._deleter.init(); } createFile() { const dir = (0, fs_1.readdirSync)(pathFormat(this._dir, data_1.LOG_DIR_NAME)); const dirIncludesFile = dir.includes(this._file_name); if (dirIncludesFile) { return; } return (0, fs_1.writeFileSync)(this._config.file_path, this._hello, "utf-8"); } createFolder() { const dir = (0, fs_1.readdirSync)(this._dir); const dirIncludesFile = dir.includes(data_1.LOG_DIR_NAME); if (!dirIncludesFile) { (0, fs_1.mkdirSync)(pathFormat(this._dir, data_1.LOG_DIR_NAME), { recursive: true }); } return this.createFile(); } readFile() { return (0, fs_1.readFileSync)(pathFormat(this._dir, data_1.LOG_DIR_NAME, this._file_name), "utf-8"); } writeFile() { const cacheEntry = cache.get(this._config.file_path); if (!cacheEntry) { throw new Error(`No cache entry found for ${this._config.file_path}`); } return (0, fs_1.writeFileSync)(this._config.file_path, cacheEntry.content, "utf-8"); } get file() { return this.readFile(); } } exports.Log = Log; exports.default = Log;