@@ -129,7 +129,13 @@ class ACL {
129129 } ;
130130 this . memoryUsage = "" ;
131131 this . cwd = process . cwd ( ) ;
132+
132133 this . currentFileName = __filename . replace ( this . cwd , "" ) . replace ( / ^ \/ / , "" ) ;
134+
135+ // Call the method to write the header if outputFilename is defined
136+ if ( this . outputFilename ) {
137+ this . writeHeader ( ) ;
138+ }
133139 }
134140
135141 /**
@@ -157,6 +163,38 @@ class ACL {
157163 return ACL . instance ;
158164 }
159165
166+ /**
167+ * Writes a custom header to the log file at the start of every new run.
168+ *
169+ * The header includes the current file name (`this.currentFileName`) and a timestamp
170+ * in the format defined by `this.timestampFormat`. It also inserts a separator line
171+ * (`-----------------------------`) for better visual distinction of log sessions.
172+ *
173+ * Example Header Format:
174+ * ```
175+ * /path/to/currentFile.js (YYYY-MM-DD HH:mm:ss.SSS):
176+ * -----------------------------
177+ * ```
178+ *
179+ * This method is called automatically during initialization if `outputFilename` is defined.
180+ * If the file cannot be written, an error message is printed to `stderr`.
181+ *
182+ * @throws {Error } If writing to the log file fails, logs an error message to `stderr`.
183+ */
184+ writeHeader ( ) {
185+ const scriptName = require . main ? require . main . filename : process . argv [ 1 ] ;
186+ const header = `\n${ ACL . getCurrentTimestamp (
187+ "MM-DD-YYYY HH:mm:ss.SSS"
188+ ) } ${ scriptName } \n\n`;
189+ try {
190+ fs . appendFileSync ( this . outputFilename , header , "utf8" ) ;
191+ } catch ( err ) {
192+ process . stderr . write (
193+ `Failed to write header to log file: ${ err . message } \n`
194+ ) ;
195+ }
196+ }
197+
160198 /**
161199 * Get the current timestamp formatted according to the given pattern.
162200 * Supported patterns: YYYY, MM, DD, HH, mm, ss, SSS
@@ -234,17 +272,14 @@ class ACL {
234272 * @returns {boolean } - Whether to log to console.
235273 */
236274 shouldLogToConsole ( condition , level ) {
237- console . log ( this . logLevel , condition , level ) ;
238275 if (
239276 this . logLevel === 0 ||
240277 ( typeof condition === "boolean" && ! condition ) ||
241278 ( this . logLevel === 2 && level < 2 ) ||
242279 ( this . logLevel === 3 && level < 3 )
243280 ) {
244- console . log ( "shouldLogToConsole" , "false" ) ;
245281 return false ;
246282 }
247- console . log ( "shouldLogToConsole" , "true" ) ;
248283 return true ;
249284 }
250285
0 commit comments