@rabbit-company/logger 5.3.0 → 5.4.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 CHANGED
@@ -96,13 +96,18 @@ The console transport supports extensive datetime formatting:
96
96
  - `{type}`: Log level (INFO, ERROR, etc.)
97
97
  - `{message}`: The log message
98
98
 
99
+ #### Metadata Placeholders:
100
+
101
+ - `{metadata}`: JSON-stringified metadata (if provided)
102
+ - `{metadata-ml}`: Multi-line JSON-formatted metadata (if provided)
103
+
99
104
  ```js
100
105
  import { ConsoleTransport } from "@rabbit-company/logger";
101
106
 
102
107
  // Custom format examples
103
- new ConsoleTransport("[{datetime-local}] {type} {message}");
104
- new ConsoleTransport("{time} | {type} | {message}", false);
105
- new ConsoleTransport("EPOCH:{ms} {message}");
108
+ new ConsoleTransport("[{datetime-local}] {type} {message} {metadata}");
109
+ new ConsoleTransport("{time} | {type} | {message} | {metadata}", false);
110
+ new ConsoleTransport("EPOCH:{ms} - {message} - {metadata}");
106
111
  ```
107
112
 
108
113
  ## Transports 🚚
@@ -115,7 +120,7 @@ import { ConsoleTransport } from "@rabbit-company/logger";
115
120
  const logger = new Logger({
116
121
  transports: [
117
122
  new ConsoleTransport(
118
- "[{time-local}] {type} {message}", // Custom format
123
+ "[{time-local}] {type} {message} {metadata}", // Custom format
119
124
  true // Enable colors
120
125
  ),
121
126
  ],
@@ -203,7 +208,7 @@ Full API documentation is available in the [TypeScript definitions](https://gith
203
208
 
204
209
  ```js
205
210
  new ConsoleTransport(
206
- "{type} - {date} - {message}", // Custom format
211
+ "{type} - {date} - {message} - {metadata}", // Custom format
207
212
  false // Disable colors
208
213
  );
209
214
  ```
@@ -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[];
@@ -642,7 +646,7 @@ export declare class Logger {
642
646
  * @example
643
647
  * // Custom format with local timestamps
644
648
  * const transport = new ConsoleTransport(
645
- * "[{datetime-local}] {type} - {message}",
649
+ * "[{datetime-local}] {type} - {message} {metadata}",
646
650
  * true
647
651
  * );
648
652
  */
@@ -667,7 +671,11 @@ export declare class ConsoleTransport implements Transport {
667
671
  * - `{type}`: Log level name (e.g., "INFO")
668
672
  * - `{message}`: The log message content
669
673
  *
670
- * @default "[{datetime-local}] {type} {message}"
674
+ * ### Metadata Placeholders
675
+ * - `{metadata}`: JSON-stringified metadata (if provided)
676
+ * - `{metadata-ml}`: Multi-line JSON-formatted metadata (if provided)
677
+ *
678
+ * @default "[{datetime-local}] {type} {message} {metadata}"
671
679
  *
672
680
  * @param colors Enable ANSI color output. When disabled:
673
681
  * - Improves performance in non-TTY environments
@@ -676,11 +684,11 @@ export declare class ConsoleTransport implements Transport {
676
684
  *
677
685
  * @example
678
686
  * // UTC format example
679
- * new ConsoleTransport("{date} {time} [{type}] {message}");
687
+ * new ConsoleTransport("{date} {time} [{type}] {message} {metadata}");
680
688
  *
681
689
  * @example
682
690
  * // Local time with colors disabled
683
- * new ConsoleTransport("{time-local} - {message}", false);
691
+ * new ConsoleTransport("{time-local} - {message} {metadata}", false);
684
692
  */
685
693
  constructor(format?: string, colors?: boolean);
686
694
  /**
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
- Object.keys(utcFormats).forEach((key) => {
86
+ for (const key in utcFormats) {
73
87
  utcFormats[key] = colorize(utcFormats[key]);
74
- });
75
- Object.keys(localFormats).forEach((key) => {
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabbit-company/logger",
3
- "version": "5.3.0",
3
+ "version": "5.4.0",
4
4
  "description": "A simple and lightweight logger",
5
5
  "main": "./module/logger.js",
6
6
  "type": "module",