@rabbit-company/logger 5.3.0 → 5.5.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 +13 -5
- package/module/logger.d.ts +32 -9
- package/module/logger.js +33 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,9 @@ logger.error("Database connection failed", {
|
|
|
49
49
|
db: "primary",
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
+
// Dynamic log levels
|
|
53
|
+
logger.log(Levels.WARN, "High memory usage detected", { usage: "85%" });
|
|
54
|
+
|
|
52
55
|
// Audit logging
|
|
53
56
|
logger.audit("User login", {
|
|
54
57
|
userId: "usr_123",
|
|
@@ -96,13 +99,18 @@ The console transport supports extensive datetime formatting:
|
|
|
96
99
|
- `{type}`: Log level (INFO, ERROR, etc.)
|
|
97
100
|
- `{message}`: The log message
|
|
98
101
|
|
|
102
|
+
#### Metadata Placeholders:
|
|
103
|
+
|
|
104
|
+
- `{metadata}`: JSON-stringified metadata (if provided)
|
|
105
|
+
- `{metadata-ml}`: Multi-line JSON-formatted metadata (if provided)
|
|
106
|
+
|
|
99
107
|
```js
|
|
100
108
|
import { ConsoleTransport } from "@rabbit-company/logger";
|
|
101
109
|
|
|
102
110
|
// Custom format examples
|
|
103
|
-
new ConsoleTransport("[{datetime-local}] {type} {message}");
|
|
104
|
-
new ConsoleTransport("{time} | {type} | {message}", false);
|
|
105
|
-
new ConsoleTransport("EPOCH:{ms} {message}");
|
|
111
|
+
new ConsoleTransport("[{datetime-local}] {type} {message} {metadata}");
|
|
112
|
+
new ConsoleTransport("{time} | {type} | {message} | {metadata}", false);
|
|
113
|
+
new ConsoleTransport("EPOCH:{ms} - {message} - {metadata}");
|
|
106
114
|
```
|
|
107
115
|
|
|
108
116
|
## Transports 🚚
|
|
@@ -115,7 +123,7 @@ import { ConsoleTransport } from "@rabbit-company/logger";
|
|
|
115
123
|
const logger = new Logger({
|
|
116
124
|
transports: [
|
|
117
125
|
new ConsoleTransport(
|
|
118
|
-
"[{time-local}] {type} {message}", // Custom format
|
|
126
|
+
"[{time-local}] {type} {message} {metadata}", // Custom format
|
|
119
127
|
true // Enable colors
|
|
120
128
|
),
|
|
121
129
|
],
|
|
@@ -203,7 +211,7 @@ Full API documentation is available in the [TypeScript definitions](https://gith
|
|
|
203
211
|
|
|
204
212
|
```js
|
|
205
213
|
new ConsoleTransport(
|
|
206
|
-
"{type} - {date} - {message}", // Custom format
|
|
214
|
+
"{type} - {date} - {message} - {metadata}", // Custom format
|
|
207
215
|
false // Disable colors
|
|
208
216
|
);
|
|
209
217
|
```
|
package/module/logger.d.ts
CHANGED
|
@@ -186,26 +186,30 @@ export interface Transport {
|
|
|
186
186
|
* - `{type}`: Log level name (e.g., "INFO", "ERROR")
|
|
187
187
|
* - `{message}`: The actual log message content
|
|
188
188
|
*
|
|
189
|
+
* ## Metadata Placeholders
|
|
190
|
+
* - `{metadata}`: JSON-stringified metadata (if provided)
|
|
191
|
+
* - `{metadata-ml}`: Multi-line JSON-formatted metadata (if provided)
|
|
192
|
+
*
|
|
189
193
|
* @property {Transport[]} [transports=[ConsoleTransport]] - Array of transports to use
|
|
190
194
|
*
|
|
191
195
|
* @example <caption>Default Format</caption>
|
|
192
196
|
* {
|
|
193
|
-
* format: "[{datetime-local}] {type} {message}"
|
|
197
|
+
* format: "[{datetime-local}] {type} {message} {metadata}"
|
|
194
198
|
* }
|
|
195
199
|
*
|
|
196
200
|
* @example <caption>UTC Time Format</caption>
|
|
197
201
|
* {
|
|
198
|
-
* format: "[{datetime} UTC] {type}: {message}"
|
|
202
|
+
* format: "[{datetime} UTC] {type}: {message} {metadata}"
|
|
199
203
|
* }
|
|
200
204
|
*
|
|
201
205
|
* @example <caption>Detailed Local Format</caption>
|
|
202
206
|
* {
|
|
203
|
-
* format: "{date-local} {time-local} [{type}] {message}"
|
|
207
|
+
* format: "{date-local} {time-local} [{type}] {message} {metadata}"
|
|
204
208
|
* }
|
|
205
209
|
*
|
|
206
210
|
* @example <caption>Epoch Timestamp</caption>
|
|
207
211
|
* {
|
|
208
|
-
* format: "{ms} - {type} - {message}"
|
|
212
|
+
* format: "{ms} - {type} - {message} - {metadata}"
|
|
209
213
|
* }
|
|
210
214
|
*/
|
|
211
215
|
export interface LoggerConfig {
|
|
@@ -213,7 +217,7 @@ export interface LoggerConfig {
|
|
|
213
217
|
level?: Levels;
|
|
214
218
|
/** Enable colored output (default: true) */
|
|
215
219
|
colors?: boolean;
|
|
216
|
-
/** Format string using placeholders (default: "[{datetime-local}] {type} {message}") */
|
|
220
|
+
/** Format string using placeholders (default: "[{datetime-local}] {type} {message} {metadata}") */
|
|
217
221
|
format?: string;
|
|
218
222
|
/** Array of transports to use (default: [ConsoleTransport]) */
|
|
219
223
|
transports?: Transport[];
|
|
@@ -524,6 +528,21 @@ export declare class Logger {
|
|
|
524
528
|
* @param entry The log entry to process
|
|
525
529
|
*/
|
|
526
530
|
private processEntry;
|
|
531
|
+
/**
|
|
532
|
+
* Logs a message at the specified level with optional metadata.
|
|
533
|
+
*
|
|
534
|
+
* This is the primary logging method that all other level-specific methods
|
|
535
|
+
* (error, warn, info, etc.) delegate to. It provides fine-grained control
|
|
536
|
+
* over the log level and is useful for dynamic logging scenarios.
|
|
537
|
+
*
|
|
538
|
+
* @param level - The severity level for this log entry (use Levels enum)
|
|
539
|
+
* @param message - The log message to record
|
|
540
|
+
* @param metadata - Optional structured data to attach to the log entry
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* logger.log(Levels.ERROR, "Database connection failed", { error: error.stack });
|
|
544
|
+
*/
|
|
545
|
+
log(level: Levels, message: string, metadata?: Record<string, any>): void;
|
|
527
546
|
/**
|
|
528
547
|
* Logs an error message (highest severity)
|
|
529
548
|
* @param message The error message
|
|
@@ -642,7 +661,7 @@ export declare class Logger {
|
|
|
642
661
|
* @example
|
|
643
662
|
* // Custom format with local timestamps
|
|
644
663
|
* const transport = new ConsoleTransport(
|
|
645
|
-
* "[{datetime-local}] {type} - {message}",
|
|
664
|
+
* "[{datetime-local}] {type} - {message} {metadata}",
|
|
646
665
|
* true
|
|
647
666
|
* );
|
|
648
667
|
*/
|
|
@@ -667,7 +686,11 @@ export declare class ConsoleTransport implements Transport {
|
|
|
667
686
|
* - `{type}`: Log level name (e.g., "INFO")
|
|
668
687
|
* - `{message}`: The log message content
|
|
669
688
|
*
|
|
670
|
-
*
|
|
689
|
+
* ### Metadata Placeholders
|
|
690
|
+
* - `{metadata}`: JSON-stringified metadata (if provided)
|
|
691
|
+
* - `{metadata-ml}`: Multi-line JSON-formatted metadata (if provided)
|
|
692
|
+
*
|
|
693
|
+
* @default "[{datetime-local}] {type} {message} {metadata}"
|
|
671
694
|
*
|
|
672
695
|
* @param colors Enable ANSI color output. When disabled:
|
|
673
696
|
* - Improves performance in non-TTY environments
|
|
@@ -676,11 +699,11 @@ export declare class ConsoleTransport implements Transport {
|
|
|
676
699
|
*
|
|
677
700
|
* @example
|
|
678
701
|
* // UTC format example
|
|
679
|
-
* new ConsoleTransport("{date} {time} [{type}] {message}");
|
|
702
|
+
* new ConsoleTransport("{date} {time} [{type}] {message} {metadata}");
|
|
680
703
|
*
|
|
681
704
|
* @example
|
|
682
705
|
* // Local time with colors disabled
|
|
683
|
-
* new ConsoleTransport("{time-local} - {message}", false);
|
|
706
|
+
* new ConsoleTransport("{time-local} - {message} {metadata}", false);
|
|
684
707
|
*/
|
|
685
708
|
constructor(format?: string, colors?: boolean);
|
|
686
709
|
/**
|
package/module/logger.js
CHANGED
|
@@ -47,7 +47,7 @@ var LevelColors = {
|
|
|
47
47
|
[7 /* SILLY */]: "\x1B[90m" /* BRIGHT_BLACK */
|
|
48
48
|
};
|
|
49
49
|
// src/formatters/consoleFormatter.ts
|
|
50
|
-
function formatConsoleMessage(message, logLevel, format, colorsEnabled) {
|
|
50
|
+
function formatConsoleMessage(message, logLevel, metadata, format, colorsEnabled) {
|
|
51
51
|
const now = new Date;
|
|
52
52
|
const type = Levels[logLevel];
|
|
53
53
|
const utcFormats = {
|
|
@@ -64,22 +64,36 @@ function formatConsoleMessage(message, logLevel, format, colorsEnabled) {
|
|
|
64
64
|
"{date-local}": now.toLocaleDateString("sv-SE"),
|
|
65
65
|
"{full-local}": now.toString()
|
|
66
66
|
};
|
|
67
|
+
const metadataFormats = {};
|
|
68
|
+
if (metadata) {
|
|
69
|
+
metadataFormats["{metadata}"] = JSON.stringify(metadata);
|
|
70
|
+
metadataFormats["{metadata-ml}"] = JSON.stringify(metadata, null, 2);
|
|
71
|
+
if (colorsEnabled) {
|
|
72
|
+
const color = LevelColors[logLevel];
|
|
73
|
+
const colorize = (text) => color + text + "\x1B[0m" /* RESET */;
|
|
74
|
+
for (const key in metadataFormats) {
|
|
75
|
+
metadataFormats[key] = colorize(metadataFormats[key]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
format = format.replace(/{metadata(-ml)?}/g, "");
|
|
80
|
+
}
|
|
67
81
|
let coloredType = type;
|
|
68
82
|
let coloredMessage = message;
|
|
69
83
|
if (colorsEnabled) {
|
|
70
84
|
const color = LevelColors[logLevel];
|
|
71
85
|
const colorize = (text) => "\x1B[90m" /* BRIGHT_BLACK */ + text + "\x1B[0m" /* RESET */;
|
|
72
|
-
|
|
86
|
+
for (const key in utcFormats) {
|
|
73
87
|
utcFormats[key] = colorize(utcFormats[key]);
|
|
74
|
-
}
|
|
75
|
-
|
|
88
|
+
}
|
|
89
|
+
for (const key in localFormats) {
|
|
76
90
|
localFormats[key] = colorize(localFormats[key]);
|
|
77
|
-
}
|
|
91
|
+
}
|
|
78
92
|
coloredType = "\x1B[1m" /* BOLD */ + color + type + "\x1B[0m" /* RESET */;
|
|
79
93
|
coloredMessage = color + message + "\x1B[0m" /* RESET */;
|
|
80
94
|
}
|
|
81
95
|
let output = format;
|
|
82
|
-
const allFormats = { ...utcFormats, ...localFormats };
|
|
96
|
+
const allFormats = { ...utcFormats, ...localFormats, ...metadataFormats };
|
|
83
97
|
for (const [placeholder, value] of Object.entries(allFormats)) {
|
|
84
98
|
output = output.replace(new RegExp(placeholder, "g"), value);
|
|
85
99
|
}
|
|
@@ -90,12 +104,12 @@ function formatConsoleMessage(message, logLevel, format, colorsEnabled) {
|
|
|
90
104
|
class ConsoleTransport {
|
|
91
105
|
format;
|
|
92
106
|
colors;
|
|
93
|
-
constructor(format = "[{datetime-local}] {type} {message}", colors = true) {
|
|
107
|
+
constructor(format = "[{datetime-local}] {type} {message} {metadata}", colors = true) {
|
|
94
108
|
this.format = format;
|
|
95
109
|
this.colors = colors;
|
|
96
110
|
}
|
|
97
111
|
log(entry) {
|
|
98
|
-
console.info(formatConsoleMessage(entry.message, entry.level, this.format, this.colors));
|
|
112
|
+
console.info(formatConsoleMessage(entry.message, entry.level, entry.metadata, this.format, this.colors));
|
|
99
113
|
}
|
|
100
114
|
}
|
|
101
115
|
|
|
@@ -129,29 +143,32 @@ class Logger {
|
|
|
129
143
|
transport.log(entry);
|
|
130
144
|
}
|
|
131
145
|
}
|
|
146
|
+
log(level, message, metadata) {
|
|
147
|
+
this.processEntry(this.createLogEntry(message, level, metadata));
|
|
148
|
+
}
|
|
132
149
|
error(message, metadata) {
|
|
133
|
-
this.
|
|
150
|
+
this.log(0 /* ERROR */, message, metadata);
|
|
134
151
|
}
|
|
135
152
|
warn(message, metadata) {
|
|
136
|
-
this.
|
|
153
|
+
this.log(1 /* WARN */, message, metadata);
|
|
137
154
|
}
|
|
138
155
|
audit(message, metadata) {
|
|
139
|
-
this.
|
|
156
|
+
this.log(2 /* AUDIT */, message, metadata);
|
|
140
157
|
}
|
|
141
158
|
info(message, metadata) {
|
|
142
|
-
this.
|
|
159
|
+
this.log(3 /* INFO */, message, metadata);
|
|
143
160
|
}
|
|
144
161
|
http(message, metadata) {
|
|
145
|
-
this.
|
|
162
|
+
this.log(4 /* HTTP */, message, metadata);
|
|
146
163
|
}
|
|
147
164
|
debug(message, metadata) {
|
|
148
|
-
this.
|
|
165
|
+
this.log(5 /* DEBUG */, message, metadata);
|
|
149
166
|
}
|
|
150
167
|
verbose(message, metadata) {
|
|
151
|
-
this.
|
|
168
|
+
this.log(6 /* VERBOSE */, message, metadata);
|
|
152
169
|
}
|
|
153
170
|
silly(message, metadata) {
|
|
154
|
-
this.
|
|
171
|
+
this.log(7 /* SILLY */, message, metadata);
|
|
155
172
|
}
|
|
156
173
|
addTransport(transport) {
|
|
157
174
|
this.transports.push(transport);
|