Skip to content

Commit c738fc4

Browse files
committed
feat: Introduced configuration options memoryUpdateInterval
- `memoryUpdateInterval` replaces `memoryCheckFrequency` to better handle high frequency logging environments
1 parent e9528be commit c738fc4

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
![Version 1.0.0](https://img.shields.io/badge/Version-1.0.0-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Built with Node.js](https://img.shields.io/badge/Built%20with-Node.js-green)](https://nodejs.org/)
66

7-
**Advanced Console Log (ACL)**, available as the `advanced-console-log` NPM package, is a lightweight logging module for Node.js applications. It supports console and file logging with various levels, colors, and additional features such as memory usage tracking and caller information.
7+
**Advanced Console Log (ACL)**, available as the `advanced-console-log` NPM package, is a lightweight logging module for Node.js applications. It supports console and file logging with various levels, colors, and additional features such as memory usage tracking and caller information. ACL prioritizes performance by using lazy loading, ensuring efficient resource management.
88

99
![Advanced Console Log](https://samestrin.github.io/media/advanced-console-log/advanced-console-log.png)
1010

@@ -13,7 +13,7 @@
1313
### Core Logging Features
1414

1515
- **Advanced Configuration Options**: Provides extensive [configuration options](docs/configuration-options.md) such as memory display modes, caller info inclusion levels, and more.
16-
- **Multiple Log Levels**: Supports six logging levels (debug, log, info, warn, error, fatal) to categorize and prioritize log messages.
16+
- **Multiple Log Levels**: Supports six logging levels (`debug`, `log`, `info`, `warn`, `error`, `fatal`) to categorize and prioritize log messages.
1717
- **Console Logging**: Outputs log messages to the console with color-coded and formatted output based on log level.
1818
- **File Logging**: Optionally logs messages to a specified file, with separate control over the log level for file output.
1919
- **Asynchronous Logging Modes**: Supports multiple asynchronous logging modes ("async", "async-queue", "worker") for non-blocking operations in high-throughput environments.
@@ -28,9 +28,9 @@
2828

2929
### Advanced Information Tracking
3030

31-
- **Caller Information**: Includes caller information (file, function, line, and column) in log messages based on log level and configuration.
32-
- **Inline Caller Information**: Displays inline caller information within log messages for quick debugging reference.
33-
- **Memory Usage Tracking**: Tracks and displays memory usage, either in MB or percentage format, based on configuration.
31+
- **Caller Information**: Includes caller information (file, function, line, and column) in log messages based on log level and configuration. (2 modes available)
32+
- **Inline Caller Information**: Displays inline caller information within log messages for quick debugging reference. (3 modes available)
33+
- **Memory Usage Tracking**: Tracks and displays memory usage, either in MB or percentage format, based on configuration. (Uses color to provide a visual warning as usage increases)
3434

3535
### Performance Measurement
3636

docs/configuration-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The `advanced-console-log` module offers a wide range of configuration options t
1414
| `includeTimestamps` | `boolean` | `true` | Determines whether to include timestamps in log messages. |
1515
| `includeMemoryUsage` | `boolean` | `false` | If `true`, includes memory usage information in log messages. |
1616
| `generateReport` | `boolean` | `false` | If `true`, generates a summary report showing the number of times each log method was called. |
17-
| `memoryCheckFrequency` | `number` | `10` | Defines the frequency of memory checks. |
17+
| `memoryUpdateInterval` | `number` | `1000` | Defines the frequency of memory checks in ms. |
1818
| `memoryDisplayMode` | `number` | `1` | Defines the format for memory usage display. (1 is `MB`, 2 is `%`, and 3 is both). |
1919
| `extraSpace` | `boolean` | `false` | If `true`, adds an extra space after each logging message. |
2020
| `enableTimers` | `boolean` | `false` | If `true`, enables timer methods. (`startTimer`, `stopTimer`, `getTimer`, etc.) |

docs/performance-considerations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const logger = ACL.getInstance({
8181
Memory usage tracking is an optional feature that can be enabled using the `includeMemoryUsage` configuration. If this information is not needed, disable memory tracking to reduce the computational cost associated with collecting and formatting memory statistics.
8282

8383
- **`includeMemoryUsage`**: Set to `false` to disable memory usage tracking.
84-
- **`memoryCheckFrequency`**: If memory tracking is required, adjust the frequency of checks to reduce performance impact.
84+
- **`memoryUpdateInterval`**: If memory tracking is required, adjust the frequency of checks (in ms) to reduce performance impact. Use a larger value to limit the frequency of memory checks.
8585

8686
#### Example:
8787

examples/configuration-options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const logger = ACL.getInstance({
1111
outputFileLogLevel: 2, // Set file log level
1212
includeTimestamps: true, // Include timestamps in log messages
1313
includeMemoryUsage: true, // Include memory usage information
14+
memoryUpdateInterval: 1000,
1415
memoryDisplayMode: 1, // Display memory usage in MB
1516
includeCallerInfo: true, // Include caller information
1617
callerInfoLevel: 2, // Include caller info for log level 2 and above

examples/memory-usage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ACL = require("../index");
99
const logger = ACL.getInstance({
1010
includeMemoryUsage: true,
1111
memoryDisplayMode: 3, // Display both MB and percentage
12-
memoryCheckFrequency: 1,
12+
memoryUpdateInterval: 1000,
1313
});
1414

1515
function main() {

src/core/ACL.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ACL {
2929
* @param {number} [config.logLevel=1] - Console log level (0 = debug, 5 = fatal).
3030
* @param {boolean} [config.includeTimestamps=true] - Include timestamps in logs.
3131
* @param {boolean} [config.includeMemoryUsage=false] - Include memory usage info.
32-
* @param {number} [config.memoryCheckFrequency=10] - Frequency of memory checks.
32+
* @param {number} [config.memoryUpdateInterval=1000] - Frequency of memory checks in ms
3333
* @param {number} [config.memoryDisplayMode=1] - Memory display format (1 = MB, 2 = %, 3 = both).
3434
* @param {boolean} [config.includeCallerInfo=false] - Include caller info in logs.
3535
* @param {number} [config.callerInfoLevel=2] - Log level for including caller info.
@@ -55,7 +55,6 @@ class ACL {
5555
this.logLevel = typeof config.logLevel === "number" ? config.logLevel : 1;
5656
this.includeTimestamps = config.includeTimestamps !== false;
5757
this.includeMemoryUsage = config.includeMemoryUsage || false;
58-
this.memoryCheckFrequency = config.memoryCheckFrequency ?? 10;
5958
this.memoryDisplayMode = config.memoryDisplayMode ?? 1;
6059
this.includeCallerInfo = config.includeCallerInfo || false;
6160
this.callerInfoLevel = config.callerInfoLevel ?? 2;
@@ -79,6 +78,8 @@ class ACL {
7978
this.generateReport = !!config.generateReport;
8079
this.terminateOnFatal = !!config.terminateOnFatal;
8180

81+
this.memoryUpdateInterval = config.memoryUpdateInterval || 1000;
82+
8283
this.firstShown = false;
8384
this.timers = {};
8485
this.logEventCount = 0;
@@ -96,6 +97,7 @@ class ACL {
9697

9798
if (this.includeMemoryUsage) {
9899
this._totalHeapSizeLimit = getTotalHeapSizeLimit();
100+
this.startMemoryUsageUpdates();
99101
}
100102

101103
// Lazy load FileLogger when file logging is needed
@@ -315,6 +317,10 @@ class ACL {
315317
this.isClosing = true;
316318

317319
try {
320+
if (includeMemoryUsage) {
321+
this.stopMemoryUsageUpdates();
322+
}
323+
318324
// Flush and close file logs if enabled
319325
if (this.fileLogHandler) {
320326
await this.flushFileLogs();
@@ -554,15 +560,25 @@ class ACL {
554560
}
555561

556562
/**
557-
* Checks and updates the memory usage based on the configured frequency.
563+
* Starts a timer to update memory usage at a regular interval.
558564
*/
559-
updateMemoryUsage() {
560-
if (
561-
this.includeMemoryUsage &&
562-
this.logEventCount % this.memoryCheckFrequency === 0
563-
) {
565+
startMemoryUsageUpdates() {
566+
if (!this.memoryTimer && this.includeMemoryUsage) {
564567
this.memoryUsage = this.getFormattedMemoryUsage();
565-
this.logEventCount = 0;
568+
569+
this.memoryTimer = setInterval(() => {
570+
this.memoryUsage = this.getFormattedMemoryUsage();
571+
}, this.memoryUpdateInterval);
572+
}
573+
}
574+
575+
/**
576+
* Stops the memory usage updates when the logger is closed or memory tracking is disabled.
577+
*/
578+
stopMemoryUsageUpdates() {
579+
if (this.memoryTimer) {
580+
clearInterval(this.memoryTimer);
581+
this.memoryTimer = null;
566582
}
567583
}
568584

@@ -634,8 +650,6 @@ class ACL {
634650
} `
635651
: "";
636652

637-
this.updateMemoryUsage();
638-
639653
const inlineCallerInfo =
640654
this.includeInlineCallerInfo && level >= 1
641655
? `${this.color.inlineCaller}${this.getInlineCallerInfo(
@@ -706,8 +720,6 @@ class ACL {
706720
} `
707721
: "";
708722

709-
this.updateMemoryUsage();
710-
711723
const inlineCallerInfo =
712724
this.includeInlineCallerInfo && level >= 1
713725
? `${this.color.inlineCaller}${this.getInlineCallerInfo(

0 commit comments

Comments
 (0)