@@ -61,6 +61,19 @@ describe('MongoLogManager', function () {
6161 expect ( log [ 0 ] . t . $date ) . to . be . a ( 'string' ) ;
6262 } ) ;
6363
64+ it ( 'can take a custom prefix for log files' , async function ( ) {
65+ const manager = new MongoLogManager ( {
66+ directory,
67+ retentionDays,
68+ prefix : 'custom_' ,
69+ onwarn,
70+ onerror,
71+ } ) ;
72+
73+ const writer = await manager . createLogWriter ( ) ;
74+ expect ( writer . logFilePath as string ) . to . match ( / c u s t o m _ / ) ;
75+ } ) ;
76+
6477 it ( 'cleans up old log files when requested' , async function ( ) {
6578 retentionDays = 0.000001 ; // 86.4 ms
6679 const manager = new MongoLogManager ( {
@@ -131,8 +144,8 @@ describe('MongoLogManager', function () {
131144 directory,
132145 retentionDays,
133146 retentionGB : 3 ,
134- onwarn,
135147 onerror,
148+ onwarn,
136149 } ) ;
137150
138151 const offset = Math . floor ( Date . now ( ) / 1000 ) ;
@@ -178,7 +191,57 @@ describe('MongoLogManager', function () {
178191 expect ( leftoverFiles ) . deep . equals ( [ faultyFile , ...validFiles . slice ( 3 ) ] ) ;
179192 } ) ;
180193
181- it ( 'cleans up least recent log files when over a storage limit' , async function ( ) {
194+ it ( 'cleanup only applies to files with the prefix, if set' , async function ( ) {
195+ const manager = new MongoLogManager ( {
196+ directory,
197+ retentionDays,
198+ maxLogFileCount : 7 ,
199+ prefix : 'custom_' ,
200+ onwarn,
201+ onerror,
202+ } ) ;
203+
204+ const paths : string [ ] = [ ] ;
205+ const offset = Math . floor ( Date . now ( ) / 1000 ) ;
206+
207+ // Create 4 files: 2 with a different prefix and 2 with no prefix
208+ for ( let i = 1 ; i >= 0 ; i -- ) {
209+ const withoutPrefix = path . join (
210+ directory ,
211+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) + '_log'
212+ ) ;
213+ await fs . writeFile ( withoutPrefix , '' ) ;
214+ paths . push ( withoutPrefix ) ;
215+
216+ const withDifferentPrefix = path . join (
217+ directory ,
218+ 'different_' +
219+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) +
220+ '_log'
221+ ) ;
222+ await fs . writeFile ( withDifferentPrefix , '' ) ;
223+ paths . push ( withDifferentPrefix ) ;
224+ }
225+
226+ // Create 10 files with the prefix
227+ for ( let i = 9 ; i >= 0 ; i -- ) {
228+ const filename = path . join (
229+ directory ,
230+ `custom_${ ObjectId . createFromTime ( offset - i ) . toHexString ( ) } _log`
231+ ) ;
232+ await fs . writeFile ( filename , '' ) ;
233+ paths . push ( filename ) ;
234+ }
235+
236+ expect ( await getFilesState ( paths ) ) . to . equal ( '11111111111111' ) ;
237+ await manager . cleanupOldLogFiles ( ) ;
238+
239+ // The first 4 files without the right prefix should still be there.
240+ // The next (oldest) 3 files with the prefix should be deleted.
241+ expect ( await getFilesState ( paths ) ) . to . equal ( '11110001111111' ) ;
242+ } ) ;
243+
244+ it ( 'cleans up least recent log files when requested with a storage limit' , async function ( ) {
182245 const manager = new MongoLogManager ( {
183246 directory,
184247 retentionDays,
0 commit comments