(startingIndex = maxSavedLogFiles - 1)
| 43 | }; |
| 44 | |
| 45 | function maybeRotate(startingIndex = maxSavedLogFiles - 1) { |
| 46 | let pendingFileIndex = startingIndex; |
| 47 | try { |
| 48 | const { birthtimeMs } = fs.statSync(logFile); |
| 49 | |
| 50 | if (isMoreRecentThan(birthtimeMs, interval)) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | for (; pendingFileIndex >= 0; pendingFileIndex -= 1) { |
| 55 | const currentPath = |
| 56 | pendingFileIndex === 0 ? logFile : `${logFile}.${pendingFileIndex}`; |
| 57 | const nextPath = `${logFile}.${pendingFileIndex + 1}`; |
| 58 | |
| 59 | if (fs.existsSync(nextPath)) { |
| 60 | fs.unlinkSync(nextPath); |
| 61 | } |
| 62 | if (!fs.existsSync(currentPath)) { |
| 63 | continue; |
| 64 | } |
| 65 | fs.renameSync(currentPath, nextPath); |
| 66 | } |
| 67 | } catch (error) { |
| 68 | // If we can't access the old log files - try rotating after a small |
| 69 | // delay. |
| 70 | if ( |
| 71 | retryCount < MAX_RETRY_COUNT && |
| 72 | (error.code === 'EACCES' || error.code === 'EPERM') |
| 73 | ) { |
| 74 | retryCount += 1; |
| 75 | warn(`rotatingPinoDest: retrying rotation, retryCount=${retryCount}`); |
| 76 | setTimeout(() => maybeRotate(pendingFileIndex), RETRY_DELAY); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | boom.destroy(); |
| 81 | boom.emit('error', error); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Success, reopen |
| 86 | boom.reopen(); |
| 87 | |
| 88 | if (retryCount !== 0) { |
| 89 | warn(`rotatingPinoDest: rotation succeeded after ${retryCount} retries`); |
| 90 | } |
| 91 | |
| 92 | retryCount = 0; |
| 93 | } |
| 94 | |
| 95 | maybeRotate(); |
| 96 | setInterval(maybeRotate, interval); |
no test coverage detected