@@ -8,6 +8,8 @@ import FileIndex from "../file";
8
8
9
9
let fileName = null ;
10
10
let fileStream = null ;
11
+ let appFileNames = { } ;
12
+ let appFileStreams = { } ;
11
13
12
14
const stringDatetime = ( ) => {
13
15
return date . format ( new Date ( ) , "YYYYMMDD" ) ;
@@ -17,7 +19,7 @@ const logsDir = () => {
17
19
} ;
18
20
19
21
const appLogsDir = ( ) => {
20
- return path . join ( AppEnv . userData , "data/ logs" ) ;
22
+ return path . join ( AppEnv . dataRoot , "logs" ) ;
21
23
} ;
22
24
23
25
const root = ( ) => {
@@ -28,6 +30,10 @@ const file = () => {
28
30
return path . join ( logsDir ( ) , "log_" + stringDatetime ( ) + ".log" ) ;
29
31
} ;
30
32
33
+ const appFile = ( name : string ) => {
34
+ return path . join ( appLogsDir ( ) , name + "_" + stringDatetime ( ) + ".log" ) ;
35
+ }
36
+
31
37
const cleanOldLogs = ( keepDays : number ) => {
32
38
const logDirs = [
33
39
// 系统日志
@@ -104,6 +110,46 @@ const error = (label: string, data: any = null) => {
104
110
return log ( "ERROR" , label , data ) ;
105
111
} ;
106
112
113
+ const appLog = ( name : string , level : "INFO" | "ERROR" , label : string , data : any = null ) => {
114
+ if ( appFileNames [ name ] !== appFile ( name ) ) {
115
+ appFileNames [ name ] = appFile ( name ) ;
116
+ if ( appFileStreams [ name ] ) {
117
+ appFileStreams [ name ] . end ( ) ;
118
+ }
119
+ const logDir = appLogsDir ( ) ;
120
+ if ( ! fs . existsSync ( logDir ) ) {
121
+ fs . mkdirSync ( logDir ) ;
122
+ }
123
+ appFileStreams [ name ] = fs . createWriteStream ( appFileNames [ name ] , { flags : "a" } ) ;
124
+ }
125
+ let line = [ ] ;
126
+ line . push ( date . format ( new Date ( ) , "YYYY-MM-DD HH:mm:ss" ) ) ;
127
+ line . push ( level ) ;
128
+ line . push ( label ) ;
129
+ if ( data ) {
130
+ if ( ! [ "number" , "string" ] . includes ( typeof data ) ) {
131
+ data = JSON . stringify ( data ) ;
132
+ }
133
+ line . push ( data ) ;
134
+ }
135
+ console . log ( `[APP:${ name } ] - ` + line . join ( " - " ) ) ;
136
+ appFileStreams [ name ] . write ( line . join ( " - " ) + "\n" ) ;
137
+ }
138
+
139
+ const appPath = ( name : string ) => {
140
+ if ( ! appFileNames [ name ] ) {
141
+ appFileNames [ name ] = appFile ( name ) ;
142
+ }
143
+ return appFileNames [ name ] ;
144
+ }
145
+
146
+ const appInfo = ( name : string , label : string , data : any = null ) => {
147
+ return appLog ( name , "INFO" , label , data ) ;
148
+ }
149
+ const appError = ( name : string , label : string , data : any = null ) => {
150
+ return appLog ( name , "ERROR" , label , data ) ;
151
+ } ;
152
+
107
153
const infoRenderOrMain = ( label : string , data : any = null ) => {
108
154
if ( electron . ipcRenderer ) {
109
155
return electron . ipcRenderer . invoke ( "log:info" , label , data ) ;
@@ -119,7 +165,23 @@ const errorRenderOrMain = (label: string, data: any = null) => {
119
165
}
120
166
} ;
121
167
122
- const collectRenderOrMain = async ( option ?: { startTime ?: string ; endTime ?: string ; limit ?: number } ) => {
168
+ const appInfoRenderOrMain = ( name : string , label : string , data : any = null ) => {
169
+ if ( electron . ipcRenderer ) {
170
+ return electron . ipcRenderer . invoke ( "log:appInfo" , name , label , data ) ;
171
+ } else {
172
+ return appInfo ( name , label , data ) ;
173
+ }
174
+ }
175
+
176
+ const appErrorRenderOrMain = ( name : string , label : string , data : any = null ) => {
177
+ if ( electron . ipcRenderer ) {
178
+ return electron . ipcRenderer . invoke ( "log:appError" , name , label , data ) ;
179
+ } else {
180
+ return appError ( name , label , data ) ;
181
+ }
182
+ } ;
183
+
184
+ const collectRenderOrMain = async ( option ?: { startTime ?: string ; endTime ?: string ; limit ?: number } ) => {
123
185
option = Object . assign (
124
186
{
125
187
startTime : dayjs ( ) . subtract ( 1 , "day" ) . format ( "YYYY-MM-DD HH:mm:ss" ) ,
@@ -194,10 +256,18 @@ export default {
194
256
error,
195
257
infoRenderOrMain,
196
258
errorRenderOrMain,
259
+ appPath,
260
+ appInfo,
261
+ appError,
262
+ appInfoRenderOrMain,
263
+ appErrorRenderOrMain,
197
264
collectRenderOrMain,
198
265
} ;
199
266
200
267
export const Log = {
201
268
info : infoRenderOrMain ,
202
269
error : errorRenderOrMain ,
270
+ appPath,
271
+ appInfo : appInfoRenderOrMain ,
272
+ appError : appErrorRenderOrMain ,
203
273
} ;
0 commit comments