feat: 添加日志输出

This commit is contained in:
imsyy 2023-12-06 09:25:26 +08:00
parent aab54d2190
commit 68edae7e74
4 changed files with 645 additions and 1224 deletions

View File

@ -4,15 +4,47 @@ const bodyParser = require("koa-bodyparser");
const cors = require("koa2-cors");
const serve = require("koa-static");
const views = require("koa-views");
const winston = require("winston");
const path = require("path");
const app = new Koa();
const net = require("net");
const router = require("./routes");
const { createLogger, format, transports } = winston;
// 配置信息
let domain = process.env.ALLOWED_DOMAIN || "*";
let port = process.env.PORT || 6688;
// 配置 log
const log = createLogger({
format: format.combine(
format.timestamp(),
format.printf(({ timestamp, level, message }) => `[${timestamp}] [${level}] ${message}`),
),
transports: [
new transports.Console(),
new transports.File({
filename: path.join(__dirname, "logs", "app.log"),
maxsize: 2 * 1024 * 1024,
}),
],
});
// 重新定义 console
console.log = (...args) => {
log.info(args.join(" "));
};
console.info = (...args) => {
log.info(args.join(" "));
};
console.warn = (...args) => {
log.warn(args.join(" "));
};
console.error = (...args) => {
log.error(args.join(" "));
};
// 解析请求体
app.use(bodyParser());
@ -50,7 +82,7 @@ app.use(router.allowedMethods());
// 启动应用程序并监听端口
const startApp = (port) => {
app.listen(port, () => {
console.log(`成功在 ${port} 端口上运行`);
log.info(`成功在 ${port} 端口上运行`);
});
};
@ -61,7 +93,7 @@ const checkPort = (port) => {
.createServer()
.once("error", (err) => {
if (err.code === "EADDRINUSE") {
console.log(`端口 ${port} 已被占用, 正在尝试其他端口...`);
log.info(`端口 ${port} 已被占用, 正在尝试其他端口...`);
server.close();
resolve(false);
} else {

View File

@ -8,8 +8,7 @@
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
"start": "node index.js",
"dev": "npx nodemon index.js",
"prd": "pm2 start index.js",
"build": "node index.js"
"prd": "pm2 start index.js"
},
"author": "imsyy",
"license": "MIT",
@ -17,6 +16,8 @@
"axios": "^1.3.4",
"cheerio": "1.0.0-rc.12",
"dotenv": "^16.0.3",
"eslint": "^8.48.0",
"eslint-plugin-vue": "^9.17.0",
"koa": "^2.14.1",
"koa-bodyparser": "^4.3.0",
"koa-router": "^12.0.0",
@ -25,8 +26,7 @@
"koa2-cors": "^2.0.6",
"node-cache": "^5.1.2",
"nodemon": "^2.0.22",
"eslint": "^8.48.0",
"eslint-plugin-vue": "^9.17.0",
"prettier": "^3.0.2"
"prettier": "^3.0.2",
"winston": "^3.11.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,6 @@ const getData = (data) => {
.load(newListHtml)(".placeholder")
.get()
.map((v) => {
console.log($(v));
dataList.push({
title: $(v).find(".plc-title").text(),
img: $(v).find("img").attr("data-original"),