feat: 日志功能和文案优化

This commit is contained in:
digua
2025-12-20 12:47:09 +08:00
parent fdc195ba8d
commit 76573cb18b
8 changed files with 80 additions and 15 deletions
+2 -2
View File
@@ -20,9 +20,9 @@ function getLogDir(): string {
try {
const docPath = app.getPath('documents')
LOG_DIR = path.join(docPath, 'ChatLab', 'logs')
LOG_DIR = path.join(docPath, 'ChatLab', 'logs', 'ai')
} catch {
LOG_DIR = path.join(process.cwd(), 'logs')
LOG_DIR = path.join(process.cwd(), 'logs', 'ai')
}
return LOG_DIR
+1 -1
View File
@@ -105,7 +105,7 @@ export function registerCacheHandlers(_context: IpcContext): void {
{
id: 'logs',
name: '日志文件',
description: '调试日志和错误日志',
description: '软件的运行日志,包含导入、AI、错误日志',
path: path.join(chatLabDir, 'logs'),
icon: 'i-heroicons-document-text',
canClear: true, // 可以清理
+65
View File
@@ -0,0 +1,65 @@
/**
* 简单的日志工具
* 日志保存到 Documents/ChatLab/logs/ 目录
*/
import { app } from 'electron'
import * as fs from 'fs'
import * as path from 'path'
// 日志目录(与 cache.ts 保持一致)
function getLogDir(): string {
try {
const docPath = app.getPath('documents')
return path.join(docPath, 'ChatLab', 'logs')
} catch {
return path.join(process.cwd(), 'ChatLab', 'logs')
}
}
// 日志文件路径
function getLogPath(): string {
return path.join(getLogDir(), 'app.log')
}
// 确保日志目录存在
function ensureLogDir(): void {
const logDir = getLogDir()
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true })
}
}
// 格式化时间
function formatTime(): string {
const now = new Date()
const year = now.getFullYear()
const month = String(now.getMonth() + 1).padStart(2, '0')
const day = String(now.getDate()).padStart(2, '0')
const hour = String(now.getHours()).padStart(2, '0')
const minute = String(now.getMinutes()).padStart(2, '0')
const second = String(now.getSeconds()).padStart(2, '0')
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}
// 写入日志
function writeLog(level: string, message: string): void {
try {
ensureLogDir()
const logLine = `[${formatTime()}] [${level}] ${message}\n`
fs.appendFileSync(getLogPath(), logLine, 'utf-8')
} catch (error) {
// 日志写入失败时静默处理,避免影响主程序
console.error('[Logger] 写入日志失败:', error)
}
}
/**
* 日志工具
*/
export const logger = {
info: (message: string) => writeLog('INFO', message),
warn: (message: string) => writeLog('WARN', message),
error: (message: string) => writeLog('ERROR', message),
debug: (message: string) => writeLog('DEBUG', message),
}
+7 -6
View File
@@ -1,6 +1,7 @@
import { dialog, app } from 'electron'
import { autoUpdater } from 'electron-updater'
import { platform } from '@electron-toolkit/utils'
import { logger } from './logger'
let isFirstShow = true
const checkUpdate = (win) => {
@@ -59,7 +60,8 @@ const checkUpdate = (win) => {
console.log('wait for post download operation')
})
.catch((downloadError) => {
dialog.showErrorBox('客户端下载失败', `err:${downloadError}`)
// 下载失败记录到日志,不显示给用户
logger.error(`[Update] 下载更新失败: ${downloadError}`)
})
}
})
@@ -109,11 +111,10 @@ const checkUpdate = (win) => {
}
})
// 错误处理
autoUpdater.on('error', (err, ev) => {
// 更新出错,其中一步错误都会emit
console.log('error事件:', err, ev)
dialog.showErrorBox('遇到错误', `err:${err}, ev:${ev}`)
// 错误处理(静默处理,记录到日志)
autoUpdater.on('error', (err) => {
// 更新错误记录到日志,不显示给用户
logger.error(`[Update] 更新错误: ${err.message || err}`)
})
// 等待 3 秒再检查更新,确保窗口准备完成,用户进入系统
+1 -2
View File
@@ -17,7 +17,7 @@ let currentLogFile: string | null = null
*/
function getLogDir(): string {
const dbDir = getDbDir()
const logDir = path.join(path.dirname(dbDir), 'logs')
const logDir = path.join(path.dirname(dbDir), 'logs', 'import')
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true })
}
@@ -106,4 +106,3 @@ export function resetPerfLog(): void {
export function getCurrentLogFile(): string | null {
return currentLogFile
}