Skip to content

Commit 5af3793

Browse files
committed
feat: Log file header
1 parent 793f7fd commit 5af3793

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const ACL = require("../index");
2+
3+
// Create an ACL instance with file logging and suppressed console logs
4+
const logger = new ACL({
5+
logLevel: 0, // Suppress console logs
6+
outputFilename: "app.log", // Specify the file to save logs
7+
outputFilenameLogLevel: 1, // Log all levels to file
8+
includeTimestamps: true, // Include timestamps in the logs
9+
includeCallerInfo: false, // Optional: disable caller info if not needed
10+
includeMemoryUsage: false, // Optional: disable memory usage if not needed
11+
});
12+
13+
// Use the logger as usual
14+
logger.debug("This is a debug message");
15+
logger.log("This is a regular message");
16+
logger.info("This is an info message");
17+
logger.warn("This is a warning message");
18+
logger.error("This is an error message");
19+
logger.fatal("This is a fatal message");
20+
21+
// The above messages will be saved in `app.log` but will not be displayed in the console.

src/acl.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)