-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87638a2
commit 4e3fa28
Showing
5 changed files
with
105 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import chalk from 'chalk' | ||
import dayjs from 'dayjs' | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import util from 'util' | ||
import db from '#/datastore' | ||
import { app } from 'electron' | ||
import { IChalkType } from '#/types/enum' | ||
const baseDir = app.getPath('userData') | ||
|
||
class Logger { | ||
private level = { | ||
success: IChalkType.success, | ||
info: IChalkType.info, | ||
warn: IChalkType.warn, | ||
error: IChalkType.error | ||
} | ||
protected handleLog (type: ILogType, msg: string | Error): string | Error | undefined { | ||
// if configPath is invalid then this.ctx.config === undefined | ||
// if not then check config.silent | ||
const log = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]:`) | ||
console.log(log, msg) | ||
process.nextTick(() => { | ||
this.handleWriteLog(type, msg) | ||
}) | ||
return msg | ||
} | ||
|
||
protected handleWriteLog (type: string, msg: string | Error): void { | ||
try { | ||
const logLevel = db.get('settings.logLevel') | ||
const logPath = db.get('settings.logPath') || path.join(baseDir, './picgo.log') | ||
if (this.checkLogLevel(type, logLevel)) { | ||
const picgoLog = fs.createWriteStream(logPath, { flags: 'a', encoding: 'utf8' }) | ||
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] ${msg}` | ||
const logger = new console.Console(picgoLog) | ||
if (typeof msg === 'object' && type === 'error') { | ||
log += `\n------Error Stack Begin------\n${util.format(msg.stack)}\n-------Error Stack End-------` | ||
} | ||
logger.log(log) | ||
picgoLog.destroy() | ||
} | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
protected checkLogLevel (type: string, level: undefined | string | string[]): boolean { | ||
if (level === undefined || level === 'all') { | ||
return true | ||
} | ||
if (Array.isArray(level)) { | ||
return level.some((item: string) => (item === type || item === 'all')) | ||
} else { | ||
return type === level | ||
} | ||
} | ||
|
||
success (msg: string | Error): string | Error | undefined { | ||
return this.handleLog('success', msg) | ||
} | ||
|
||
info (msg: string | Error): string | Error | undefined { | ||
return this.handleLog('info', msg) | ||
} | ||
|
||
error (msg: string | Error): string | Error | undefined { | ||
return this.handleLog('error', msg) | ||
} | ||
|
||
warn (msg: string | Error): string | Error | undefined { | ||
return this.handleLog('warn', msg) | ||
} | ||
} | ||
|
||
export default new Logger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export enum IChalkType { | ||
success = 'green', | ||
info = 'blue', | ||
warn = 'yellow', | ||
error = 'red' | ||
} | ||
|
||
export enum IPicGoHelperType { | ||
afterUploadPlugins = 'afterUploadPlugins', | ||
beforeTransformPlugins = 'beforeTransformPlugins', | ||
beforeUploadPlugins = 'beforeUploadPlugins', | ||
uploader = 'uploader', | ||
transformer = 'transformer' | ||
} | ||
|
||
export enum IPasteStyle { | ||
MARKDOWN = 'markdown', | ||
HTML = 'HTML', | ||
URL = 'URL', | ||
UBB = 'UBB', | ||
CUSTOM = 'Custom' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters