增强日志记录功能,添加安全控制台以避免劫持,并在日志处理时输出调试信息

This commit is contained in:
2026-01-16 15:56:20 +08:00
parent b9df92435a
commit 256b295739
2 changed files with 23 additions and 1 deletions

View File

@@ -1,8 +1,26 @@
import Logger from './log/Logger.js';
import FilePipe from './file/FilePipe.js';
// 【逃生通道】创建一个纯净的 Console 对象,绕过任何潜在的劫持
const getSafeConsole = () => {
try {
if (window._amilySafeConsole) return window._amilySafeConsole;
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
const safe = iframe.contentWindow.console;
// document.body.removeChild(iframe); // 保持 iframe 以维持 console 引用有效
window._amilySafeConsole = safe;
return safe;
} catch (e) {
return window.console; // Fallback
}
};
class Amily2Bus {
constructor() {
this.safeConsole = getSafeConsole();
/** @type {Logger|null} */
this.Logger = new Logger();
/** @type {FilePipe|null} */
@@ -11,7 +29,7 @@ class Amily2Bus {
// 已注册插件列表,防止重复注册
this.registry = new Set();
console.log('[Amily2Bus] Core container initialized with secure registry.');
this.safeConsole.log('[Amily2Bus] Core container initialized with secure registry.');
}
/**
@@ -23,6 +41,7 @@ class Amily2Bus {
* @param {string} [plugin='Global'] 来源插件/命名空间,调试时可指定如 'Console'
*/
log(type, message, origin = 'Bus', plugin = 'Global') {
this.safeConsole.error('[Amily2Bus DEBUG] log called (via SafeConsole):', { type, loggerExists: !!this.Logger });
if (this.Logger) {
this.Logger.process(plugin, origin, type, message);
}

View File

@@ -144,6 +144,9 @@ class Logger {
* 统一处理过滤、格式化和输出,支持默认归属 Global
*/
process(plugin, origin, type, message, inFile = false) {
// [DEBUG] 强制输出以确认方法被调用 (使用 error 级别防止被过滤)
console.error('[Logger DEBUG] Process called:', { plugin, origin, type, message });
// 1. 默认归属处理
const safePlugin = plugin || 'Global';
const safeOrigin = origin || 'System';