optimize: 历史控制台日志,记录到日志文件中。

pull/456/head
王良 2025-02-12 12:11:17 +08:00
parent 23db1994c3
commit 6e092c6999
5 changed files with 49 additions and 48 deletions

View File

@ -8,6 +8,7 @@ const Registry = require('winreg')
const log = require('../../../utils/util.log')
const Shell = require('../../shell')
const extraPath = require('../extra-path')
const dateUtil = require('../../../utils/util.date')
const execute = Shell.execute
const execFile = Shell.execFile
@ -98,23 +99,13 @@ function saveDomesticDomainAllowListFile (fileTxt) {
if (utimesErr) {
log.error('修改 domestic-domain-allowlist.txt 文件时间失败:', utimesErr)
} else {
log.info(`'${filePath}' 文件的修改时间已更新为其最近更新时间 '${formatDate(lastModifiedTime)}'`)
log.info(`'${filePath}' 文件的修改时间已更新为其最近更新时间 '${dateUtil.format(lastModifiedTime, false)}'`)
}
})
})
}
}
function formatDate (date) {
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const hours = date.getHours().toString().padStart(2, '0')
const minutes = date.getMinutes().toString().padStart(2, '0')
const seconds = date.getSeconds().toString().padStart(2, '0')
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
function getDomesticDomainAllowList () {
loadConfig()

View File

@ -0,0 +1,23 @@
module.exports = {
format (date, needMill = true) {
if (date == null) {
return 'null'
}
const year = date.getFullYear() // 获取年份
const month = (date.getMonth() + 1).toString().padStart(2, '0') // 获取月份(注意月份从 0 开始计数)
const day = date.getDate().toString().padStart(2, '0') // 获取天数
const hours = date.getHours().toString().padStart(2, '0') // 获取小时
const minutes = date.getMinutes().toString().padStart(2, '0') // 获取分钟
const seconds = date.getSeconds().toString().padStart(2, '0') // 获取秒数
const milliseconds = needMill ? `.${date.getMilliseconds().toString().padStart(3, '0')}` : '' // 获取毫秒
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}${milliseconds}`
},
now (needMill = true) {
return this.format(new Date(), needMill)
},
}

View File

@ -1,38 +1,41 @@
const dateUtil = require('./util.date')
let log = console
// 将console中的日志缓存起来当setLogger时将控制台的日志写入日志文件
let backups = []
let backupLogs = []
function backup (fun, args) {
if (backups === null) {
if (backupLogs === null) {
return
}
try {
backups.push({
backupLogs.push({
fun,
args,
time: dateUtil.format(new Date()),
})
// 最多缓存 100 条
if (backups.length > 100) {
backups = backups.slice(1)
if (backupLogs.length > 100) {
backupLogs = backupLogs.slice(1)
}
} catch {
}
}
function printBackups () {
if (backups === null) {
if (backupLogs === null || log === console) {
return
}
try {
const backups0 = backups
backups = null
const backups = backupLogs
backupLogs = null // 先置空历史消息对象,再记录日志
for (const item of backups0) {
log[item.fun](...[`[console]`, ...item.args])
for (const item of backups) {
log[item.fun](...[`[${item.time}] console -`, ...item.args])
}
} catch {
}
@ -49,11 +52,16 @@ function _doLog (fun, args) {
module.exports = {
setLogger (logger) {
if (logger == null) {
log.error('logger 不能为空')
return
}
log = logger
try {
if (backups && backups.length > 0) {
log.info('[util.log-or-console.js] logger已设置现将控制台的日志记录到日志文件中')
if (backupLogs && backupLogs.length > 0) {
log.info('[util.log-or-console.js] 日志系统已初始化完成,现开始将历史控制台信息记录到日志文件中:')
printBackups()
}
} catch {

View File

@ -8,6 +8,7 @@ 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 dateUtil = require('@docmirror/dev-sidecar/src/utils/util.date')
const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
process.env.DS_EXTRA_PATH = path.join(__dirname, '../extra/')
@ -16,18 +17,6 @@ const getDefaultConfigBasePath = function () {
return DevSidecar.api.config.get().server.setting.userBasePath
}
const getDateTimeStr = function () {
const date = new Date() // 创建一个表示当前日期和时间的 Date 对象
const year = date.getFullYear() // 获取年份
const month = String(date.getMonth() + 1).padStart(2, '0') // 获取月份(注意月份从 0 开始计数)
const day = String(date.getDate()).padStart(2, '0') // 获取天数
const hours = String(date.getHours()).padStart(2, '0') // 获取小时
const minutes = String(date.getMinutes()).padStart(2, '0') // 获取分钟
const seconds = String(date.getSeconds()).padStart(2, '0') // 获取秒数
const milliseconds = String(date.getMilliseconds()).padStart(3, '0') // 获取毫秒
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`
}
const localApi = {
/**
* 返回所有api列表供vue来ipc调用
@ -83,7 +72,7 @@ const localApi = {
if (setting.installTime == null) {
// 设置安装时间
setting.installTime = getDateTimeStr()
setting.installTime = dateUtil.now()
// 初始化 rootCa.setuped
if (setting.rootCa == null) {
@ -216,5 +205,4 @@ export default {
},
devSidecar: DevSidecar,
invoke,
getDateTimeStr,
}

View File

@ -7,6 +7,7 @@ const request = require('request')
const log = require('../../../utils/util.log')
const matchUtil = require('../../../utils/util.match')
const pac = require('./source/pac')
const dateUtil = require('@docmirror/dev-sidecar/src/utils/util.date')
let pacClient = null
@ -55,16 +56,6 @@ function loadPacLastModifiedTime (pacTxt) {
}
}
function formatDate (date) {
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const hours = date.getHours().toString().padStart(2, '0')
const minutes = date.getMinutes().toString().padStart(2, '0')
const seconds = date.getSeconds().toString().padStart(2, '0')
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
// 保存 pac 内容到 `~/pac.txt` 文件中
function savePacFile (pacTxt) {
const pacFilePath = getTmpPacFilePath()
@ -90,7 +81,7 @@ function savePacFile (pacTxt) {
if (utimesErr) {
log.error('修改 pac.txt 文件时间失败:', utimesErr)
} else {
log.info(`'${pacFilePath}' 文件的修改时间已更新为其最近更新时间 '${formatDate(lastModifiedTime)}'`)
log.info(`'${pacFilePath}' 文件的修改时间已更新为其最近更新时间 '${dateUtil.format(lastModifiedTime, false)}'`)
}
})
})