Skip to content

Commit 5b20960

Browse files
committed
v0.0.5
1 parent da638a9 commit 5b20960

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2605
-1218
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
legacy/

README.md

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Advanced Console Log (ACL)
22

3-
[![Star on GitHub](https://img.shields.io/github/stars/samestrin/advanced-console-log?style=social)](https://github.com/samestrin/advanced-console-log/stargazers) [![Fork on GitHub](https://img.shields.io/github/forks/samestrin/advanced-console-log?style=social)](https://github.com/samestrin/advanced-console-log/network/members) [![Watch on GitHub](https://img.shields.io/github/watchers/samestrin/advanced-console-log?style=social)](https://github.com/samestrin/advanced-console-log/watchers)
4-
5-
![Version 0.0.4](https://img.shields.io/badge/Version-0.0.4-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/)
3+
[![Star on GitHub](https://img.shields.io/github/stars/samestrin/advanced-console-log?style=social)](https://github.com/samestrin/advanced-console-log/stargazers) [![Fork on GitHub](https://img.shields.io/github/forks/samestrin/advanced-console-log?style=social)](https://github.com/samestrin/advanced-console-log/network/members) [![Watch on GitHub](https://img.shields.io/github/watchers/samestrin/advanced-console-log?style=social)](https:
4+
![Version 0.0.4](https://img.shields.io/badge/Version-0.0.4-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:
65

76
**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.
87

@@ -16,6 +15,7 @@
1615
- **Multiple Log Levels**: Supports six logging levels (debug, log, info, warn, error, fatal) to categorize and prioritize log messages.
1716
- **Console Logging**: Outputs log messages to the console with color-coded and formatted output based on log level.
1817
- **File Logging**: Optionally logs messages to a specified file, with separate control over the log level for file output.
18+
- **Asynchronous Logging Modes**: Supports multiple asynchronous logging modes ("async", "async-queue", "worker") for non-blocking operations in high-throughput environments.
1919
- **Timestamps**: Includes configurable timestamps for all log messages.
2020
- **Custom Color Configuration**: Allows custom color settings for each log level to override default colors.
2121

@@ -44,6 +44,10 @@
4444

4545
- **Log Reports**: Generates a report detailing the number of times each log method was called, with percentages.
4646

47+
### File Management
48+
49+
- **File Rotation and Retention**: Supports automatic log file rotation and retention strategies to manage log file sizes and disk space.
50+
4751
## Dependencies
4852

4953
This logging module utilizes the following built-in Node.js modules:
@@ -53,6 +57,7 @@ This logging module utilizes the following built-in Node.js modules:
5357
- **`util`**: Used to format and inspect complex objects for pretty printing in log outputs.
5458
- **`process`**: Provides access to the current Node.js process, enabling memory usage tracking and process termination.
5559
- **`v8`**: Retrieves memory heap statistics to track memory usage within the application. _Lazy loaded for performance reasons._
60+
- **`worker_threads`**: Used for worker thread operations in worker mode. _Lazy loaded for performance reasons._
5661

5762
_There are no external dependencies._
5863

@@ -76,32 +81,52 @@ Then you can get a _single_ instance of ACL (recommended), created with your cus
7681

7782
```js
7883
const logger = ACL.getInstance({
79-
logLevel: 1, // Set console log level
84+
logLevel: 1,
8085
});
8186
```
8287

8388
or create a new ACL instance, using your custom [configuration options](docs/configuration-options.md).
8489

8590
```js
8691
const logger = new ACL({
87-
logLevel: 1, // Set console log level
92+
logLevel: 1,
8893
});
8994
```
9095

9196
ACL supports a number of different [configuration options](docs/configuration-options.md). Here is another example using additional configuration options:
9297

9398
```js
9499
const logger = ACL.getInstance({
95-
logLevel: 1, // Set console log level
96-
outputFilename: "app.log", // Specify log file name
97-
outputFileLogLevel: 2, // Set file log level
98-
includeTimestamps: true, // Include timestamps in logs
99-
includeMemoryUsage: true, // Track and display memory usage
100-
generateReport: true, // Enable log method call reporting
101-
terminateOnFatal: true, // Terminate on fatal log messages
100+
logLevel: 1,
101+
outputFilename: "app.log",
102+
outputFileLogLevel: 2,
103+
includeTimestamps: true,
104+
includeMemoryUsage: true,
105+
generateReport: true,
106+
terminateOnFatal: true,
107+
mode: "async",
108+
});
109+
```
110+
111+
### Asynchronous Logging Modes
112+
113+
ACL supports different modes of asynchronous logging to improve performance in high-throughput scenarios.
114+
115+
- **Regular Mode**: Default mode; logging methods are synchronous.
116+
- **Async Mode**: All logging methods are converted to their asynchronous equivalents.
117+
- **Async-Queue Mode**: Logs are queued and flushed in batches to improve performance.
118+
- **Worker Mode**: Logging operations are offloaded to a worker thread to prevent blocking the main event loop.
119+
120+
To enable an asynchronous logging mode, set the `mode` configuration option:
121+
122+
```js
123+
const logger = ACL.getInstance({
124+
mode: "async",
102125
});
103126
```
104127

128+
### Using Logging Methods
129+
105130
Once created, you can use the logger to log messages at various levels:
106131

107132
```js
@@ -112,29 +137,30 @@ logger.error("This is an error message");
112137

113138
You can also use a boolean to control log display.
114139

115-
```js
140+
```
116141
const showLog = true;
117-
logger.log(showLog, "This is an log message");
142+
logger.log(showLog, "This is a log message");
118143
```
119144

120145
If `generateReport` is set to `true`, you can generate a detailed report at the end of the application.
121146

122147
```js
123148
const logger = ACL.getInstance({
124-
generateReport: true, // Enable log method call reporting
149+
generateReport: true,
125150
});
126-
// Perform some operations
151+
127152
logger.report();
128153
```
129154

130-
ACL also supports asynchronous logging e.g. non-blocking operations. You can call configure ACL to run in async mode:
155+
### Async Logging Examples
156+
157+
ACL also supports asynchronous logging e.g., non-blocking operations. You can configure ACL to run in async mode:
131158

132159
```js
133160
const logger = ACL.getInstance({
134-
logLevel: 1, // Set console log level
135-
useAsyncLogging: true, // Configure ACL to run in async mode
161+
mode: "async",
136162
});
137-
// then all subsequent calls will automatically be asynchronous
163+
138164
logger.info("This is an async info message");
139165
logger.error("This is an async error message");
140166
```
@@ -146,14 +172,18 @@ logger.infoAsync("This is an async info message");
146172
logger.errorAsync("This is an async error message");
147173
```
148174

175+
### Using Timers
176+
149177
ACL lets you measure the elapsed time of code execution using timers:
150178

151179
```js
152180
logger.startTimer("Initialization");
153-
// Perform some operations
181+
// code to measure
154182
logger.stopTimer("Initialization");
155183
```
156184

185+
### Pretty Printing Objects
186+
157187
Use the `dir` method to pretty print complex objects:
158188

159189
```js
@@ -174,10 +204,14 @@ logger.dir(sampleObject);
174204
## Examples
175205

176206
- [Advanced Example](/examples/advanced-example.js)
207+
- [Async Queue Mode Example](/examples/async-queue.js)
208+
- [Async Mode Example](/examples/async.js)
209+
- [Worker Mode Example](/examples/worker.js)
177210
- [Custom Colors](/examples/custom-colors.js)
178211
- [Generate Report](/examples/generate-report.js)
179212
- [Log to File and Suppress Console](/examples/log-to-file-and-suppress-console.js)
180213
- [Terminate on Fatal](/examples/terminate-on-fatal.js)
214+
- [Using Timers](/examples/timers.js)
181215

182216
## Contribute
183217

@@ -189,4 +223,4 @@ This project is licensed under the MIT License - see the LICENSE file for detail
189223

190224
## Share
191225

192-
[![Twitter](https://img.shields.io/badge/X-Tweet-blue)](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20project!&url=https://github.com/samestrin/advanced-console-log) [![Facebook](https://img.shields.io/badge/Facebook-Share-blue)](https://www.facebook.com/sharer/sharer.php?u=https://github.com/samestrin/advanced-console-log) [![LinkedIn](https://img.shields.io/badge/LinkedIn-Share-blue)](https://www.linkedin.com/sharing/share-offsite/?url=https://github.com/samestrin/advanced-console-log)
226+
[![Twitter](https://img.shields.io/badge/X-Tweet-blue)](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20project!&url=https://github.com/samestrin/advanced-console-log) [![Facebook](https://img.shields.io/badge/Facebook-Share-blue)](https://www.facebook.com/sharer/sharer.php?u=https://github.com/samestrin/advanced-console-log) [![LinkedIn](https://img.shields.io/badge/LinkedIn-Share-blue)](https://www.linkedin.com/sharing/share-offsite/?url=https:

docs/configuration-options.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The `advanced-console-log` module offers a wide range of configuration options t
88

99
| **Option** | **Type** | **Default** | **Description** |
1010
| ---------------------- | --------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
11+
| `mode` | `string` | `"regular"` | Sets the logging mode. Possible values are `"regular"`, `"async"`, `"async-queue"`, and `"worker"`. |
1112
| `logLevel` | `number` | `1` | Sets the console [log level](log-levels.md). Accepts values from `0` (debug) to `5` (fatal). |
1213
| `terminateOnFatal` | `boolean` | `false` | If `true`, terminates the current process upon a `fatal` message. |
1314
| `includeTimestamps` | `boolean` | `true` | Determines whether to include timestamps in log messages. |
@@ -16,31 +17,32 @@ The `advanced-console-log` module offers a wide range of configuration options t
1617
| `memoryCheckFrequency` | `number` | `10` | Defines the frequency of memory checks. |
1718
| `memoryDisplayMode` | `number` | `1` | Defines the format for memory usage display. (1 is `MB`, 2 is `%`, and 3 is both). |
1819
| `extraSpace` | `boolean` | `false` | If `true`, adds an extra space after each logging message. |
20+
| `enableTimers` | `boolean` | `false` | If `true`, enables timer methods. (`startTimer`, `stopTimer`, `getTimer`, etc.) |
1921
| `color` | `object` | `{}` | Allows custom color configuration for log levels. See **[Example: Custom Colors](/examples/custom-colors.js)** for implementation details. |
2022

2123
### Timestamp and Caller Information Configuration
2224

23-
| **Option** | **Type** | **Default** | **Description** |
24-
| ------------------- | --------- | -------------- | ------------------------------------------------------------------------------------------------------------- |
25-
| `timestampFormat` | `string` | `HH:mm:ss.SSS` | Defines the timestamp format using date/time formatting tokens (`YYYY`, `MM`, `DD`, `HH`, `mm`, `ss`, `SSS`). |
26-
| `includeCallerInfo` | `boolean` | `false` | If `true`, includes caller information (file, function, line, and column) in log messages. |
27-
| `callerInfoLevel` | `number` | `2` | Sets the log level for caller information. Only logs of this level or higher include caller info. |
28-
| `inlineCallerInfo` | `boolean` | `false` | If `true`, displays caller information inline within log messages for easier debugging. |
29-
| `includeStackTrace` | `boolean` | `false` | If `true`, includes a stack trace in error and fatal messages. |
25+
| **Option** | **Type** | **Default** | **Description** |
26+
| ------------------------- | --------- | -------------- | --------------------------------------------------------------------------------------------------------------- |
27+
| `timestampFormat` | `string` | `HH:mm:ss.SSS` | Defines the timestamp format using date/time formatting tokens (`YYYY`, `MM`, `DD`, `HH`, `mm`, `ss`, `SSS`). |
28+
| `includeCallerInfo` | `boolean` | `false` | If `true`, includes caller information (file, function, line, and column) in log messages. |
29+
| `callerInfoLevel` | `number` | `2` | Sets the log level for caller information. Only logs of this level or higher include caller info. |
30+
| `includeInlineCallerInfo` | `boolean` | `false` | If `true`, displays caller information inline within log messages for easier debugging. |
31+
| `inlineCallerInfoLevel` | `number` | `1` | Sets the log level for inline caller information. Only logs of this level or higher include inline caller info. |
32+
| `includeStackTrace` | `boolean` | `false` | If `true`, includes a stack trace in error and fatal messages. |
3033

3134
### File Logging Configuration
3235

33-
| **Option** | **Type** | **Default** | **Description** |
34-
| --------------------------- | --------- | ----------- | -------------------------------------------------------------------------------------------------------------------------- |
35-
| `outputFilename` | `string` | `null` | Specifies the filename for file-based logging. If empty, file logging is disabled. |
36-
| `outputFileLogLevel` | `number` | `1` | Sets the [log level](log-levels.md) for file logging. Accepts values from `0` (debug) to `5` (fatal). |
37-
| `outputFileBatchOutput` | `boolean` | `false` | If `true`, enables batching of log entries for file output to improve performance. |
38-
| `outputFileBatchOutputSize` | `number` | `25` | The number of log entries to batch before writing to the file. Applies only when `outputFileBatchOutput` is set to `true`. |
39-
| `maxLogFileSizeMB` | `number` | `10` | Defines the maximum log file size in MB. When the file size is reached, a new log file is created. |
40-
| `maxLogFiles` | `number` | `5` | Limits the number of log files retained. Older files are deleted when the limit is exceeded. |
36+
| **Option** | **Type** | **Default** | **Description** |
37+
| -------------------- | -------- | ----------- | ----------------------------------------------------------------------------------------------------- |
38+
| `outputFilename` | `string` | `null` | Specifies the filename for file-based logging. If empty, file logging is disabled. |
39+
| `outputFileLogLevel` | `number` | `1` | Sets the [log level](log-levels.md) for file logging. Accepts values from `0` (debug) to `5` (fatal). |
40+
| `maxLogFileSizeMB` | `number` | `10` | Defines the maximum log file size in MB. When the file size is reached, a new log file is created. |
41+
| `maxLogFiles` | `number` | `5` | Limits the number of log files retained. Older files are deleted when the limit is exceeded. |
4142

4243
### Performance and Async Configuration
4344

44-
| **Option** | **Type** | **Default** | **Description** |
45-
| ----------------- | --------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
46-
| `useAsyncLogging` | `boolean` | `false` | If `true`, all standard log methods (`debug`, `info`, `warn`, etc.) are automatically converted to their asynchronous equivalents (`debugAsync`, `infoAsync`, etc.), enabling non-blocking logging for improved performance in high-throughput environments. |
45+
| **Option** | **Type** | **Default** | **Description** |
46+
| ---------------- | -------- | ----------- | ------------------------------------------------------------------------------------------------- |
47+
| `queueBatchSize` | `number` | `50` | Defines the number of log entries to batch before writing to the file when in `async-queue` mode. |
48+
| `flushInterval` | `number` | `1000` | The interval in milliseconds at which the log queue is flushed when in `async-queue` mode. |

docs/methods.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The following are the core logging methods available in the `advanced-console-lo
66

77
### `debug(condition = true, ...args)`
88

9-
Logs a debug message with a cyan color. It’s typically used for detailed debugging information. If `generateReport` is set to `true`, it counts the call in the report.
9+
Logs a debug message with a cyan color. It’s typically used for detailed debugging information.
1010

1111
### `log(condition = true, ...args)`
1212

@@ -30,7 +30,7 @@ Logs a fatal error message with a magenta color. It’s used for severe errors t
3030

3131
## Async Logging Methods
3232

33-
The following are the async logging methods available in the `advanced-console-log` module. These can be called directly or the instance of ACL can be configured to run in async mode and you can u8se the _Core Logging Methods_.
33+
The following are the async logging methods available in the `advanced-console-log` module. These can be called directly, or you can configure the instance of ACL to run in an async mode (e.g., `mode: "async"`) and use the Core Logging Methods.
3434

3535
### `debugAsync(condition = true, ...args)`
3636

@@ -78,6 +78,12 @@ An alias for `startTimer(label)`. Starts a timer with the given `label`.
7878

7979
An alias for `stopTimer(label)`. Stops the timer with the given `label` and logs the elapsed time.
8080

81+
### `clearAllTimers()`
82+
83+
Clears all active timers.
84+
85+
**Note:** If the `enableTimers` configuration option is not set to `true`, calling these timer methods will throw an error.
86+
8187
## Utility Methods
8288

8389
### `dir(obj)`
@@ -95,3 +101,9 @@ Logs the current stack trace of the application. It’s similar to `console.trac
95101
Generates a detailed report if `generateReport` is set to `true`. The report includes the number of calls made to each log method (`debug`, `log`, `info`, etc.) and their respective percentages.
96102

97103
**Note:** If the `generateReport` configuration option is not set to `true`, calling this method will throw an error.
104+
105+
## Close Method
106+
107+
### `close()`
108+
109+
Closes any open resources, such as file streams or worker threads. Should be called when the logger is no longer needed, especially in asynchronous modes.

0 commit comments

Comments
 (0)