-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathLogger.ts
More file actions
102 lines (89 loc) · 2.86 KB
/
Logger.ts
File metadata and controls
102 lines (89 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import chalk from 'chalk'
import dayjs from 'dayjs'
import fs from 'fs-extra'
import path from 'path'
import util from 'util'
import { ILogType } from '../utils/enum'
import {
ILogArgvType,
ILogArgvTypeWithError,
Undefinable,
ILogColor,
ILogger,
IPicGo
} from '../types'
// Convert enum into union, see: https://stackoverflow.com/a/52396706
export type LogLevel = `${ILogType}` | 'all'
export class Logger implements ILogger {
private readonly level = {
[ILogType.success]: 'green',
[ILogType.info]: 'blue',
[ILogType.warn]: 'yellow',
[ILogType.error]: 'red'
}
private readonly ctx: IPicGo
constructor (ctx: IPicGo) {
this.ctx = ctx
}
get logPath (): string {
return this.ctx.getConfig<Undefinable<string>>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log')
}
get logLevel (): LogLevel | LogLevel[] {
return this.ctx.getConfig<Undefinable<LogLevel>>('settings.logLevel') ?? 'all'
}
private handleLog (type: ILogType, ...msg: ILogArgvTypeWithError[]): void {
// check config.silent
if (!this.ctx.getConfig<Undefinable<string>>('silent')) {
const logHeader = chalk[this.level[type] as ILogColor](`[PicGo ${type.toUpperCase()}]:`)
console.log(logHeader, ...msg)
setTimeout(() => {
this.handleWriteLog(this.logPath, type, ...msg)
}, 0)
}
}
private handleWriteLog (logPath: string, type: string, ...msg: ILogArgvTypeWithError[]): void {
try {
if (this.checkLogLevel(type, this.logLevel)) {
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] `
msg.forEach((item: ILogArgvTypeWithError) => {
if (typeof item === 'object' && type === 'error') {
log += `\n------Error Stack Begin------\n${util.format(item.stack)}\n-------Error Stack End------- `
} else {
if (typeof item === 'object') {
item = JSON.stringify(item)
}
log += `${item} `
}
})
log += '\n'
// A synchronized approach to avoid log msg sequence errors
fs.appendFileSync(logPath, log)
}
} catch (e) {
console.log(e)
}
}
private checkLogLevel (type: string, level?: LogLevel | LogLevel[]): boolean {
if (level === undefined || level === 'all') {
return true
}
if (Array.isArray(level)) {
return level.some((item) => (item === type || item === 'all'))
} else {
return type === level
}
}
success (...msg: ILogArgvType[]): void {
return this.handleLog(ILogType.success, ...msg)
}
info (...msg: ILogArgvType[]): void {
return this.handleLog(ILogType.info, ...msg)
}
error (...msg: ILogArgvTypeWithError[]): void {
return this.handleLog(ILogType.error, ...msg)
}
warn (...msg: ILogArgvType[]): void {
return this.handleLog(ILogType.warn, ...msg)
}
}
export default Logger