From fe9e97baad97a08fa9c5379cb558fe75848c4a13 Mon Sep 17 00:00:00 2001 From: p-sw Date: Sun, 31 May 2026 23:03:37 +0900 Subject: [PATCH] style: format logger --- src/utils/logger.ts | 98 +++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/src/utils/logger.ts b/src/utils/logger.ts index ef73219..7e72858 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,26 +1,34 @@ import chalk, { type ChalkInstance } from "chalk"; -import { appendFileSync, existsSync, mkdirSync, createWriteStream, type WriteStream } from "fs"; +import { existsSync, mkdirSync, createWriteStream, type WriteStream } from "fs"; import { dirname } from "path"; -import { config } from "@/config"; -export type LogLevel = "debug" | "info" | "success" | "warn" | "error" | "fatal"; +export type LogLevel = + | "debug" + | "info" + | "success" + | "warn" + | "error" + | "fatal"; -const LEVELS: Record = { - debug: { rank: 0, color: chalk.gray, stderr: false }, - info: { rank: 1, color: chalk.blue, stderr: false }, - success: { rank: 2, color: chalk.green, stderr: false }, - warn: { rank: 3, color: chalk.yellow, stderr: true }, - error: { rank: 4, color: chalk.red, stderr: true }, - fatal: { rank: 5, color: chalk.bgRed.white,stderr: true }, +const LEVELS: Record< + LogLevel, + { rank: number; color: ChalkInstance; stderr: boolean } +> = { + debug: { rank: 0, color: chalk.gray, stderr: false }, + info: { rank: 1, color: chalk.blue, stderr: false }, + success: { rank: 2, color: chalk.green, stderr: false }, + warn: { rank: 3, color: chalk.yellow, stderr: true }, + error: { rank: 4, color: chalk.red, stderr: true }, + fatal: { rank: 5, color: chalk.bgRed.white, stderr: true }, }; const ICONS: Record = { - debug: "◆", - info: "ℹ", + debug: "◆", + info: "ℹ", success: "✔", - warn: "⚠", - error: "✖", - fatal: "▲", + warn: "⚠", + error: "✖", + fatal: "▲", }; export interface LoggerOptions { @@ -76,13 +84,21 @@ class Logger { return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`; } - private format(level: LogLevel, message: string): { console: string; file: string } { + private format( + level: LogLevel, + message: string, + ): { console: string; file: string } { const ts = this.timestamps ? `[${this.formatTimestamp()}]` : ""; const tag = this.tag ? `[${this.tag}]` : ""; const icon = ICONS[level]; const levelStr = level.toUpperCase(); - const consoleParts = [ts, tag, this.colors ? LEVELS[level].color(icon) : icon, message].filter(Boolean); + const consoleParts = [ + ts, + tag, + this.colors ? LEVELS[level].color(icon) : icon, + message, + ].filter(Boolean); const consoleLine = consoleParts.join(" "); const fileParts = [ts, tag, `[${levelStr}]`, message].filter(Boolean); @@ -92,18 +108,23 @@ class Logger { } private formatJson(level: LogLevel, message: string): string { - return JSON.stringify({ - timestamp: new Date().toISOString(), - level, - tag: this.tag, - message, - }) + "\n"; + return ( + JSON.stringify({ + timestamp: new Date().toISOString(), + level, + tag: this.tag, + message, + }) + "\n" + ); } private write(level: LogLevel, message: string) { if (!this.shouldLog(level)) return; - const { console: consoleLine, file: fileLine } = this.format(level, message); + const { console: consoleLine, file: fileLine } = this.format( + level, + message, + ); const jsonLine = this.formatJson(level, message); if (!this.silent) { @@ -116,14 +137,25 @@ class Logger { } } - debug(message: string) { this.write("debug", message); } - info(message: string) { this.write("info", message); } - success(message: string) { this.write("success", message); } - warn(message: string) { this.write("warn", message); } - error(message: string) { this.write("error", message); } - fatal(message: string) { this.write("fatal", message); } + debug(message: string) { + this.write("debug", message); + } + info(message: string) { + this.write("info", message); + } + success(message: string) { + this.write("success", message); + } + warn(message: string) { + this.write("warn", message); + } + error(message: string) { + this.write("error", message); + } + fatal(message: string) { + this.write("fatal", message); + } - /** Create a child logger with an additional tag */ child(tag: string): Logger { const combined = this.tag ? `${this.tag}:${tag}` : tag; return new Logger({ @@ -137,7 +169,6 @@ class Logger { }); } - /** Update options at runtime */ configure(options: Partial) { if (options.level !== undefined) this.level = options.level; if (options.timestamps !== undefined) this.timestamps = options.timestamps; @@ -159,16 +190,13 @@ class Logger { } } - /** Close file stream gracefully */ close() { this.fileStream?.end(); } } -/** Default global logger instance */ export const logger = new Logger(); -/** Create a new logger instance with custom options */ export function createLogger(options: LoggerOptions): Logger { return new Logger(options); }