diff --git a/src/main/apis/core/utils/localLogger.ts b/src/main/apis/core/utils/localLogger.ts index 67faddd..0083edd 100644 --- a/src/main/apis/core/utils/localLogger.ts +++ b/src/main/apis/core/utils/localLogger.ts @@ -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 } } diff --git a/src/main/lifeCycle/errorHandler.ts b/src/main/lifeCycle/errorHandler.ts index 09dbd77..38a7e3b 100644 --- a/src/main/lifeCycle/errorHandler.ts +++ b/src/main/lifeCycle/errorHandler.ts @@ -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) }