Files
autoAiWorkSys/api/middleware/logProxy.js
张成 5d7444cd65 1
2025-11-24 13:23:42 +08:00

82 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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();