From 9e5d11c3c8fbcd19e731457d5ca0c82d2eb19e1e Mon Sep 17 00:00:00 2001 From: xmk23333 Date: Thu, 1 Jan 2026 10:16:49 +0800 Subject: [PATCH] refactor: replace console.log with createLogger in main process --- src/main/config/controledMihomo.ts | 5 ++++- src/main/config/profile.ts | 13 ++++++++----- src/main/core/factory.ts | 5 ++++- src/main/core/mihomoApi.ts | 25 ++++++++++++++----------- src/main/index.ts | 7 +++++-- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/main/config/controledMihomo.ts b/src/main/config/controledMihomo.ts index 88451f7..9250deb 100644 --- a/src/main/config/controledMihomo.ts +++ b/src/main/config/controledMihomo.ts @@ -6,6 +6,9 @@ import { getAppConfig } from './app' import { defaultControledMihomoConfig } from '../utils/template' import { deepMerge } from '../utils/merge' import { existsSync } from 'fs' +import { createLogger } from '../utils/logger' + +const controledMihomoLogger = createLogger('ControledMihomo') let controledMihomoConfig: Partial // mihomo.yaml @@ -23,7 +26,7 @@ export async function getControledMihomoConfig(force = false): Promise = Promise.resolve() @@ -320,16 +323,16 @@ export async function setProfileStr(id: string, content: string): Promise const { generateProfile } = await import('../core/factory') await generateProfile() await mihomoUpgradeConfig() - console.log('[Profile] Config reloaded successfully using mihomoUpgradeConfig') + profileLogger.info('Config reloaded successfully using mihomoUpgradeConfig') } catch (error) { - console.error('[Profile] Failed to reload config with mihomoUpgradeConfig:', error) + profileLogger.error('Failed to reload config with mihomoUpgradeConfig', error) try { - console.log('[Profile] Falling back to restart core') + profileLogger.info('Falling back to restart core') const { restartCore } = await import('../core/manager') await restartCore() - console.log('[Profile] Core restarted successfully') + profileLogger.info('Core restarted successfully') } catch (restartError) { - console.error('[Profile] Failed to restart core:', restartError) + profileLogger.error('Failed to restart core', restartError) throw restartError } } diff --git a/src/main/core/factory.ts b/src/main/core/factory.ts index 479fbe4..9614cd8 100644 --- a/src/main/core/factory.ts +++ b/src/main/core/factory.ts @@ -21,6 +21,9 @@ import { deepMerge } from '../utils/merge' import vm from 'vm' import { existsSync, writeFileSync } from 'fs' import path from 'path' +import { createLogger } from '../utils/logger' + +const factoryLogger = createLogger('Factory') let runtimeConfigStr: string let runtimeConfig: IMihomoConfig @@ -132,7 +135,7 @@ export async function generateProfile(): Promise { } } } catch (error) { - console.error('读取或应用规则文件时出错:', error) + factoryLogger.error('Failed to read or apply rule file', error) } const profile = deepMerge(currentProfile, controledMihomoConfig) diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index a372551..d0de532 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -7,6 +7,9 @@ import { calcTraffic } from '../utils/calc' import { getRuntimeConfig } from './factory' import { floatingWindow } from '../resolve/floatingWindow' import { getMihomoIpcPath } from './manager' +import { createLogger } from '../utils/logger' + +const mihomoApiLogger = createLogger('MihomoApi') let axiosIns: AxiosInstance = null! let currentIpcPath: string = '' @@ -30,7 +33,7 @@ export const getAxios = async (force: boolean = false): Promise = } currentIpcPath = dynamicIpcPath - console.log(`[mihomoApi] Creating axios instance with path: ${dynamicIpcPath}`) + mihomoApiLogger.info(`Creating axios instance with path: ${dynamicIpcPath}`) axiosIns = axios.create({ baseURL: `http://localhost`, @@ -44,9 +47,9 @@ export const getAxios = async (force: boolean = false): Promise = }, (error) => { if (error.code === 'ENOENT') { - console.debug(`[mihomoApi] Pipe not ready: ${error.config?.socketPath}`) + mihomoApiLogger.debug(`Pipe not ready: ${error.config?.socketPath}`) } else { - console.error(`[mihomoApi] Axios error with path ${dynamicIpcPath}:`, error.message) + mihomoApiLogger.error(`Axios error with path ${dynamicIpcPath}: ${error.message}`) } if (error.response && error.response.data) { @@ -191,28 +194,28 @@ export const mihomoUpgradeUI = async (): Promise => { } export const mihomoUpgradeConfig = async (): Promise => { - console.log('[mihomoApi] mihomoUpgradeConfig called') + mihomoApiLogger.info('mihomoUpgradeConfig called') try { const instance = await getAxios() - console.log('[mihomoApi] axios instance obtained') + mihomoApiLogger.info('axios instance obtained') const { diffWorkDir = false } = await getAppConfig() const { current } = await import('../config').then((mod) => mod.getProfileConfig(true)) const { mihomoWorkConfigPath } = await import('../utils/dirs') const configPath = diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work') - console.log('[mihomoApi] config path:', configPath) + mihomoApiLogger.info(`config path: ${configPath}`) const { existsSync } = await import('fs') if (!existsSync(configPath)) { - console.log('[mihomoApi] config file does not exist, generating...') + mihomoApiLogger.info('config file does not exist, generating...') const { generateProfile } = await import('./factory') await generateProfile() } const response = await instance.put('/configs?force=true', { path: configPath }) - console.log('[mihomoApi] config upgrade request completed', response?.status || 'no status') + mihomoApiLogger.info(`config upgrade request completed ${response?.status || 'no status'}`) } catch (error) { - console.error('[mihomoApi] Failed to upgrade config:', error) + mihomoApiLogger.error('Failed to upgrade config', error) throw error } } @@ -255,7 +258,7 @@ const mihomoTraffic = async (): Promise => { const dynamicIpcPath = getMihomoIpcPath() const wsUrl = `ws+unix:${dynamicIpcPath}:/traffic` - console.log(`[mihomoApi] Creating traffic WebSocket with URL: ${wsUrl}`) + mihomoApiLogger.info(`Creating traffic WebSocket with URL: ${wsUrl}`) mihomoTrafficWs = new WebSocket(wsUrl) mihomoTrafficWs.onmessage = async (e): Promise => { @@ -286,7 +289,7 @@ const mihomoTraffic = async (): Promise => { } mihomoTrafficWs.onerror = (error): void => { - console.error(`[mihomoApi] Traffic WebSocket error:`, error) + mihomoApiLogger.error('Traffic WebSocket error', error) if (mihomoTrafficWs) { mihomoTrafficWs.close() mihomoTrafficWs = null diff --git a/src/main/index.ts b/src/main/index.ts index d100546..8f5e392 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -19,7 +19,10 @@ import { showFloatingWindow } from './resolve/floatingWindow' import { initI18n } from '../shared/i18n' import i18next from 'i18next' import { logger } from './utils/logger' +import { createLogger } from './utils/logger' import { initWebdavBackupScheduler } from './resolve/backup' + +const mainLogger = createLogger('Main') import { createWindow, mainWindow, @@ -93,7 +96,7 @@ async function checkHighPrivilegeCoreEarly(): Promise { process.exit(0) } } catch (e) { - console.error('Failed to check high privilege core:', e) + mainLogger.error('Failed to check high privilege core', e) } } @@ -105,7 +108,7 @@ async function initHardwareAcceleration(): Promise { app.disableHardwareAcceleration() } } catch (e) { - console.warn('Failed to read hardware acceleration config:', e) + mainLogger.warn('Failed to read hardware acceleration config', e) } }