UNPKG

@tomjs/logger

Version:
144 lines (141 loc) 3.83 kB
import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import dayjs from "dayjs"; import colors from "picocolors"; import stripAnsi from "strip-ansi"; import isUnicodeSupported from "is-unicode-supported"; //#region src/colors.ts const supported = isUnicodeSupported(); const logColors = { info: colors.cyan, success: colors.green, warn: colors.yellow, warning: colors.yellow, error: colors.red, debug: colors.gray, log: colors.gray }; const logSymbols = { info: colors.cyan(supported ? "ℹ" : "i"), success: colors.green(supported ? "✔" : "√"), warn: colors.yellow(supported ? "⚠" : "!"), warning: colors.yellow(supported ? "⚠" : "!"), error: colors.red(supported ? "✖" : "x"), debug: colors.gray(supported ? "◎" : "#"), log: colors.gray(supported ? "◎" : "#") }; //#endregion //#region src/index.ts let timeFormatter; function getTimeFormatter() { timeFormatter ??= new Intl.DateTimeFormat(void 0, { hour: "numeric", minute: "numeric", second: "numeric" }); return timeFormatter; } /** * log tool */ var Logger = class { constructor(options) { this._opts = {}; this.setOptions(Object.assign({}, options)); } initLogDir() { const { directory, cleanup } = this._opts; if (!directory) return; const root = this._opts.root || path.join(os.homedir(), ".tomjs"); const logDir = path.join(root, directory); this._logDir = logDir; if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true }); fs.readdirSync(logDir).forEach((s) => { if (dayjs(s.substring(0, 8)).isBefore(dayjs().endOf("day").subtract(Math.max(1, cleanup ?? 30), "day"))) fs.rmSync(path.join(logDir, s), { force: true }); }); } format(...args) { return args.map((s) => typeof s === "object" ? JSON.stringify(s) : s || "").join(" "); } _writeLog(...args) { if (!this._logDir) return; const logFile = path.join(this._logDir, `${dayjs().format("YYYYMMDD")}.log`); fs.appendFileSync(logFile, `${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")} ${stripAnsi(this.format(...args))}\n`); } _log(type, ...args) { this._writeLog(type, ...args); const flag = this._opts.flag || "symbol"; const preList = []; if (flag === "time") preList.push(colors.dim(getTimeFormatter().format(/* @__PURE__ */ new Date()))); else if (flag === "symbol") preList.push(logSymbols[type]); const { prefix } = this._opts; if (prefix) preList.push(logColors[type](colors.bold(prefix))); const list = preList.concat(args); console.log(preList.concat(args).map((s) => typeof s === "object" ? "%o" : "%s").join(" "), ...list); } /** * set debug mode or not */ enableDebug(debug) { this._opts.debug = !!debug; } /** * set debug mode or not */ setOptions(options) { this._opts = Object.assign({}, this._opts, options); this.initLogDir(); } /** * like console.log */ log(...args) { this._log("log", ...args); } /** * write log to file */ write(...args) { this._writeLog(...args); } /** * only show in debug mode */ debug(...args) { if (this._opts.debug) this._log("log", ...args); } /** * add the specified red prefix or error symbol before the log content */ error(...args) { this._log(`error`, ...args); } /** * add the specified blue prefix or info symbol before the log content */ info(...args) { this._log("info", ...args); } /** * add the specified green prefix or success symbol before the log content */ success(...args) { this._log("success", ...args); } /** * add the specified yellow prefix or warning symbol before the log content */ warning(...args) { this._log("warn", ...args); } /** * add the specified yellow prefix or warning symbol before the log content */ warn(...args) { this.warning(...args); } }; var src_default = Logger; //#endregion export { Logger, src_default as default };