Skip to content

Commit 158be01

Browse files
committed
✨ Feature: add log file size limit
1 parent 93c050d commit 158be01

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

src/lib/Logger.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ILogger,
1313
IPicGo
1414
} from '../types'
15+
import { forceNumber } from '../utils/common'
1516

1617
export class Logger implements ILogger {
1718
private readonly level = {
@@ -36,11 +37,50 @@ export class Logger implements ILogger {
3637
this.logLevel = this.ctx.getConfig('settings.logLevel')
3738
this.logPath = this.ctx.getConfig<Undefinable<string>>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log')
3839
setTimeout(() => {
40+
// fix log file is too large, now the log file's default size is 10 MB
41+
try {
42+
const result = this.checkLogFileIsLarge(this.logPath)
43+
if (result.isLarge) {
44+
const warningMsg = `Log file is too large (> ${(result.logFileSizeLimit!) / 1024 / 1024 || '10'} MB), recreate log file`
45+
console.log(chalk.yellow('[PicGo WARN]:'), warningMsg)
46+
this.recreateLogFile(this.logPath)
47+
msg.unshift(warningMsg)
48+
}
49+
} catch (e) {
50+
// why???
51+
console.error('[PicGo Error] on checking log file size', e)
52+
}
3953
this.handleWriteLog(this.logPath, type, ...msg)
4054
}, 0)
4155
}
4256
}
4357

58+
private checkLogFileIsLarge (logPath: string): {
59+
isLarge: boolean
60+
logFileSize?: number
61+
logFileSizeLimit?: number
62+
} {
63+
if (fs.existsSync(logPath)) {
64+
const logFileSize = fs.statSync(logPath).size
65+
const logFileSizeLimit = forceNumber(this.ctx.getConfig<Undefinable<number>>('settings.logFileSizeLimit') || 10) * 1024 * 1024 // 10 MB default
66+
return {
67+
isLarge: logFileSize > logFileSizeLimit,
68+
logFileSize,
69+
logFileSizeLimit
70+
}
71+
}
72+
return {
73+
isLarge: false
74+
}
75+
}
76+
77+
private recreateLogFile (logPath: string): void {
78+
if (fs.existsSync(logPath)) {
79+
fs.unlinkSync(logPath)
80+
fs.createFileSync(logPath)
81+
}
82+
}
83+
4484
private handleWriteLog (logPath: string, type: string, ...msg: ILogArgvTypeWithError[]): void {
4585
try {
4686
if (this.checkLogLevel(type, this.logLevel)) {
@@ -60,7 +100,7 @@ export class Logger implements ILogger {
60100
fs.appendFileSync(logPath, log)
61101
}
62102
} catch (e) {
63-
console.log(e)
103+
console.error('[PicGo Error] on writing log file', e)
64104
}
65105
}
66106

src/plugins/commander/setting.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const setting = {
4242
type: 'list',
4343
name: `${module}`,
4444
choices: ctx.helper[module].getIdList(),
45-
message: `Choose a(n) ${module}`,
46-
default: ctx.getConfig('picBed.uploader') || ctx.getConfig('picBed.current')
45+
message: `Choose a(n) ${module}`
46+
// default: ctx.getConfig('picBed.uploader') || ctx.getConfig('picBed.current')
4747
}
4848
]
4949
const answer = await ctx.cmd.inquirer.prompt<IStringKeyMap<any>>(prompts)
@@ -84,7 +84,9 @@ const setting = {
8484
ctx.log.warn(`No module named ${module}`)
8585
return ctx.log.warn('Available modules are uploader|transformer|plugin')
8686
}
87+
const useModuleName = module === 'plugin' ? 'plugins' : module
8788
ctx.log.success('Configure config successfully!')
89+
ctx.log.info(`If you want to use this config, please run 'picgo use ${useModuleName}'`)
8890
} catch (e: any) {
8991
ctx.log.error(e)
9092
if (process.argv.includes('--debug')) {

src/utils/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,7 @@ export function safeParse<T> (str: string): T | string {
363363
// export const isConfigKeyInWhiteList = (key: string): boolean => {
364364
// return configWhiteList.some(whiteItem => whiteItem.test(key))
365365
// }
366+
367+
export const forceNumber = (num: string | number = 0): number => {
368+
return isNaN(Number(num)) ? 0 : Number(num)
369+
}

src/utils/getClipboardImage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import windowsClipboardScript from './clipboard/windows.ps1'
1111
import windows10ClipboardScript from './clipboard/windows10.ps1'
1212
import linuxClipboardScript from './clipboard/linux.sh'
1313
import wslClipboardScript from './clipboard/wsl.sh'
14+
import { CLIPBOARD_IMAGE_FOLDER } from './static'
1415

1516
export type Platform = 'darwin' | 'win32' | 'win10' | 'linux' | 'wsl'
1617

@@ -56,9 +57,18 @@ const platform2ScriptFilename: {
5657
wsl: 'wsl.sh'
5758
}
5859

60+
function createImageFolder (ctx: IPicGo): void {
61+
const imagePath = path.join(ctx.baseDir, CLIPBOARD_IMAGE_FOLDER)
62+
if (!fs.existsSync(imagePath)) {
63+
fs.mkdirSync(imagePath)
64+
}
65+
}
66+
5967
// Thanks to vs-picgo: https://github.com/Spades-S/vs-picgo/blob/master/src/extension.ts
6068
const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
61-
const imagePath = path.join(ctx.baseDir, `${dayjs().format('YYYYMMDDHHmmss')}.png`)
69+
createImageFolder(ctx)
70+
// add an clipboard image folder to control the image cache file
71+
const imagePath = path.join(ctx.baseDir, CLIPBOARD_IMAGE_FOLDER, `${dayjs().format('YYYYMMDDHHmmss')}.png`)
6272
return await new Promise<IClipboardImage>((resolve: Function, reject: Function): void => {
6373
const platform = getCurrentPlatform()
6474
const scriptPath = path.join(ctx.baseDir, platform2ScriptFilename[platform])

src/utils/static.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CLIPBOARD_IMAGE_FOLDER = 'picgo-clipboard-images'

0 commit comments

Comments
 (0)