@@ -8,6 +8,8 @@ const fsMiniPass = require('fs-minipass')
8
8
const log = require ( './log-shim' )
9
9
const withChownSync = require ( './with-chown-sync' )
10
10
11
+ const padZero = ( n , length ) => n . toString ( ) . padStart ( length . toString ( ) . length , '0' )
12
+
11
13
const _logHandler = Symbol ( 'logHandler' )
12
14
const _formatLogItem = Symbol ( 'formatLogItem' )
13
15
const _getLogFilePath = Symbol ( 'getLogFilePath' )
@@ -34,7 +36,7 @@ class LogFiles {
34
36
// here for infinite loops that still log. This is also partially handled
35
37
// by the config.get('max-files') option, but this is a failsafe to
36
38
// prevent runaway log file creation
37
- #MAX_LOG_FILES_PER_PROCESS = null
39
+ #MAX_FILES_PER_PROCESS = null
38
40
39
41
#fileLogCount = 0
40
42
#totalLogCount = 0
@@ -48,18 +50,14 @@ class LogFiles {
48
50
} = { } ) {
49
51
this . #logId = LogFiles . logId ( new Date ( ) )
50
52
this . #MAX_LOGS_PER_FILE = maxLogsPerFile
51
- this . #MAX_LOG_FILES_PER_PROCESS = maxFilesPerProcess
53
+ this . #MAX_FILES_PER_PROCESS = maxFilesPerProcess
52
54
this . on ( )
53
55
}
54
56
55
57
static logId ( d ) {
56
58
return d . toISOString ( ) . replace ( / [ . : ] / g, '_' )
57
59
}
58
60
59
- static fileName ( prefix , suffix ) {
60
- return `${ prefix } -debug-${ suffix } .log`
61
- }
62
-
63
61
static format ( count , level , title , ...args ) {
64
62
let prefix = `${ count } ${ level } `
65
63
if ( title ) {
@@ -149,7 +147,7 @@ class LogFiles {
149
147
if ( this . #fileLogCount >= this . #MAX_LOGS_PER_FILE) {
150
148
// Write last chunk to the file and close it
151
149
this [ _endStream ] ( logOutput )
152
- if ( this . #files. length >= this . #MAX_LOG_FILES_PER_PROCESS ) {
150
+ if ( this . #files. length >= this . #MAX_FILES_PER_PROCESS ) {
153
151
// but if its way too many then we just stop listening
154
152
this . off ( )
155
153
} else {
@@ -166,23 +164,21 @@ class LogFiles {
166
164
return LogFiles . format ( this . #totalLogCount++ , ...args )
167
165
}
168
166
169
- [ _getLogFilePath ] ( prefix , suffix ) {
170
- return path . resolve ( this . #dir, LogFiles . fileName ( prefix , suffix ) )
167
+ [ _getLogFilePath ] ( prefix , suffix , sep = '-' ) {
168
+ return path . resolve ( this . #dir, prefix + sep + 'debug' + sep + suffix + '.log' )
171
169
}
172
170
173
171
[ _openLogFile ] ( ) {
174
172
// Count in filename will be 0 indexed
175
173
const count = this . #files. length
176
174
177
- // Pad with zeros so that our log files are always sorted properly
178
- // We never want to write files ending in `-9.log` and `-10.log` because
179
- // log file cleaning is done by deleting the oldest so in this example
180
- // `-10.log` would be deleted next
181
- const countDigits = this . #MAX_LOG_FILES_PER_PROCESS. toString ( ) . length
182
-
183
175
try {
184
176
const logStream = withChownSync (
185
- this [ _getLogFilePath ] ( this . #logId, count . toString ( ) . padStart ( countDigits , '0' ) ) ,
177
+ // Pad with zeros so that our log files are always sorted properly
178
+ // We never want to write files ending in `-9.log` and `-10.log` because
179
+ // log file cleaning is done by deleting the oldest so in this example
180
+ // `-10.log` would be deleted next
181
+ this [ _getLogFilePath ] ( this . #logId, padZero ( count , this . #MAX_FILES_PER_PROCESS) ) ,
186
182
// Some effort was made to make the async, but we need to write logs
187
183
// during process.on('exit') which has to be synchronous. So in order
188
184
// to never drop log messages, it is easiest to make it sync all the time
@@ -214,14 +210,13 @@ class LogFiles {
214
210
return
215
211
}
216
212
217
- // Add 1 to account for the current log file and make
218
- // minimum config 0 so current log file is never deleted
219
- // XXX: we should make a separate documented option to
220
- // disable log file writing
221
- const max = Math . max ( this . #logsMax, 0 ) + 1
222
213
try {
223
- const files = await glob ( this [ _getLogFilePath ] ( '*' , '*' ) )
224
- const toDelete = files . length - max
214
+ // Handle the old (prior to 8.2.0) log file names which did not have an counter suffix
215
+ // so match by anything after `-debug` and before `.log` (including nothing)
216
+ const logGlob = this [ _getLogFilePath ] ( '*-' , '*' , '' )
217
+ // Always ignore the currently written files
218
+ const files = await glob ( logGlob , { ignore : this . #files } )
219
+ const toDelete = files . length - this . #logsMax
225
220
226
221
if ( toDelete <= 0 ) {
227
222
return
@@ -233,7 +228,7 @@ class LogFiles {
233
228
try {
234
229
await rimraf ( file )
235
230
} catch ( e ) {
236
- log . warn ( 'logfile' , 'error removing log file' , file , e )
231
+ log . silly ( 'logfile' , 'error removing log file' , file , e )
237
232
}
238
233
}
239
234
} catch ( e ) {
0 commit comments