mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
✨ feat: 输出美化
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
"dependencies": {
|
||||
"@hono/node-server": "^1.11.2",
|
||||
"axios": "^1.7.2",
|
||||
"chalk": "^5.3.0",
|
||||
"cheerio": "1.0.0-rc.12",
|
||||
"dayjs": "^1.11.11",
|
||||
"dotenv": "^16.4.5",
|
||||
|
||||
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@@ -14,6 +14,9 @@ importers:
|
||||
axios:
|
||||
specifier: ^1.7.2
|
||||
version: 1.7.2
|
||||
chalk:
|
||||
specifier: ^5.3.0
|
||||
version: 5.3.0
|
||||
cheerio:
|
||||
specifier: 1.0.0-rc.12
|
||||
version: 1.0.0-rc.12
|
||||
@@ -385,6 +388,10 @@ packages:
|
||||
resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
chalk@5.3.0:
|
||||
resolution: {integrity: sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==}
|
||||
engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}
|
||||
|
||||
charenc@0.0.2:
|
||||
resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==}
|
||||
|
||||
@@ -1275,6 +1282,8 @@ snapshots:
|
||||
ansi-styles: 4.3.0
|
||||
supports-color: 7.2.0
|
||||
|
||||
chalk@5.3.0: {}
|
||||
|
||||
charenc@0.0.2: {}
|
||||
|
||||
cheerio-select@2.1.0:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { config } from "../config.js";
|
||||
import { createLogger, format, transports } from "winston";
|
||||
import path from "path";
|
||||
import chalk from "chalk";
|
||||
|
||||
let pathOption: (typeof transports.File)[] = [];
|
||||
|
||||
@@ -26,6 +27,28 @@ if (config.USE_LOG_FILE) {
|
||||
}
|
||||
}
|
||||
|
||||
// 定义不同日志级别的彩色块
|
||||
const levelColors: { [key: string]: string } = {
|
||||
error: chalk.bgRed(" ERROR "),
|
||||
warn: chalk.bgYellow(" WARN "),
|
||||
info: chalk.bgBlue(" INFO "),
|
||||
debug: chalk.bgGreen(" DEBUG "),
|
||||
default: chalk.bgWhite(" LOG "),
|
||||
};
|
||||
|
||||
// 自定义控制台日志输出格式
|
||||
const consoleFormat = format.printf(({ level, message, timestamp, stack }) => {
|
||||
// 获取原始日志级别
|
||||
const originalLevel = Object.keys(levelColors).find((lvl) => level.includes(lvl)) || "default";
|
||||
const colorLevel = levelColors[originalLevel] || levelColors.default;
|
||||
|
||||
let logMessage = `${colorLevel} [${timestamp}] ${message}`;
|
||||
if (stack) {
|
||||
logMessage += `\n${stack}`;
|
||||
}
|
||||
return logMessage;
|
||||
});
|
||||
|
||||
// logger
|
||||
const logger = createLogger({
|
||||
// 最低的日志级别
|
||||
@@ -47,7 +70,7 @@ if (process.env.NODE_ENV !== "production") {
|
||||
try {
|
||||
logger.add(
|
||||
new transports.Console({
|
||||
format: format.combine(format.colorize(), format.simple()),
|
||||
format: format.combine(format.colorize(), consoleFormat),
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user