MCPcopy Create free account
hub / github.com/nodejs/node / #cleanLogs

Method #cleanLogs

deps/npm/lib/utils/log-file.js:181–244  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

179 }
180
181 async #cleanLogs () {
182 // module to clean out the old log files this is a best-effort attempt.
183 // if a rm fails, we just log a message about it and move on.
184 // We do return a Promise that succeeds when we've tried to delete everything, just for the benefit of testing this function properly.
185
186 try {
187 const logPath = this.#getLogFilePath()
188 const patternFileName = basename(logPath)
189 // tell glob to only match digits
190 .replace(/\d/g, 'd')
191 // Handle the old (prior to 8.2.0) log file names which did not have a counter suffix
192 .replace('-.log', '')
193
194 let files = await fs.readdir(
195 dirname(logPath), {
196 withFileTypes: true,
197 encoding: 'utf-8',
198 })
199 files = files.sort((a, b) => basename(a.name).localeCompare(basename(b.name), 'en'))
200
201 const logFiles = []
202
203 for (const file of files) {
204 if (!file.isFile()) {
205 continue
206 }
207
208 const genericFileName = file.name.replace(/\d/g, 'd')
209 const filePath = join(dirname(logPath), basename(file.name))
210
211 // Always ignore the currently written files
212 if (
213 genericFileName.includes(patternFileName)
214 && genericFileName.endsWith('.log')
215 && !this.#files.includes(filePath)
216 ) {
217 logFiles.push(filePath)
218 }
219 }
220
221 const toDelete = logFiles.length - this.#logsMax
222
223 if (toDelete <= 0) {
224 return
225 }
226
227 log.silly('logfile', `start cleaning logs, removing ${toDelete} files`)
228
229 for (const file of logFiles.slice(0, toDelete)) {
230 try {
231 await fs.rm(file, { force: true })
232 } catch (e) {
233 log.silly('logfile', 'error removing log file', file, e)
234 }
235 }
236 } catch (e) {
237 // Disable cleanup failure warnings when log writing is disabled
238 if (this.#logsMax > 0) {

Callers 1

loadMethod · 0.95

Calls 12

#getLogFilePathMethod · 0.95
basenameFunction · 0.85
dirnameFunction · 0.85
sortMethod · 0.80
includesMethod · 0.80
verboseMethod · 0.80
sliceMethod · 0.65
joinFunction · 0.50
readdirMethod · 0.45
isFileMethod · 0.45
pushMethod · 0.45
rmMethod · 0.45

Tested by

no test coverage detected