Skip to content

Commit

Permalink
bugfix: 日志相关的配置未生效的问题修复。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jan 10, 2025
1 parent 6a43dcf commit 19fb8e3
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 20 deletions.
119 changes: 114 additions & 5 deletions packages/core/src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const fs = require('node:fs')
const path = require('node:path')
const lodash = require('lodash')
const jsonApi = require('@docmirror/mitmproxy/src/json')
const mergeApi = require('../merge')

function getUserBasePath () {
const userHome = process.env.USERPROFILE || process.env.HOME || '/'
Expand All @@ -13,7 +17,7 @@ function getRootCaKeyPath () {
return path.join(getUserBasePath(), '/dev-sidecar.ca.key.pem')
}

module.exports = {
const defaultConfig = {
app: {
mode: 'default',
autoStart: {
Expand All @@ -37,6 +41,10 @@ module.exports = {
},
closeStrategy: 0,
showShutdownTip: true,

// 日志相关配置
logFileSavePath: path.join(getUserBasePath(), '/logs'), // 日志文件保存路径
keepLogFileCount: 15, // 保留日志文件数
},
server: {
enabled: true,
Expand Down Expand Up @@ -69,10 +77,6 @@ module.exports = {

// 慢速IP延迟时间:测速超过该值时,则视为延迟高,显示为橙色
lowSpeedDelay: 200,

// 日志相关配置
logFileSavePath: path.join(getUserBasePath(), '/logs'), // 日志文件保存路径
keepLogFileCount: 15, // 保留日志文件数
},
compatible: {
// **** 自定义兼容配置 **** //
Expand Down Expand Up @@ -442,3 +446,108 @@ module.exports = {
],
},
}

// region 加载本地配置文件所需的方法

function _getConfigPath () {
const dir = defaultConfig.server.setting.userBasePath
if (!fs.existsSync(dir)) {
return null
}

// 兼容1.7.3及以下版本的配置文件处理逻辑
const newFilePath = path.join(dir, '/config.json')
const oldFilePath = path.join(dir, '/config.json5')
if (!fs.existsSync(newFilePath) && fs.existsSync(oldFilePath)) {
return oldFilePath // 如果新文件不存在,且旧文件存在,则返回旧文件路径
}
return newFilePath
}

function _getConfig () {
const configFilePath = _getConfigPath()
if (configFilePath == null) {
return {}
}

return jsonApi.parse(fs.readFileSync(configFilePath))
}

function _getRemoteSavePath (suffix = '') {
const dir = defaultConfig.server.setting.userBasePath
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return path.join(dir, `/remote_config${suffix}.json5`)
}

function _readRemoteConfigStr (suffix = '') {
if (defaultConfig.app.remoteConfig.enabled !== true) {
if (suffix === '_personal') {
if (!defaultConfig.app.remoteConfig.personalUrl) {
return '{}'
}
} else if (suffix === '') {
if (!defaultConfig.app.remoteConfig.url) {
return '{}'
}
}
return '{}'
}
try {
const path = _getRemoteSavePath(suffix)
if (fs.existsSync(path)) {
const file = fs.readFileSync(path)
return file.toString()
}
} catch {
}

return '{}'
}

function _readRemoteConfig (suffix = '') {
return jsonApi.parse(_readRemoteConfigStr(suffix))
}

function _getConfigFromFiles () {
const newConfig = _getConfig()

const merged = newConfig != null ? lodash.cloneDeep(newConfig) : {}

if (defaultConfig.app.remoteConfig.enabled === true) {
let personalRemoteConfig = null
let shareRemoteConfig = null

if (defaultConfig.app.remoteConfig.personalUrl) {
personalRemoteConfig = _readRemoteConfig('_personal')
mergeApi.doMerge(merged, personalRemoteConfig) // 先合并一次个人远程配置,使配置顺序在前
}
if (defaultConfig.app.remoteConfig.url) {
shareRemoteConfig = _readRemoteConfig()
mergeApi.doMerge(merged, shareRemoteConfig) // 先合并一次共享远程配置,使配置顺序在前
}
mergeApi.doMerge(merged, defaultConfig) // 合并默认配置,顺序排在最后
if (defaultConfig.app.remoteConfig.url) {
mergeApi.doMerge(merged, shareRemoteConfig) // 再合并一次共享远程配置,使配置生效
}
if (defaultConfig.app.remoteConfig.personalUrl) {
mergeApi.doMerge(merged, personalRemoteConfig) // 再合并一次个人远程配置,使配置生效
}
} else {
mergeApi.doMerge(merged, defaultConfig) // 合并默认配置
}
if (newConfig != null) {
mergeApi.doMerge(merged, newConfig) // 再合并一次用户配置,使用户配置重新生效
}
mergeApi.deleteNullItems(merged) // 删除为null及[delete]的项

return merged
}

// endregion

// 从本地文件中加载配置
defaultConfig.configFromFiles = _getConfigFromFiles()

module.exports = defaultConfig
12 changes: 6 additions & 6 deletions packages/core/src/utils/util.logger.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const path = require('node:path')
const log4js = require('log4js')
const config = require('../config/index')
const configFromFiles = require('../config/index').configFromFiles

const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info'

function getDefaultConfigBasePath () {
if (config.server.setting.logFileSavePath) {
let logFileSavePath = config.server.setting.logFileSavePath
if (configFromFiles.app.logFileSavePath) {
let logFileSavePath = configFromFiles.app.logFileSavePath
if (logFileSavePath.endsWith('/') || logFileSavePath.endsWith('\\')) {
logFileSavePath = logFileSavePath.slice(0, -1)
}
// eslint-disable-next-line no-template-curly-in-string
return logFileSavePath.replace('${userBasePath}', config.server.setting.userBasePath)
return logFileSavePath.replace('${userBasePath}', configFromFiles.server.setting.userBasePath)
} else {
return path.join(config.server.setting.userBasePath, '/logs')
return path.join(configFromFiles.server.setting.userBasePath, '/logs')
}
}

Expand All @@ -23,7 +23,7 @@ const guiLogFilename = path.join(getDefaultConfigBasePath(), '/gui.log')
const serverLogFilename = path.join(getDefaultConfigBasePath(), '/server.log')

// 日志相关配置
const backups = config.server.setting.keepLogFileCount // 保留日志文件数
const backups = configFromFiles.app.keepLogFileCount // 保留日志文件数
const appenderConfig = {
type: 'file',
pattern: 'yyyy-MM-dd',
Expand Down
4 changes: 4 additions & 0 deletions packages/gui/src/bridge/api/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import lodash from 'lodash'

const jsonApi = require('@docmirror/mitmproxy/src/json')
const pk = require('../../../package.json')
const configFromFiles = require('@docmirror/dev-sidecar/src/config/index.js').configFromFiles
const log = require('../../utils/util.log')

const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
Expand Down Expand Up @@ -50,6 +51,9 @@ const localApi = {
getConfigDir () {
return getDefaultConfigBasePath()
},
getLogDir () {
return configFromFiles.app.logFileSavePath || path.join(getDefaultConfigBasePath(), '/logs/')
},
getSystemPlatform (throwIfUnknown = false) {
return DevSidecar.api.shell.getSystemPlatform(throwIfUnknown)
},
Expand Down
4 changes: 2 additions & 2 deletions packages/gui/src/view/mixins/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ export default {
return this.systemPlatform === 'linux'
},
async openLog () {
const dir = await this.$api.info.getConfigDir()
this.$api.ipc.openPath(`${dir}/logs/`)
const dir = await this.$api.info.getLogDir()
this.$api.ipc.openPath(dir)
},
},
}
13 changes: 10 additions & 3 deletions packages/gui/src/view/pages/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,18 @@ export default {
</div>
</a-form-item>
<hr>
<a-form-item label="日志文件保存路径" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input v-model="config.server.setting.logFileSavePath" />
<a-form-item label="日志文件保存目录" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input v-model="config.app.logFileSavePath" />
<div class="form-help">
修改后,重启DS才生效!<br>
注意:原目录中的文件不会自动转移到新的目录,请自行转移或删除。
</div>
</a-form-item>
<a-form-item label="保留日志文件数" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input-number v-model="config.server.setting.keepLogFileCount" :step="1" :min="0" />
<a-input-number v-model="config.app.keepLogFileCount" :step="1" :min="0" />
<div class="form-help">
修改后,重启DS才生效!
</div>
</a-form-item>
</div>
<template slot="footer">
Expand Down
6 changes: 2 additions & 4 deletions packages/mitmproxy/src/json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
let JSON5 = require('json5')
const log = require('./utils/util.log')

if (JSON5.default) {
JSON5 = JSON5.default
Expand All @@ -17,11 +16,10 @@ module.exports = {
stringify2 (obj) {
try {
return JSON.stringify(obj)
} catch (e) {
} catch {
try {
return JSON5.stringify(obj)
} catch (e2) {
log.debug('转换为JSON字符串失败, error:', e, ', obj:', obj)
} catch {
return obj
}
}
Expand Down

0 comments on commit 19fb8e3

Please sign in to comment.