UNPKG

cfs-logger

Version:

Simple colorful JSON logger for Node.js projects

114 lines (113 loc) 4.04 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 = void 0; const chalk_1 = __importDefault(require("chalk")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class Logger { constructor(options = {}) { this.levels = { error: 0, warn: 1, info: 2, debug: 3, }; this.colors = { error: chalk_1.default.red, warn: chalk_1.default.yellow, info: chalk_1.default.blue, debug: chalk_1.default.gray, }; this.level = options.level || "info"; this.logDir = options.logDir || path_1.default.join(process.cwd(), "logs"); this.enableConsole = options.enableConsole ?? true; this.enableFile = options.enableFile ?? true; this.jsonFormat = options.jsonFormat ?? true; this.prettyPrint = options.prettyPrint ?? false; this.singleLine = options.singleLine ?? false; if (this.enableFile && !fs_1.default.existsSync(this.logDir)) { fs_1.default.mkdirSync(this.logDir, { recursive: true }); } } shouldLog(level) { return this.levels[level] <= this.levels[this.level]; } getLogFileName() { const date = new Date().toISOString().split("T")[0]; return path_1.default.join(this.logDir, `${date}.log`); } writeFile(message) { const logFile = this.getLogFileName(); fs_1.default.appendFileSync(logFile, message + "\n", "utf8"); } colorizeJson(jsonString) { return jsonString.replace(/("(.*?)":)|(".*?")|(\b\d+\b)|\b(true|false|null)\b/g, (match) => { if (/^".*":$/.test(match)) { return chalk_1.default.cyan(match); } else if (/^".*"$/.test(match)) { return chalk_1.default.green(match); } else if (/^\d+$/.test(match)) { return chalk_1.default.yellow(match); } else if (/true|false/.test(match)) { return chalk_1.default.magenta(match); } else if (/null/.test(match)) { return chalk_1.default.gray(match); } return match; }); } formatMessage(level, ...args) { const timestamp = new Date().toISOString(); if (this.jsonFormat) { const logObj = { timestamp, level, messages: args.map((a) => (typeof a === "object" ? a : String(a))), }; // singleLine luôn ưu tiên const space = this.singleLine ? 0 : this.prettyPrint ? 2 : 0; const fileMsg = JSON.stringify(logObj, null, space); let consoleMsg = fileMsg; if (this.prettyPrint && !this.singleLine) { consoleMsg = this.colorizeJson(fileMsg); } return { consoleMsg, fileMsg }; } const msg = args .map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a))) .join(" "); const formatted = `[${timestamp}] [${level.toUpperCase()}]: ${msg}`; return { consoleMsg: formatted, fileMsg: formatted }; } log(level, ...args) { if (!this.shouldLog(level)) return; const { consoleMsg, fileMsg } = this.formatMessage(level, ...args); if (this.enableConsole) { console.log(this.colors[level](consoleMsg)); } if (this.enableFile) { this.writeFile(fileMsg); } } info(...args) { this.log("info", ...args); } warn(...args) { this.log("warn", ...args); } error(...args) { this.log("error", ...args); } debug(...args) { this.log("debug", ...args); } } exports.Logger = Logger;