feat: 添加日志输出

This commit is contained in:
imsyy 2023-12-05 16:42:25 +08:00
parent aab54d2190
commit 9eb76a5f52
4 changed files with 644 additions and 1222 deletions

View File

@ -4,15 +4,47 @@ const bodyParser = require("koa-bodyparser");
const cors = require("koa2-cors"); const cors = require("koa2-cors");
const serve = require("koa-static"); const serve = require("koa-static");
const views = require("koa-views"); const views = require("koa-views");
const winston = require("winston");
const path = require("path");
const app = new Koa(); const app = new Koa();
const net = require("net"); const net = require("net");
const router = require("./routes"); const router = require("./routes");
const { createLogger, format, transports } = winston;
// 配置信息 // 配置信息
let domain = process.env.ALLOWED_DOMAIN || "*"; let domain = process.env.ALLOWED_DOMAIN || "*";
let port = process.env.PORT || 6688; 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()); app.use(bodyParser());
@ -50,7 +82,7 @@ app.use(router.allowedMethods());
// 启动应用程序并监听端口 // 启动应用程序并监听端口
const startApp = (port) => { const startApp = (port) => {
app.listen(port, () => { app.listen(port, () => {
console.log(`成功在 ${port} 端口上运行`); log.info(`成功在 ${port} 端口上运行`);
}); });
}; };
@ -61,7 +93,7 @@ const checkPort = (port) => {
.createServer() .createServer()
.once("error", (err) => { .once("error", (err) => {
if (err.code === "EADDRINUSE") { if (err.code === "EADDRINUSE") {
console.log(`端口 ${port} 已被占用, 正在尝试其他端口...`); log.info(`端口 ${port} 已被占用, 正在尝试其他端口...`);
server.close(); server.close();
resolve(false); resolve(false);
} else { } else {

View File

@ -17,6 +17,8 @@
"axios": "^1.3.4", "axios": "^1.3.4",
"cheerio": "1.0.0-rc.12", "cheerio": "1.0.0-rc.12",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"eslint": "^8.48.0",
"eslint-plugin-vue": "^9.17.0",
"koa": "^2.14.1", "koa": "^2.14.1",
"koa-bodyparser": "^4.3.0", "koa-bodyparser": "^4.3.0",
"koa-router": "^12.0.0", "koa-router": "^12.0.0",
@ -25,8 +27,7 @@
"koa2-cors": "^2.0.6", "koa2-cors": "^2.0.6",
"node-cache": "^5.1.2", "node-cache": "^5.1.2",
"nodemon": "^2.0.22", "nodemon": "^2.0.22",
"eslint": "^8.48.0", "prettier": "^3.0.2",
"eslint-plugin-vue": "^9.17.0", "winston": "^3.11.0"
"prettier": "^3.0.2"
} }
} }

File diff suppressed because it is too large Load Diff

View File

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