Skip to content

Commit

Permalink
feature: 1)启用日志压缩;2)日志保留数和保存路径可配置化。 (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 authored Jan 10, 2025
1 parent cb7a3c6 commit 3a53756
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ module.exports = {

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

// 日志相关配置
logFileSavePath: path.join(getUserBasePath(), '/logs'), // 日志文件保存路径
keepLogFileCount: 15, // 保留日志文件数
},
compatible: {
// **** 自定义兼容配置 **** //
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/util.log.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const log4js = require('./util.logger')

const logger = log4js.getLogger('core')

module.exports = logger
39 changes: 32 additions & 7 deletions packages/core/src/utils/util.logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,44 @@ const config = require('../config/index')
const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info'

function getDefaultConfigBasePath () {
return config.server.setting.userBasePath
if (config.server.setting.logFileSavePath) {
let logFileSavePath = config.server.setting.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)
} else {
return path.join(config.server.setting.userBasePath, '/logs')
}
}

const coreLogFilename = path.join(getDefaultConfigBasePath(), '/logs/core.log')
const guiLogFilename = path.join(getDefaultConfigBasePath(), '/logs/gui.log')
const serverLogFilename = path.join(getDefaultConfigBasePath(), '/logs/server.log')
// 日志文件名
const coreLogFilename = path.join(getDefaultConfigBasePath(), '/core.log')
const guiLogFilename = path.join(getDefaultConfigBasePath(), '/gui.log')
const serverLogFilename = path.join(getDefaultConfigBasePath(), '/server.log')

// 日志相关配置
const backups = config.server.setting.keepLogFileCount // 保留日志文件数
const appenderConfig = {
type: 'file',
pattern: 'yyyy-MM-dd',
keepFileExt: true, // 保留日志文件扩展名
compress: true, // 压缩日志文件

// 以下三个配置都设置,兼容新旧版本
backups,
numBackups: backups,
daysToKeep: backups,
}

// 设置日志配置
log4js.configure({
appenders: {
std: { type: 'stdout' },
core: { type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename: coreLogFilename },
gui: { type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename: guiLogFilename },
server: { level: 'debug', type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename: serverLogFilename },
core: { ...appenderConfig, filename: coreLogFilename },
gui: { ...appenderConfig, filename: guiLogFilename },
server: { ...appenderConfig, filename: serverLogFilename },
},
categories: {
default: { appenders: ['std'], level },
Expand Down
7 changes: 7 additions & 0 deletions packages/gui/src/view/pages/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ export default {
预发布版本号为带有 “<code>-</code>” 的版本。注:该配置只对当前版本为正式版本时有效。
</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>
<a-form-item label="保留日志文件数" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input-number v-model="config.server.setting.keepLogFileCount" :step="1" :min="0" />
</a-form-item>
</div>
<template slot="footer">
<div class="footer-bar">
Expand Down

0 comments on commit 3a53756

Please sign in to comment.