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

ISSUES CLOSED: #995
This commit is contained in:
PiEgg 2022-10-23 15:47:11 +08:00
parent 9791ff24d8
commit 012a01d5d9
2 changed files with 21 additions and 9 deletions

View File

@ -22,7 +22,7 @@ const checkLogFileIsLarge = (logPath: string): {
}
} catch (e) {
// why throw error???
console.error(e)
console.log(e)
return {
isLarge: true
}
@ -30,9 +30,13 @@ const checkLogFileIsLarge = (logPath: string): {
}
const recreateLogFile = (logPath: string): void => {
if (fs.existsSync(logPath)) {
fs.unlinkSync(logPath)
fs.createFileSync(logPath)
try {
if (fs.existsSync(logPath)) {
fs.unlinkSync(logPath)
fs.createFileSync(logPath)
}
} catch (e) {
// do nothing
}
}
@ -49,10 +53,16 @@ const getLogger = (logPath: string) => {
recreateLogFile(logPath)
}
} catch (e) {
console.error(e)
console.log(e)
hasUncathcedError = true
}
return (type: string, ...msg: any[]) => {
if (hasUncathcedError) {
if (checkLogFileIsLarge(logPath).isLarge) {
recreateLogFile(logPath)
}
return
}
try {
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] `
msg.forEach((item: ILogArgvTypeWithError) => {
@ -68,11 +78,14 @@ const getLogger = (logPath: string) => {
log += '\n'
console.log(log)
// A synchronized approach to avoid log msg sequence errors
if (checkLogFileIsLarge(logPath).isLarge) {
recreateLogFile(logPath)
}
if (!hasUncathcedError) {
fs.appendFileSync(logPath, log)
}
} catch (e) {
console.error(e)
console.log(e)
hasUncathcedError = true
}
}

View File

@ -1,7 +1,7 @@
import path from 'path'
import { dbPathDir } from 'apis/core/datastore/dbChecker'
import { app } from 'electron'
import { getLogger } from 'apis/core/utils/localLogger'
const STORE_PATH = dbPathDir()
const STORE_PATH = app.getPath('userData')
const LOG_PATH = path.join(STORE_PATH, 'picgo-gui-local.log')
const logger = getLogger(LOG_PATH)
@ -10,7 +10,6 @@ const logger = getLogger(LOG_PATH)
// so we can't use the log from picgo
const handleProcessError = (error: Error) => {
console.error(error)
logger('error', error)
}