修复打包日志模块识别失败问题

This commit is contained in:
2025-08-31 19:00:08 +08:00
parent a09d6cab0e
commit ff05567e6b
2 changed files with 30 additions and 5 deletions
+28 -4
View File
@@ -78,15 +78,33 @@ class Logger {
this.level = options.level || LogLevel.INFO; this.level = options.level || LogLevel.INFO;
this.enableConsole = options.enableConsole !== false; this.enableConsole = options.enableConsole !== false;
this.enableFile = options.enableFile || false; this.enableFile = options.enableFile || false;
this.logDir = options.logDir || path.join(__dirname, '../logs');
// 动态设置日志目录,避免pkg静态分析问题
this.logDir = options.logDir || this._getLogDir();
this.maxFileSize = options.maxFileSize || 10 * 1024 * 1024; // 10MB this.maxFileSize = options.maxFileSize || 10 * 1024 * 1024; // 10MB
this.maxFiles = options.maxFiles || 5; this.maxFiles = options.maxFiles || 5;
this.enableColors = options.enableColors !== false; this.enableColors = options.enableColors !== false;
this.module = options.module || 'App'; this.module = options.module || 'App';
// 确保日志目录存在 // 延迟初始化,不在构造函数中创建目录
if (this.enableFile) { this._initialized = false;
this.ensureLogDir(); }
/**
* 动态获取日志目录路径
*/
_getLogDir() {
// 检测是否在pkg打包环境中运行
const isPkg = process.pkg !== undefined;
if (isPkg) {
// 在打包环境中,使用可执行文件所在目录
return path.join(process.cwd(), 'logs');
} else {
// 在开发环境中,使用项目根目录的logs文件夹
// 使用相对路径避免pkg静态分析问题
return 'logs';
} }
} }
@@ -147,6 +165,12 @@ class Logger {
writeToFile(message) { writeToFile(message) {
if (!this.enableFile) return; if (!this.enableFile) return;
// 延迟初始化,只在第一次写入时创建目录
if (!this._initialized) {
this.ensureLogDir();
this._initialized = true;
}
const dateStr = this.getDateString(); const dateStr = this.getDateString();
const logFile = path.join(this.logDir, `${dateStr}.log`); const logFile = path.join(this.logDir, `${dateStr}.log`);
+2 -1
View File
@@ -3,6 +3,7 @@ const path = require('path');
const { defaultLogger } = require('../backend/utils/logger'); const { defaultLogger } = require('../backend/utils/logger');
// 创建logger实例 // 创建logger实例
const logger = defaultLogger.child('CreatePortable');
async function createPortable() { async function createPortable() {
const distDir = path.join(__dirname, '..', 'dist'); const distDir = path.join(__dirname, '..', 'dist');
@@ -99,7 +100,7 @@ pause
logger.info('📦 可以将整个文件夹打包分发给用户'); logger.info('📦 可以将整个文件夹打包分发给用户');
} catch (error) { } catch (error) {
console.error('❌ 创建便携版失败:', error); logger.error('❌ 创建便携版失败', error);
} }
} }