From 3f4e9c35b0502f9d8919c75a9c7dd54171a29e91 Mon Sep 17 00:00:00 2001 From: zdg Date: Mon, 2 Sep 2024 13:51:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B4=A9=E6=BA=83=E7=AD=89=EF=BC=8C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=96=87=E4=BB=B6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.js | 40 ++++++++++++++++++++++++++++--- src/main/logger.js | 52 ++++++++++++++++++++++++++++++++++++++++ src/main/store.js | 23 ++++++++++++++---- src/renderer/src/main.js | 6 ++++- 4 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 src/main/logger.js diff --git a/src/main/index.js b/src/main/index.js index 6381e86..3f475be 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,15 +3,17 @@ import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import icon from '../../resources/icon.png?asset' import File from './file' -import chat from './chat' // chat封装 -import Store from './store' // Store封装 +import Logger from './logger' // 日志封装 +import chat from './chat' // chat封装 +import Store from './store' // Store封装 import updateInit from './update' - // 代理 electron/remote // 第一步:引入remote import remote from '@electron/remote/main' // 第二步: 初始化remote remote.initialize() +// 日志配置-初始化(日志直接绑定到console上) +Logger.initialize() // 持久化数据-初始化 Store.initialize() @@ -153,6 +155,7 @@ async function createLinkWin(data) { // 初始化完成 app.on('ready', () => { + appWatchError() // 监听app错误 process.env.LANG = 'en_US.UTF-8' // 设置应用程序用户模型标识符 electronApp.setAppUserModelId('com.electron') @@ -259,3 +262,34 @@ function handleAll() { win.webContents.send('pinia-state-set', storeName, jsonStr) }) } + +// app 崩溃监听器 +function appWatchError() { + // 渲染进程崩溃 + app.on('renderer-process-crashed', (event, webContents, killed) => { + console.error( + `APP-ERROR:renderer-process-crashed; event: ${JSON.stringify(event)}; webContents:${JSON.stringify( + webContents + )}; killed:${JSON.stringify(killed)}` + ) + }) + + // GPU进程崩溃 + app.on('gpu-process-crashed', (event, killed) => { + console.error(`APP-ERROR:gpu-process-crashed; event: ${JSON.stringify(event)}; killed: ${JSON.stringify(killed)}`) + }) + + // 渲染进程结束 + app.on('render-process-gone', async (event, webContents, details) => { + console.error( + `APP-ERROR:render-process-gone; event: ${JSON.stringify(event)}; webContents:${JSON.stringify( + webContents + )}; details:${JSON.stringify(details)}` + ) + }) + + // 子进程结束 + app.on('child-process-gone', async (event, details) => { + console.error(`APP-ERROR:child-process-gone; event: ${JSON.stringify(event)}; details:${JSON.stringify(details)}`) + }) +} \ No newline at end of file diff --git a/src/main/logger.js b/src/main/logger.js new file mode 100644 index 0000000..03b6db3 --- /dev/null +++ b/src/main/logger.js @@ -0,0 +1,52 @@ +/** + * @description 日志配置 + * @author zdg + * @date 2021-07-05 14:07:01 + */ +// import log from 'electron-log' +import log from 'electron-log/main' +import { app } from 'electron' +import path from 'path' + +// 关闭控制台打印 +// 日志控制台等级,默认值:false +log.transports.console.level = false +// log.transports.console.level = 'info' +// 日志文件等级,默认值:false +log.transports.file.level = 'info' +// 日志文件名,默认:main.log +// log.transports.file.fileName = 'main.log'; +// 日志大小,默认:1048576(1M),达到最大上限后,备份文件并重命名为:main.old.log,有且仅有一个备份文件 +log.transports.file.maxSize = 10 * 1024 * 1024; // 文件最大不超过 10M +// 自定义日志文件滚动策略 +log.transports.file.rollSize = 10 * 1024 * 1024; // 10MB +// 日志格式,默认:[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text} +log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text}' +let date = new Date() +let dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() +// 文件位置及命名方式 +// 默认位置为:C:\Users\[user]\AppData\Roaming\[appname]\electron_log\ +// 文件名为:年-月-日.log +// 自定义文件保存位置为安装目录下 \log\年-月-日.log +// log.transports.file.resolvePathFn = () => 'logs\\' + dateStr+ '.log'; +log.transports.file.resolvePathFn = () => path.join(app.getPath('userData'), `logs/${dateStr}.log`) + +// 有六个日志级别error, warn, info, verbose, debug, silly。默认是silly +export const logger = { + error: (...args) => log.error(...args), + warn: (...args) => log.warn(...args), + info: (...args) => log.info(...args), + verbose: (...args) => log.verbose(...args), + debug: (...args) => log.debug(...args), + silly: (...args) => log.silly(...args) +} +export function initialize(bool = true, type = 'all') { + log.initialize() // 为渲染器进行初始化 + if (bool) { // 是否替换默认的console + if (type == 'all') Object.assign(console, log.functions) + else { // 替换指定类型 + console[type] = log[type] + } + } +} +export default { initialize } \ No newline at end of file diff --git a/src/main/store.js b/src/main/store.js index b2fd745..c37f8f5 100644 --- a/src/main/store.js +++ b/src/main/store.js @@ -19,7 +19,6 @@ const defaultData = { } }, local: { // 本地(永久localStorage) - }, } @@ -29,7 +28,15 @@ export function initialize(){ const sessionStore = new Store({ name: 'session-store', // 存储文件名 fileExtension: 'ini', // 文件后缀名 - encryptionKey: 'BvPLmgCC4DSIG0KkTec5' // 数据加密-防止用户直接改配置 + encryptionKey: 'BvPLmgCC4DSIG0KkTec5', // 数据加密-防止用户直接改配置 + beforeEachMigration: (store, context) => { // 版本迁移回调 + console.log(`[session-store] 迁移从 ${context.fromVersion} → ${context.toVersion}`); + }, + migrations: { // 版本变化 + '0.0.0': store => { + // store.set('debugPhase', true); + } + } }) sessionStore.clear() // 先清除-所有缓存数据 sessionStore.set(defaultData.session) // 初始化-默认数据 @@ -38,9 +45,17 @@ export function initialize(){ const localStore = new Store({ name: 'local-store', // 存储文件名 fileExtension: 'ini', // 文件后缀名 - encryptionKey: '6CyoHQmUaPmLzvVsh' // 数据加密-防止用户直接改配置 + encryptionKey: '6CyoHQmUaPmLzvVsh', // 数据加密-防止用户直接改配置 + beforeEachMigration: (store, context) => { // 版本迁移回调 + console.log(`[local-store] 迁移从 ${context.fromVersion} → ${context.toVersion}`); + }, + migrations: { // 版本变化 + '0.0.0': store => { + // store.set('debugPhase', true); + } + } }) - localStore.set(defaultData.local) // 初始化-默认数据 + localStore.set(defaultData.local) // 初始化-默认数据 return {sessionStore, localStore} } export default { initialize } \ No newline at end of file diff --git a/src/renderer/src/main.js b/src/renderer/src/main.js index f3a9a67..da34555 100644 --- a/src/renderer/src/main.js +++ b/src/renderer/src/main.js @@ -12,10 +12,14 @@ import 'virtual:windi.css' import { store } from '@/store' import App from './App.vue' import router from './router' +import log from 'electron-log/renderer' // 渲染进程日志-文件记录 + +if(process.env.NODE_ENV != 'development') { // 非开发环境,将日志打印到日志文件 + Object.assign(console, log.functions) // 渲染进程日志-控制台替换 +} const app = createApp(App) - app.use(router) .use(store) .use(ElementPlus, { locale: zhLocale }).mount('#app') \ No newline at end of file -- 2.44.0.windows.1