Skip to content

Commit

Permalink
bugfix: 多进程导致日志记录异常的问题修复 (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 authored Jan 13, 2025
1 parent 80e3f98 commit 8a96850
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/mitmproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'

Expand All @@ -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)
4 changes: 2 additions & 2 deletions packages/core/src/utils/util.log.js
Original file line number Diff line number Diff line change
@@ -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
84 changes: 57 additions & 27 deletions packages/core/src/utils/util.logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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
}
},
}
4 changes: 2 additions & 2 deletions packages/gui/src/bridge/mitmproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
4 changes: 2 additions & 2 deletions packages/gui/src/utils/util.log.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions packages/mitmproxy/src/utils/util.log.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8a96850

Please sign in to comment.