🐛 Fix: some case will cause picgo-gui-local.log too large

This commit is contained in:
PiEgg 2022-10-17 17:53:15 +08:00
parent ae87a512a1
commit 3c01861122

View File

@ -7,17 +7,25 @@ const checkLogFileIsLarge = (logPath: string): {
logFileSize?: number logFileSize?: number
logFileSizeLimit?: number logFileSizeLimit?: number
} => { } => {
if (fs.existsSync(logPath)) { try {
const logFileSize = fs.statSync(logPath).size if (fs.existsSync(logPath)) {
const logFileSizeLimit = 10 * 1024 * 1024 // 10 MB default const logFileSize = fs.statSync(logPath).size
return { const logFileSizeLimit = 10 * 1024 * 1024 // 10 MB default
isLarge: logFileSize > logFileSizeLimit, return {
logFileSize, isLarge: logFileSize > logFileSizeLimit,
logFileSizeLimit logFileSize,
logFileSizeLimit
}
}
return {
isLarge: false
}
} catch (e) {
// why throw error???
console.error(e)
return {
isLarge: true
} }
}
return {
isLarge: false
} }
} }
@ -32,11 +40,17 @@ const recreateLogFile = (logPath: string): void => {
* for local log before picgo inited * for local log before picgo inited
*/ */
const getLogger = (logPath: string) => { const getLogger = (logPath: string) => {
if (!fs.existsSync(logPath)) { let hasUncathcedError = false
fs.ensureFileSync(logPath) try {
} if (!fs.existsSync(logPath)) {
if (checkLogFileIsLarge(logPath).isLarge) { fs.ensureFileSync(logPath)
recreateLogFile(logPath) }
if (checkLogFileIsLarge(logPath).isLarge) {
recreateLogFile(logPath)
}
} catch (e) {
console.error(e)
hasUncathcedError = true
} }
return (type: string, ...msg: any[]) => { return (type: string, ...msg: any[]) => {
try { try {
@ -54,9 +68,12 @@ const getLogger = (logPath: string) => {
log += '\n' log += '\n'
console.log(log) console.log(log)
// A synchronized approach to avoid log msg sequence errors // A synchronized approach to avoid log msg sequence errors
fs.appendFileSync(logPath, log) if (!hasUncathcedError) {
fs.appendFileSync(logPath, log)
}
} catch (e) { } catch (e) {
console.error(e) console.error(e)
hasUncathcedError = true
} }
} }
} }