style: format logger

This commit is contained in:
2026-05-31 23:03:37 +09:00
parent d6efb4baea
commit fe9e97baad

View File

@@ -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<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 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<LogLevel, string> = {
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<LoggerOptions>) {
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);
}