optimize: 历史控制台日志,记录到日志文件中。
parent
23db1994c3
commit
6e092c6999
|
@ -8,6 +8,7 @@ const Registry = require('winreg')
|
||||||
const log = require('../../../utils/util.log')
|
const log = require('../../../utils/util.log')
|
||||||
const Shell = require('../../shell')
|
const Shell = require('../../shell')
|
||||||
const extraPath = require('../extra-path')
|
const extraPath = require('../extra-path')
|
||||||
|
const dateUtil = require('../../../utils/util.date')
|
||||||
|
|
||||||
const execute = Shell.execute
|
const execute = Shell.execute
|
||||||
const execFile = Shell.execFile
|
const execFile = Shell.execFile
|
||||||
|
@ -98,23 +99,13 @@ function saveDomesticDomainAllowListFile (fileTxt) {
|
||||||
if (utimesErr) {
|
if (utimesErr) {
|
||||||
log.error('修改 domestic-domain-allowlist.txt 文件时间失败:', utimesErr)
|
log.error('修改 domestic-domain-allowlist.txt 文件时间失败:', utimesErr)
|
||||||
} else {
|
} 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 () {
|
function getDomesticDomainAllowList () {
|
||||||
loadConfig()
|
loadConfig()
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
},
|
||||||
|
|
||||||
|
}
|
|
@ -1,38 +1,41 @@
|
||||||
|
const dateUtil = require('./util.date')
|
||||||
|
|
||||||
let log = console
|
let log = console
|
||||||
|
|
||||||
// 将console中的日志缓存起来,当setLogger时,将控制台的日志写入日志文件
|
// 将console中的日志缓存起来,当setLogger时,将控制台的日志写入日志文件
|
||||||
let backups = []
|
let backupLogs = []
|
||||||
|
|
||||||
function backup (fun, args) {
|
function backup (fun, args) {
|
||||||
if (backups === null) {
|
if (backupLogs === null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
backups.push({
|
backupLogs.push({
|
||||||
fun,
|
fun,
|
||||||
args,
|
args,
|
||||||
|
time: dateUtil.format(new Date()),
|
||||||
})
|
})
|
||||||
|
|
||||||
// 最多缓存 100 条
|
// 最多缓存 100 条
|
||||||
if (backups.length > 100) {
|
if (backupLogs.length > 100) {
|
||||||
backups = backups.slice(1)
|
backupLogs = backupLogs.slice(1)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function printBackups () {
|
function printBackups () {
|
||||||
if (backups === null) {
|
if (backupLogs === null || log === console) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const backups0 = backups
|
const backups = backupLogs
|
||||||
backups = null
|
backupLogs = null // 先置空历史消息对象,再记录日志
|
||||||
|
|
||||||
for (const item of backups0) {
|
for (const item of backups) {
|
||||||
log[item.fun](...[`[console]`, ...item.args])
|
log[item.fun](...[`[${item.time}] console -`, ...item.args])
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
}
|
}
|
||||||
|
@ -49,11 +52,16 @@ function _doLog (fun, args) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
setLogger (logger) {
|
setLogger (logger) {
|
||||||
|
if (logger == null) {
|
||||||
|
log.error('logger 不能为空')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log = logger
|
log = logger
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (backups && backups.length > 0) {
|
if (backupLogs && backupLogs.length > 0) {
|
||||||
log.info('[util.log-or-console.js] logger已设置,现将控制台的日志记录到日志文件中')
|
log.info('[util.log-or-console.js] 日志系统已初始化完成,现开始将历史控制台信息记录到日志文件中:')
|
||||||
printBackups()
|
printBackups()
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
@ -8,6 +8,7 @@ const jsonApi = require('@docmirror/mitmproxy/src/json')
|
||||||
const pk = require('../../../package.json')
|
const pk = require('../../../package.json')
|
||||||
const configFromFiles = require('@docmirror/dev-sidecar/src/config/index.js').configFromFiles
|
const configFromFiles = require('@docmirror/dev-sidecar/src/config/index.js').configFromFiles
|
||||||
const log = require('../../utils/util.log')
|
const log = require('../../utils/util.log')
|
||||||
|
const dateUtil = require('@docmirror/dev-sidecar/src/utils/util.date')
|
||||||
|
|
||||||
const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
|
const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
|
||||||
process.env.DS_EXTRA_PATH = path.join(__dirname, '../extra/')
|
process.env.DS_EXTRA_PATH = path.join(__dirname, '../extra/')
|
||||||
|
@ -16,18 +17,6 @@ const getDefaultConfigBasePath = function () {
|
||||||
return DevSidecar.api.config.get().server.setting.userBasePath
|
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 = {
|
const localApi = {
|
||||||
/**
|
/**
|
||||||
* 返回所有api列表,供vue来ipc调用
|
* 返回所有api列表,供vue来ipc调用
|
||||||
|
@ -83,7 +72,7 @@ const localApi = {
|
||||||
|
|
||||||
if (setting.installTime == null) {
|
if (setting.installTime == null) {
|
||||||
// 设置安装时间
|
// 设置安装时间
|
||||||
setting.installTime = getDateTimeStr()
|
setting.installTime = dateUtil.now()
|
||||||
|
|
||||||
// 初始化 rootCa.setuped
|
// 初始化 rootCa.setuped
|
||||||
if (setting.rootCa == null) {
|
if (setting.rootCa == null) {
|
||||||
|
@ -216,5 +205,4 @@ export default {
|
||||||
},
|
},
|
||||||
devSidecar: DevSidecar,
|
devSidecar: DevSidecar,
|
||||||
invoke,
|
invoke,
|
||||||
getDateTimeStr,
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ const request = require('request')
|
||||||
const log = require('../../../utils/util.log')
|
const log = require('../../../utils/util.log')
|
||||||
const matchUtil = require('../../../utils/util.match')
|
const matchUtil = require('../../../utils/util.match')
|
||||||
const pac = require('./source/pac')
|
const pac = require('./source/pac')
|
||||||
|
const dateUtil = require('@docmirror/dev-sidecar/src/utils/util.date')
|
||||||
|
|
||||||
let pacClient = null
|
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` 文件中
|
// 保存 pac 内容到 `~/pac.txt` 文件中
|
||||||
function savePacFile (pacTxt) {
|
function savePacFile (pacTxt) {
|
||||||
const pacFilePath = getTmpPacFilePath()
|
const pacFilePath = getTmpPacFilePath()
|
||||||
|
@ -90,7 +81,7 @@ function savePacFile (pacTxt) {
|
||||||
if (utimesErr) {
|
if (utimesErr) {
|
||||||
log.error('修改 pac.txt 文件时间失败:', utimesErr)
|
log.error('修改 pac.txt 文件时间失败:', utimesErr)
|
||||||
} else {
|
} else {
|
||||||
log.info(`'${pacFilePath}' 文件的修改时间已更新为其最近更新时间 '${formatDate(lastModifiedTime)}'`)
|
log.info(`'${pacFilePath}' 文件的修改时间已更新为其最近更新时间 '${dateUtil.format(lastModifiedTime, false)}'`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue