@@ -8,6 +8,23 @@ import {
88 type FlatCacheOptions ,
99} from "flat-cache" ;
1010
11+ export type ILogger = {
12+ /** Current log level */
13+ level ?: string ;
14+ /** Trace level logging */
15+ trace : ( message : string | object , ...args : unknown [ ] ) => void ;
16+ /** Debug level logging */
17+ debug : ( message : string | object , ...args : unknown [ ] ) => void ;
18+ /** Info level logging */
19+ info : ( message : string | object , ...args : unknown [ ] ) => void ;
20+ /** Warning level logging */
21+ warn : ( message : string | object , ...args : unknown [ ] ) => void ;
22+ /** Error level logging */
23+ error : ( message : string | object , ...args : unknown [ ] ) => void ;
24+ /** Fatal level logging */
25+ fatal : ( message : string | object , ...args : unknown [ ] ) => void ;
26+ } ;
27+
1128export type FileEntryCacheOptions = {
1229 /** Whether to use file modified time for change detection (default: true) */
1330 useModifiedTime ?: boolean ;
@@ -19,6 +36,8 @@ export type FileEntryCacheOptions = {
1936 cwd ?: string ;
2037 /** Restrict file access to within cwd boundaries (default: true) */
2138 strictPaths ?: boolean ;
39+ /** Logger instance for logging (default: undefined) */
40+ logger ?: ILogger ;
2241 /** Options for the underlying flat cache */
2342 cache ?: FlatCacheOptions ;
2443} ;
@@ -127,6 +146,7 @@ export class FileEntryCache {
127146 private _hashAlgorithm = "md5" ;
128147 private _cwd : string = process . cwd ( ) ;
129148 private _strictPaths = true ;
149+ private _logger ?: ILogger ;
130150
131151 /**
132152 * Create a new FileEntryCache instance
@@ -152,6 +172,10 @@ export class FileEntryCache {
152172 if ( options ?. strictPaths !== undefined ) {
153173 this . _strictPaths = options . strictPaths ;
154174 }
175+
176+ if ( options ?. logger ) {
177+ this . _logger = options . logger ;
178+ }
155179 }
156180
157181 /**
@@ -170,6 +194,22 @@ export class FileEntryCache {
170194 this . _cache = cache ;
171195 }
172196
197+ /**
198+ * Get the logger
199+ * @returns {ILogger | undefined } The logger instance
200+ */
201+ public get logger ( ) : ILogger | undefined {
202+ return this . _logger ;
203+ }
204+
205+ /**
206+ * Set the logger
207+ * @param {ILogger | undefined } logger - The logger to set
208+ */
209+ public set logger ( logger : ILogger | undefined ) {
210+ this . _logger = logger ;
211+ }
212+
173213 /**
174214 * Use the hash to check if the file has changed
175215 * @returns {boolean } if the hash is used to check if the file has changed (default: false)
@@ -337,39 +377,62 @@ export class FileEntryCache {
337377 filePath : string ,
338378 options ?: GetFileDescriptorOptions ,
339379 ) : FileDescriptor {
380+ this . _logger ?. debug ( { filePath, options } , "Getting file descriptor" ) ;
381+
340382 let fstat : fs . Stats ;
341383 const result : FileDescriptor = {
342384 key : this . createFileKey ( filePath ) ,
343385 changed : false ,
344386 meta : { } ,
345387 } ;
346388
389+ this . _logger ?. trace ( { key : result . key } , "Created file key" ) ;
390+
347391 const metaCache = this . _cache . getKey < FileDescriptorMeta > ( result . key ) ;
348392
393+ if ( metaCache ) {
394+ this . _logger ?. trace ( { metaCache } , "Found cached meta" ) ;
395+ } else {
396+ this . _logger ?. trace ( "No cached meta found" ) ;
397+ }
398+
349399 // Start with cached meta to preserve custom properties
350400 result . meta = metaCache ? { ...metaCache } : { } ;
351401
352402 // Convert to absolute path for file system operations
353403 const absolutePath = this . getAbsolutePath ( filePath ) ;
404+ this . _logger ?. trace ( { absolutePath } , "Resolved absolute path" ) ;
354405
355406 const useCheckSumValue = options ?. useCheckSum ?? this . _useCheckSum ;
407+ this . _logger ?. debug (
408+ { useCheckSum : useCheckSumValue } ,
409+ "Using checksum setting" ,
410+ ) ;
356411
357412 try {
358413 fstat = fs . statSync ( absolutePath ) ;
359414 // Update the file stats while preserving existing meta properties
360415 result . meta . size = fstat . size ;
361416 result . meta . mtime = fstat . mtime . getTime ( ) ;
362417
418+ this . _logger ?. trace (
419+ { size : result . meta . size , mtime : result . meta . mtime } ,
420+ "Read file stats" ,
421+ ) ;
422+
363423 if ( useCheckSumValue ) {
364424 // Get the file hash
365425 const buffer = fs . readFileSync ( absolutePath ) ;
366426 result . meta . hash = this . getHash ( buffer ) ;
427+ this . _logger ?. trace ( { hash : result . meta . hash } , "Calculated file hash" ) ;
367428 }
368429 } catch ( error ) {
430+ this . _logger ?. error ( { filePath, error } , "Error reading file" ) ;
369431 this . removeEntry ( filePath ) ;
370432 let notFound = false ;
371433 if ( ( error as Error ) . message . includes ( "ENOENT" ) ) {
372434 notFound = true ;
435+ this . _logger ?. debug ( { filePath } , "File not found" ) ;
373436 }
374437
375438 return {
@@ -384,25 +447,43 @@ export class FileEntryCache {
384447 if ( ! metaCache ) {
385448 result . changed = true ;
386449 this . _cache . setKey ( result . key , result . meta ) ;
450+ this . _logger ?. debug ( { filePath } , "File not in cache, marked as changed" ) ;
387451 return result ;
388452 }
389453
390454 // If the file is in the cache, check if the file has changed
391- /* c8 ignore next 3 */
392455 if ( useCheckSumValue === false && metaCache ?. mtime !== result . meta ?. mtime ) {
393456 result . changed = true ;
457+ this . _logger ?. debug (
458+ { filePath, oldMtime : metaCache . mtime , newMtime : result . meta . mtime } ,
459+ "File changed: mtime differs" ,
460+ ) ;
394461 }
395462
396463 if ( metaCache ?. size !== result . meta ?. size ) {
397464 result . changed = true ;
465+ this . _logger ?. debug (
466+ { filePath, oldSize : metaCache . size , newSize : result . meta . size } ,
467+ "File changed: size differs" ,
468+ ) ;
398469 }
399470
400471 if ( useCheckSumValue && metaCache ?. hash !== result . meta ?. hash ) {
401472 result . changed = true ;
473+ this . _logger ?. debug (
474+ { filePath, oldHash : metaCache . hash , newHash : result . meta . hash } ,
475+ "File changed: hash differs" ,
476+ ) ;
402477 }
403478
404479 this . _cache . setKey ( result . key , result . meta ) ;
405480
481+ if ( result . changed ) {
482+ this . _logger ?. info ( { filePath } , "File has changed" ) ;
483+ } else {
484+ this . _logger ?. debug ( { filePath } , "File unchanged" ) ;
485+ }
486+
406487 return result ;
407488 }
408489
0 commit comments