1
This commit is contained in:
81
api/middleware/logProxy.js
Normal file
81
api/middleware/logProxy.js
Normal file
@@ -0,0 +1,81 @@
|
||||
const Framework = require('../../framework/node-core-framework');
|
||||
|
||||
/**
|
||||
* Log代理模块
|
||||
* 提供统一的日志接口,延迟获取logsService
|
||||
*/
|
||||
class LogProxy {
|
||||
constructor() {
|
||||
this._logsService = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取logsService实例
|
||||
* @returns {object} logsService实例
|
||||
*/
|
||||
get logsService() {
|
||||
if (!this._logsService) {
|
||||
try {
|
||||
this._logsService = Framework.getServices().logsService;
|
||||
} catch (error) {
|
||||
console.error('无法获取logsService,使用默认日志:', error.message);
|
||||
}
|
||||
}
|
||||
return this._logsService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取调用位置信息
|
||||
* @returns {object} 包含文件路径和行号的对象
|
||||
*/
|
||||
getCallerInfo() {
|
||||
const stack = new Error().stack;
|
||||
const lines = stack.split('\n');
|
||||
|
||||
// 跳过前3行:Error、getCallerInfo、当前方法
|
||||
for (let i = 3; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
// 匹配文件路径和行号
|
||||
const match = line.match(/at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)/);
|
||||
if (match) {
|
||||
const [, functionName, filePath, lineNumber, columnNumber] = match;
|
||||
return {
|
||||
functionName: functionName || 'anonymous',
|
||||
filePath: filePath,
|
||||
lineNumber: parseInt(lineNumber),
|
||||
columnNumber: parseInt(columnNumber)
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录普通日志
|
||||
* @param {...any} args 日志参数
|
||||
*/
|
||||
log(...args) {
|
||||
this.logsService.log(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录错误日志
|
||||
* @param {...any} args 日志参数
|
||||
*/
|
||||
error(...args) {
|
||||
this.logsService.error(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录警告日志
|
||||
* @param {...any} args 日志参数
|
||||
*/
|
||||
warn(...args) {
|
||||
|
||||
this.logsService.warn(...args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 导出单例实例
|
||||
module.exports = new LogProxy();
|
||||
Reference in New Issue
Block a user