feat(logger): sync renderer log level cache and support verbose IPC

- Add LOGGER_LEVEL_CHANGED IPC channel for broadcasting level changes
- setLogLevel() now notifies all BrowserWindows when level changes
- Add verbose case in IPC forwardToWinston (was falling through to info)
- Renderer logger API listens for level changes and updates cached level
- Add cleanup() method to remove level change listener

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-04-03 21:18:56 +08:00
parent 21359b31c6
commit a2e3681c8f
4 changed files with 31 additions and 4 deletions

View File

@@ -9,7 +9,7 @@
* - Error-level logs bypass circuit breaker
*/
import { ipcMain } from 'electron'
import { ipcMain, BrowserWindow } from 'electron'
import winston from 'winston'
import { createLogger } from '../services/logger'
import logger from '../services/logger'
@@ -161,6 +161,9 @@ class LoggerHandlerState {
: entry.message
switch (entry.level) {
case 'verbose':
childLogger.verbose(message, entry.context)
break
case 'debug':
childLogger.debug(message, entry.context)
break

View File

@@ -11,8 +11,10 @@
import winston from 'winston'
import DailyRotateFile from 'winston-daily-rotate-file'
import path from 'path'
import { BrowserWindow } from 'electron'
import { serializeError, sanitizeError } from './error-utils'
import { getLogDir, isProduction } from './shared'
import { IPC_CHANNELS } from '../../../shared/ipc-channels'
// Cache isProduction() at module load — app.isPackaged never changes at runtime
const IS_PROD = isProduction()
@@ -113,11 +115,18 @@ const logger = winston.createLogger({
})
/**
* Update the logger level dynamically
* Update the logger level dynamically and notify renderer processes
* @param level - The new log level
*/
export function setLogLevel(level: string): void {
logger.level = level
// Broadcast level change to all renderer windows so they update their cached level
for (const win of BrowserWindow.getAllWindows()) {
if (!win.isDestroyed()) {
win.webContents.send(IPC_CHANNELS.LOGGER_LEVEL_CHANGED, level)
}
}
}
/**