From 8a96850e4274f777baad744a2618c091a49db8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Mon, 13 Jan 2025 16:44:57 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E5=A4=9A=E8=BF=9B=E7=A8=8B=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D=20(#434)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/mitmproxy.js | 4 +- packages/core/src/utils/util.log.js | 4 +- packages/core/src/utils/util.logger.js | 84 ++++++++++++++++-------- packages/gui/src/bridge/mitmproxy.js | 4 +- packages/gui/src/utils/util.log.js | 4 +- packages/mitmproxy/src/utils/util.log.js | 4 +- 6 files changed, 67 insertions(+), 37 deletions(-) diff --git a/packages/cli/src/mitmproxy.js b/packages/cli/src/mitmproxy.js index 19ad90e7b9..7612972ac9 100644 --- a/packages/cli/src/mitmproxy.js +++ b/packages/cli/src/mitmproxy.js @@ -2,7 +2,7 @@ const fs = require('node:fs') const path = require('node:path') const server = require('@docmirror/mitmproxy') const jsonApi = require('@docmirror/mitmproxy/src/json') -const log = require('@docmirror/mitmproxy/src/utils/util.log') +const log = require('@docmirror/mitmproxy/src/utils/util.log') // 当前脚本是在 server 的进程中执行的,所以使用 mitmproxy 中的logger const home = process.env.USER_HOME || process.env.HOME || 'C:/Users/Administrator/' @@ -21,5 +21,5 @@ const config = jsonApi.parse(configJson.toString()) // const pacFilePath = '../../gui/extra/pac/pac.txt' // config.plugin.overwall.pac.customPacFilePath = path.join(__dirname, pacFilePath) config.setting.rootDir = path.join(__dirname, '../../gui/') -log.info(`start mitmproxy config by cli: 读取配置文件: ${configPath}`) +log.info(`start mitmproxy by cli, configPath: ${configPath}`) server.start(config) diff --git a/packages/core/src/utils/util.log.js b/packages/core/src/utils/util.log.js index 5c73495142..bc0a904362 100644 --- a/packages/core/src/utils/util.log.js +++ b/packages/core/src/utils/util.log.js @@ -1,5 +1,5 @@ -const log4js = require('./util.logger') +const loggerFactory = require('./util.logger') -const logger = log4js.getLogger('core') +const logger = loggerFactory.getLogger('core') module.exports = logger diff --git a/packages/core/src/utils/util.logger.js b/packages/core/src/utils/util.logger.js index bc90321424..60697aa969 100644 --- a/packages/core/src/utils/util.logger.js +++ b/packages/core/src/utils/util.logger.js @@ -2,6 +2,7 @@ const path = require('node:path') const log4js = require('log4js') const configFromFiles = require('../config/index').configFromFiles +// 日志级别 const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info' function getDefaultConfigBasePath () { @@ -17,39 +18,68 @@ function getDefaultConfigBasePath () { } } -// 日志文件名 -const coreLogFilename = path.join(getDefaultConfigBasePath(), '/core.log') -const guiLogFilename = path.join(getDefaultConfigBasePath(), '/gui.log') -const serverLogFilename = path.join(getDefaultConfigBasePath(), '/server.log') +// 日志文件目录 +const basePath = getDefaultConfigBasePath() -// 日志相关配置 -const backups = configFromFiles.app.keepLogFileCount // 保留日志文件数 +// 通用日志配置 const appenderConfig = { type: 'file', pattern: 'yyyy-MM-dd', - keepFileExt: true, // 保留日志文件扩展名 compress: true, // 压缩日志文件 + keepFileExt: true, // 保留日志文件扩展名为 .log + backups: configFromFiles.app.keepLogFileCount, // 保留日志文件数 +} + +let log = null + +// 设置一组日志配置 +function log4jsConfigure (categories) { + const config = { + appenders: { + std: { type: 'stdout' }, + }, + categories: { + default: { appenders: ['std'], level }, + }, + } + + for (const category of categories) { + config.appenders[category] = { ...appenderConfig, filename: path.join(basePath, `/${category}.log`) } + config.categories[category] = { appenders: [category, 'std'], level } + } + + log4js.configure(config) + + // 拿第一个日志类型来logger并设置到log变量中 + log = log4js.getLogger(categories[0]) - // 以下三个配置都设置,兼容新旧版本 - backups, - numBackups: backups, - daysToKeep: backups, + log.info('设置日志配置完成:', config) } -// 设置日志配置 -log4js.configure({ - appenders: { - std: { type: 'stdout' }, - core: { ...appenderConfig, filename: coreLogFilename }, - gui: { ...appenderConfig, filename: guiLogFilename }, - server: { ...appenderConfig, filename: serverLogFilename }, - }, - categories: { - default: { appenders: ['std'], level }, - core: { appenders: ['core', 'std'], level }, - gui: { appenders: ['gui', 'std'], level }, - server: { appenders: ['server', 'std'], level }, - }, -}) +module.exports = { + getLogger (category) { + if (!category) { + if (log) { + log.error('未指定日志类型,无法配置并获取日志对象!!!') + } + throw new Error('未指定日志类型,无法配置并获取日志对象!!!') + } + + if (category === 'core' || category === 'gui') { + // core 和 gui 的日志配置,因为它们在同一进程中,所以一起配置,且只能配置一次 + if (log == null) { + log4jsConfigure(['core', 'gui']) + } -module.exports = log4js + return log4js.getLogger(category) + } else { + if (log == null) { + log4jsConfigure([category]) + } else { + log.error(`当前进程已经设置过日志配置,无法设置 "${category}" 的配置,先临时返回 "${log.category}" 的 log 进行日志记录。如果与其他类型的日志在同一进程中写入,请参照 core 和 gui 一起配置`) + } + + return log + } + }, +} diff --git a/packages/gui/src/bridge/mitmproxy.js b/packages/gui/src/bridge/mitmproxy.js index e5dedc8243..bd7f3f0e57 100644 --- a/packages/gui/src/bridge/mitmproxy.js +++ b/packages/gui/src/bridge/mitmproxy.js @@ -2,7 +2,7 @@ const fs = require('node:fs') const path = require('node:path') const server = require('@docmirror/mitmproxy') const jsonApi = require('@docmirror/mitmproxy/src/json') -const log = require('../utils/util.log') +const log = require('@docmirror/mitmproxy/src/utils/util.log') // 当前脚本是在 server 的进程中执行的,所以使用 mitmproxy 中的logger const configPath = process.argv[2] const configJson = fs.readFileSync(configPath) @@ -13,5 +13,5 @@ const config = jsonApi.parse(configJson.toString()) // const pacFilePath = '../extra/pac/pac.txt' // config.plugin.overwall.pac.customPacFilePath = path.join(__dirname, pacFilePath) config.setting.rootDir = path.join(__dirname, '../') -log.info(`start mitmproxy config by gui bridge: ${configPath}`) +log.info(`start mitmproxy by gui bridge, configPath: ${configPath}`) server.start(config) diff --git a/packages/gui/src/utils/util.log.js b/packages/gui/src/utils/util.log.js index cdf77c997c..c95af6c22d 100644 --- a/packages/gui/src/utils/util.log.js +++ b/packages/gui/src/utils/util.log.js @@ -1,5 +1,5 @@ -const log4js = require('@docmirror/dev-sidecar/src/utils/util.logger') +const loggerFactory = require('@docmirror/dev-sidecar/src/utils/util.logger') -const logger = log4js.getLogger('gui') +const logger = loggerFactory.getLogger('gui') module.exports = logger diff --git a/packages/mitmproxy/src/utils/util.log.js b/packages/mitmproxy/src/utils/util.log.js index cb9a3e43ec..5714ee9f6c 100644 --- a/packages/mitmproxy/src/utils/util.log.js +++ b/packages/mitmproxy/src/utils/util.log.js @@ -1,5 +1,5 @@ -const log4js = require('@docmirror/dev-sidecar/src/utils/util.logger') +const loggerFactory = require('@docmirror/dev-sidecar/src/utils/util.logger') -const logger = log4js.getLogger('server') +const logger = loggerFactory.getLogger('server') module.exports = logger