@@ -4,51 +4,67 @@ import dayjs from 'dayjs'
4
4
import fs from 'fs-extra'
5
5
import path from 'path'
6
6
import util from 'util'
7
+ import { ILogType } from '../utils/enum'
8
+ import {
9
+ ILogArgvType ,
10
+ ILogArgvTypeWithError
11
+ } from '../utils/interfaces'
7
12
8
13
class Logger {
9
- level : {
10
- [ propName : string ] : string
14
+ private level = {
15
+ [ ILogType . success ] : 'green' ,
16
+ [ ILogType . info ] : 'blue' ,
17
+ [ ILogType . warn ] : 'yellow' ,
18
+ [ ILogType . error ] : 'red'
11
19
}
12
- ctx : PicGo
20
+ private ctx : PicGo
21
+ // private logger: Console
22
+ private logLevel : string
23
+ private logPath : string
13
24
constructor ( ctx : PicGo ) {
14
- this . level = {
15
- success : 'green' ,
16
- info : 'blue' ,
17
- warn : 'yellow' ,
18
- error : 'red'
19
- }
20
25
this . ctx = ctx
21
26
}
22
- protected handleLog ( type : string , msg : string | Error ) : string | Error | undefined {
27
+ private handleLog ( type : ILogType , ... msg : ILogArgvTypeWithError [ ] ) : void {
23
28
// if configPath is invalid then this.ctx.config === undefined
24
29
// if not then check config.silent
25
30
if ( this . ctx . getConfig ( ) === undefined || ! this . ctx . getConfig ( 'silent' ) ) {
26
31
let log = chalk [ this . level [ type ] ] ( `[PicGo ${ type . toUpperCase ( ) } ]: ` )
27
- log += msg
28
- console . log ( log )
29
- process . nextTick ( ( ) => {
30
- this . handleWriteLog ( type , msg , this . ctx )
31
- } )
32
- return msg
32
+ console . log ( log , ...msg )
33
+ this . logLevel = this . ctx . getConfig ( 'settings.logLevel' )
34
+ const logPath = this . checkLogPathChange ( )
35
+ setTimeout ( ( ) => {
36
+ // The incoming logPath is a value
37
+ // lock the path with a closure
38
+ this . handleWriteLog ( logPath , type , ...msg )
39
+ } , 0 )
33
40
} else {
34
41
return
35
42
}
36
43
}
37
44
38
- protected handleWriteLog ( type : string , msg : string | Error , ctx : PicGo ) : void {
45
+ private checkLogPathChange ( ) : string {
46
+ const logPath = this . ctx . getConfig ( 'settings.logPath' ) || path . join ( this . ctx . baseDir , './picgo.log' )
47
+ if ( logPath !== this . logPath ) {
48
+ this . logPath = logPath
49
+ }
50
+ return logPath
51
+ }
52
+
53
+ protected handleWriteLog ( logPath : string , type : string , ...msg : ILogArgvTypeWithError [ ] ) : void {
39
54
try {
40
- const logLevel = this . ctx . getConfig ( 'settings.logLevel' )
41
- const logPath = this . ctx . getConfig ( 'settings.logPath' ) || path . join ( ctx . baseDir , './picgo.log' )
42
- if ( this . checkLogLevel ( type , logLevel ) ) {
43
- const picgoLog = fs . createWriteStream ( logPath , { flags : 'a' , encoding : 'utf8' } )
44
- let log = `${ dayjs ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) } [PicGo ${ type . toUpperCase ( ) } ] ${ msg } `
45
- let logger = new console . Console ( picgoLog )
46
- if ( typeof msg === 'object' && type === 'error' ) {
47
- log += `\n------Error Stack Begin------\n${ util . format ( msg . stack ) } \n-------Error Stack End-------`
48
- }
49
- logger . log ( log )
50
- picgoLog . destroy ( )
51
- logger = null
55
+ if ( this . checkLogLevel ( type , this . logLevel ) ) {
56
+ let log = `${ dayjs ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) } [PicGo ${ type . toUpperCase ( ) } ] `
57
+ msg . forEach ( ( item : ILogArgvTypeWithError ) => {
58
+ if ( typeof item === 'object' && type === 'error' ) {
59
+ log += `\n------Error Stack Begin------\n${ util . format ( item . stack ) } \n-------Error Stack End------- `
60
+ } else {
61
+ log += `${ item } `
62
+ }
63
+ } )
64
+ log += '\n'
65
+ fs . appendFile ( logPath , log , ( err : Error ) => {
66
+ if ( err ) console . log ( err )
67
+ } )
52
68
}
53
69
} catch ( e ) {
54
70
console . log ( e )
@@ -66,20 +82,20 @@ class Logger {
66
82
}
67
83
}
68
84
69
- success ( msg : string | Error ) : string | Error | undefined {
70
- return this . handleLog ( ' success' , msg )
85
+ success ( ... msg : ILogArgvType [ ] ) : void {
86
+ return this . handleLog ( ILogType . success , ... msg )
71
87
}
72
88
73
- info ( msg : string | Error ) : string | Error | undefined {
74
- return this . handleLog ( ' info' , msg )
89
+ info ( ... msg : ILogArgvType [ ] ) : void {
90
+ return this . handleLog ( ILogType . info , ... msg )
75
91
}
76
92
77
- error ( msg : string | Error ) : string | Error | undefined {
78
- return this . handleLog ( ' error' , msg )
93
+ error ( ... msg : ILogArgvTypeWithError [ ] ) : void {
94
+ return this . handleLog ( ILogType . error , ... msg )
79
95
}
80
96
81
- warn ( msg : string | Error ) : string | Error | undefined {
82
- return this . handleLog ( ' warn' , msg )
97
+ warn ( ... msg : ILogArgvType [ ] ) : void {
98
+ return this . handleLog ( ILogType . warn , ... msg )
83
99
}
84
100
}
85
101
0 commit comments