Commit ed22eae2 by linjinhong

修改问题

parent c55b2d32
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"name": "JomallProductionAssistant", "name": "JomallProductionAssistant",
"productName": "JomallProductionAssistant", "productName": "JomallProductionAssistant",
"description": "", "description": "",
"version": "1.0.16", "version": "1.0.17",
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
......
...@@ -3,42 +3,91 @@ const moment = require("moment"); ...@@ -3,42 +3,91 @@ const moment = require("moment");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
export function returnLogFilePath() { // 获取日志文件路径
function returnLogFilePath() {
const today = moment(new Date()).format("YYYY-MM-DD"); const today = moment(new Date()).format("YYYY-MM-DD");
const low_path = process.cwd(); const low_path = process.cwd();
let log_dir = path.join(low_path, "logs") let log_dir = path.join(low_path, "logs");
if (!fs.existsSync(log_dir)) { if (!fs.existsSync(log_dir)) {
fs.mkdirSync(log_dir) // 创建日志文件夹 fs.mkdirSync(log_dir, { recursive: true });
} }
const dir = path.join(low_path, `/logs/${today}`); const dir = path.join(low_path, `/logs/${today}`);
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir); fs.mkdirSync(dir);
} }
return path.join(dir, "api.log"); return path.join(dir, "api.log");
} }
//日志对象 // 获取错误日志文件路径
const logger = (name) => { function returnErrorLogFilePath() {
const today = moment(new Date()).format("YYYY-MM-DD");
const low_path = process.cwd();
let log_dir = path.join(low_path, "logs");
if (!fs.existsSync(log_dir)) {
fs.mkdirSync(log_dir, { recursive: true });
}
const dir = path.join(low_path, `/logs/${today}`);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return path.join(dir, "error.log");
}
// 初始化日志系统
function initLogger() {
const logFilePath = returnLogFilePath(); const logFilePath = returnLogFilePath();
const errorLogFilePath = returnErrorLogFilePath();
log4js.configure({ log4js.configure({
appenders: { appenders: {
out: { type: "console" }, //在控制台输出日志 console: { type: "console" },
cheese: { file: {
type: "file", type: "file",
filename: logFilePath, filename: logFilePath,
maxLogSize: 1024 * 1000 * 10 //10M maxLogSize: 1024 * 1000 * 10,
} backups: 5,
compress: true,
},
errorFile: {
type: "file",
filename: errorLogFilePath,
maxLogSize: 1024 * 1000 * 5,
backups: 10,
compress: true,
},
errors: {
type: "logLevelFilter",
appender: "errorFile",
level: "error",
},
}, },
categories: { categories: {
//需要在控制台输出日志时:appenders: ['cheese', 'out'] default: {
default: { appenders: ["cheese"], level: log4js.levels.DEBUG } appenders: ["console", "file", "errors"],
} level: "debug",
},
},
}); });
return log4js.getLogger(name);
};
//添加日志 return log4js.getLogger();
}
// 创建全局日志实例
const logger = initLogger();
// 添加请求日志
const addFormatLog = function(req, res, data) { const addFormatLog = function(req, res, data) {
// 检查是否需要屏蔽此URL
if (shouldSkipLogging(req.url)) {
return;
}
const now = new Date(); const now = new Date();
const resTime = now - req._startTime; const resTime = now - req._startTime;
...@@ -50,8 +99,9 @@ const addFormatLog = function(req, res, data) { ...@@ -50,8 +99,9 @@ const addFormatLog = function(req, res, data) {
url, url,
body, body,
httpVersion, httpVersion,
res: { statusCode, _headers } res: { statusCode, _headers },
} = req; } = req;
let logInfo = { let logInfo = {
ip, ip,
host: headers.host, host: headers.host,
...@@ -64,10 +114,10 @@ const addFormatLog = function(req, res, data) { ...@@ -64,10 +114,10 @@ const addFormatLog = function(req, res, data) {
statusCode, statusCode,
contentLength: _headers["content-length"], contentLength: _headers["content-length"],
userAgent: headers["user-agent"], userAgent: headers["user-agent"],
data: data[0] data: data[0],
}; };
// ${JSON.stringify(logInfo)}
logger("log").info(` logger.info(`
时间:${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")} 时间:${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")}
ip : ${logInfo.ip} ip : ${logInfo.ip}
host : ${logInfo.host} host : ${logInfo.host}
...@@ -78,14 +128,28 @@ const addFormatLog = function(req, res, data) { ...@@ -78,14 +128,28 @@ const addFormatLog = function(req, res, data) {
body :${JSON.stringify(logInfo.body)} body :${JSON.stringify(logInfo.body)}
状态码 :${logInfo.statusCode} 状态码 :${logInfo.statusCode}
data :${JSON.stringify(logInfo.data)} data :${JSON.stringify(logInfo.data)}
` `);
);
}; };
//日志中间件 // 判断是否需要跳过日志记录
function shouldSkipLogging(url) {
// 定义需要屏蔽的URL列表
const skippedUrls = [
"/getAllCountry", // 您想要屏蔽的接口
// 可以添加更多需要屏蔽的URL
];
return skippedUrls.some((skippedUrl) => url.includes(skippedUrl));
}
// 日志中间件
export const logMiddleWare = () => { export const logMiddleWare = () => {
return function(req, res, next) { return function(req, res, next) {
// 检查是否需要跳过此请求的日志记录
if (shouldSkipLogging(req.url)) {
return next();
}
req._startTime = new Date(); req._startTime = new Date();
const oldSend = res.send; const oldSend = res.send;
...@@ -100,3 +164,59 @@ export const logMiddleWare = () => { ...@@ -100,3 +164,59 @@ export const logMiddleWare = () => {
}; };
}; };
// ================= 错误捕获增强 =================
// 捕获未处理的异常
process.on("uncaughtException", (error) => {
logger.fatal(`
====== 未捕获的异常 ======
时间: ${moment().format("YYYY-MM-DD HH:mm:ss")}
错误信息: ${error.message}
堆栈跟踪:
${error.stack}
========================
`);
});
// 捕获未处理的Promise拒绝
process.on("unhandledRejection", (reason, promise) => {
logger.fatal(`
====== 未处理的Promise拒绝 ======
时间: ${moment().format("YYYY-MM-DD HH:mm:ss")}
拒绝原因: ${reason}
Promise对象: ${promise}
===============================
`);
});
// 导出增强的日志系统
export default {
logger,
logMiddleWare,
// 添加错误日志函数
logError: (error, context = {}) => {
logger.error(`
====== 错误日志 ======
时间: ${moment().format("YYYY-MM-DD HH:mm:ss")}
错误信息: ${error.message}
堆栈跟踪:
${error.stack}
上下文信息:
${JSON.stringify(context, null, 2)}
=====================
`);
},
// 添加致命错误日志函数
logFatal: (error, context = {}) => {
logger.fatal(`
====== 致命错误 ======
时间: ${moment().format("YYYY-MM-DD HH:mm:ss")}
错误信息: ${error.message}
堆栈跟踪:
${error.stack}
上下文信息:
${JSON.stringify(context, null, 2)}
=====================
`);
},
};
...@@ -191,7 +191,7 @@ async function cropImage(inputPath, outputPath, options = {}) { ...@@ -191,7 +191,7 @@ async function cropImage(inputPath, outputPath, options = {}) {
stripHeight = 64, // 流式模式条带高度 stripHeight = 64, // 流式模式条带高度
maxDimension = 2500, // 小图/大图分界点 maxDimension = 2500, // 小图/大图分界点
padding = 0, // 裁剪后留白 padding = 0, // 裁剪后留白
debug = false, // 调试模式 debug = true, // 调试模式
forceStreamMode = false, // 强制使用流式模式 forceStreamMode = false, // 强制使用流式模式
} = options; } = options;
......
...@@ -304,6 +304,7 @@ export default { ...@@ -304,6 +304,7 @@ export default {
}, },
async hasDesignImagesCanvasJsonList(designImagesCanvasJsonList) { async hasDesignImagesCanvasJsonList(designImagesCanvasJsonList) {
let imageResList = []; let imageResList = [];
// let debug = false;
if (this.detail.adjustable && designImagesCanvasJsonList) { if (this.detail.adjustable && designImagesCanvasJsonList) {
designImagesCanvasJsonList = JSON.parse(designImagesCanvasJsonList); designImagesCanvasJsonList = JSON.parse(designImagesCanvasJsonList);
if (designImagesCanvasJsonList[0].images) { if (designImagesCanvasJsonList[0].images) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment