@rabbit-company/logger 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -15
- package/module/logger.d.ts +504 -85
- package/module/logger.js +108 -481
- package/package.json +9 -11
package/README.md
CHANGED
|
@@ -1,31 +1,113 @@
|
|
|
1
1
|
# Logger-JS
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`Logger-JS` is a lightweight logging utility for JavaScript (ES6) that provides various logging levels and supports NDJson formatting for structured logging.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Installation](#installation)
|
|
8
|
+
2. [Importing the Library](#importing-the-library)
|
|
9
|
+
3. [Configuration](#configuration)
|
|
10
|
+
4. [Logging Messages](#logging-messages)
|
|
11
|
+
5. [NDJson Logging](#ndjson-logging)
|
|
12
|
+
6. [Customization](#customization)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
To install `Logger-JS`, use npm to add it to your project:
|
|
6
17
|
|
|
7
|
-
### 1. Download library
|
|
8
18
|
```bash
|
|
9
19
|
npm i --save @rabbit-company/logger
|
|
10
20
|
```
|
|
11
21
|
|
|
12
|
-
|
|
22
|
+
## Importing the Library
|
|
23
|
+
|
|
24
|
+
After installation, you can import the `Logger` into your JavaScript file:
|
|
25
|
+
|
|
13
26
|
```js
|
|
14
27
|
import Logger from "@rabbit-company/logger";
|
|
15
28
|
```
|
|
16
29
|
|
|
17
|
-
|
|
30
|
+
## Configuration
|
|
31
|
+
|
|
32
|
+
Configure the logger to suit your needs. You can set the log level to control which types of messages are logged:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
// Set the log level to SILLY to enable all levels of logging
|
|
36
|
+
Logger.level = Logger.Levels.SILLY;
|
|
37
|
+
|
|
38
|
+
// Enable or disable colored output
|
|
39
|
+
Logger.colors = true; // Set to false to disable colors
|
|
40
|
+
|
|
41
|
+
// Enable or disable NDJson logging
|
|
42
|
+
Logger.NDJson = false; // Set to true to enable NDJson logging
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Logging Messages
|
|
46
|
+
|
|
47
|
+
Use the provided methods to log messages at different levels of severity:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
// Log an error message
|
|
51
|
+
Logger.error("This is an error message.");
|
|
52
|
+
|
|
53
|
+
// Log a warning message
|
|
54
|
+
Logger.warn("This is a warning message.");
|
|
55
|
+
|
|
56
|
+
// Log an informational message
|
|
57
|
+
Logger.info("This is an informational message.");
|
|
58
|
+
|
|
59
|
+
// Log an HTTP-related message
|
|
60
|
+
Logger.http("This is an HTTP-related message.");
|
|
61
|
+
|
|
62
|
+
// Log a verbose message
|
|
63
|
+
Logger.verbose("This is a verbose message.");
|
|
64
|
+
|
|
65
|
+
// Log a debug message
|
|
66
|
+
Logger.debug("This is a debug message.");
|
|
67
|
+
|
|
68
|
+
// Log a silly message
|
|
69
|
+
Logger.silly("This is a silly message.");
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## NDJson Logging
|
|
73
|
+
|
|
74
|
+
When NDJson logging is enabled, log messages are formatted as newline-delimited JSON. You can retrieve the NDJson formatted log using:
|
|
75
|
+
|
|
18
76
|
```js
|
|
19
|
-
// Enable
|
|
20
|
-
Logger.
|
|
77
|
+
// Enable NDJson logging
|
|
78
|
+
Logger.NDJson = true;
|
|
79
|
+
|
|
80
|
+
// Retrieve NDJson log data
|
|
81
|
+
const ndjsonLog = Logger.getNDJson();
|
|
82
|
+
console.log(ndjsonLog);
|
|
83
|
+
|
|
84
|
+
// Clear NDJson log data
|
|
85
|
+
Logger.resetNDJson();
|
|
21
86
|
```
|
|
22
87
|
|
|
23
|
-
|
|
88
|
+
## Customization
|
|
89
|
+
|
|
90
|
+
### 1. Customizing Log Colors
|
|
91
|
+
|
|
92
|
+
You can customize the colors associated with different logging levels by modifying the `LevelColors` mapping:
|
|
93
|
+
|
|
24
94
|
```js
|
|
25
|
-
|
|
26
|
-
Logger.
|
|
27
|
-
Logger.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
95
|
+
// Change colors for different log levels
|
|
96
|
+
Logger.LevelColors[Logger.Levels.ERROR] = Logger.Colors.BRIGHT_RED;
|
|
97
|
+
Logger.LevelColors[Logger.Levels.INFO] = Logger.Colors.GREEN;
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 2. Customizing Log Message Format
|
|
101
|
+
|
|
102
|
+
You can also customize the format of the log messages by setting the `Logger.format` string. This format string can include the following placeholders:
|
|
103
|
+
|
|
104
|
+
- `{date}`: Inserts the current timestamp.
|
|
105
|
+
- `{type}`: Inserts the log level type (e.g., ERROR, INFO).
|
|
106
|
+
- `{message}`: Inserts the actual log message.
|
|
107
|
+
|
|
108
|
+
Example of customizing the log format:
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
// Set a custom log message format
|
|
112
|
+
Logger.format = "[{date}] - {type}: {message}";
|
|
113
|
+
```
|