@@ -59,6 +59,19 @@ describe('MongoLogManager', function () {
5959 expect ( log [ 0 ] . t . $date ) . to . be . a ( 'string' ) ;
6060 } ) ;
6161
62+ it ( 'can take a custom prefix for log files' , async function ( ) {
63+ const manager = new MongoLogManager ( {
64+ directory,
65+ retentionDays,
66+ prefix : 'custom_' ,
67+ onwarn,
68+ onerror,
69+ } ) ;
70+
71+ const writer = await manager . createLogWriter ( ) ;
72+ expect ( writer . logFilePath as string ) . to . match ( / c u s t o m _ / ) ;
73+ } ) ;
74+
6275 it ( 'cleans up old log files when requested' , async function ( ) {
6376 retentionDays = 0.000001 ; // 86.4 ms
6477 const manager = new MongoLogManager ( {
@@ -124,6 +137,56 @@ describe('MongoLogManager', function () {
124137 expect ( await getFilesState ( paths ) ) . to . equal ( '0000011111' ) ;
125138 } ) ;
126139
140+ it ( 'cleanup only applies to files with the prefix, if set' , async function ( ) {
141+ const manager = new MongoLogManager ( {
142+ directory,
143+ retentionDays,
144+ maxLogFileCount : 7 ,
145+ prefix : 'custom_' ,
146+ onwarn,
147+ onerror,
148+ } ) ;
149+
150+ const paths : string [ ] = [ ] ;
151+ const offset = Math . floor ( Date . now ( ) / 1000 ) ;
152+
153+ // Create 4 files: 2 with a different prefix and 2 with no prefix
154+ for ( let i = 1 ; i >= 0 ; i -- ) {
155+ const withoutPrefix = path . join (
156+ directory ,
157+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) + '_log'
158+ ) ;
159+ await fs . writeFile ( withoutPrefix , '' ) ;
160+ paths . push ( withoutPrefix ) ;
161+
162+ const withDifferentPrefix = path . join (
163+ directory ,
164+ 'different_' +
165+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) +
166+ '_log'
167+ ) ;
168+ await fs . writeFile ( withDifferentPrefix , '' ) ;
169+ paths . push ( withDifferentPrefix ) ;
170+ }
171+
172+ // Create 10 files with the prefix
173+ for ( let i = 9 ; i >= 0 ; i -- ) {
174+ const filename = path . join (
175+ directory ,
176+ `custom_${ ObjectId . createFromTime ( offset - i ) . toHexString ( ) } _log`
177+ ) ;
178+ await fs . writeFile ( filename , '' ) ;
179+ paths . push ( filename ) ;
180+ }
181+
182+ expect ( await getFilesState ( paths ) ) . to . equal ( '11111111111111' ) ;
183+ await manager . cleanupOldLogFiles ( ) ;
184+
185+ // The first 4 files without the right prefix should still be there.
186+ // The next (oldest) 3 files with the prefix should be deleted.
187+ expect ( await getFilesState ( paths ) ) . to . equal ( '11110001111111' ) ;
188+ } ) ;
189+
127190 it ( 'cleans up least recent log files when requested with a storage limit' , async function ( ) {
128191 const manager = new MongoLogManager ( {
129192 directory,
0 commit comments