@ricdotnet/logger 0.4.1 → 0.4.2

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.
@@ -1,4 +1,6 @@
1
1
  import type { Formatter, LogEntry } from '../types';
2
2
  export declare class PrettyFormatter implements Formatter {
3
+ private indent;
3
4
  format(entry: LogEntry): string;
5
+ private unwrapContext;
4
6
  }
@@ -1,7 +1,19 @@
1
1
  export class PrettyFormatter {
2
+ indent = 2;
2
3
  format(entry) {
3
4
  const timestamp = new Date(entry.timestamp).toLocaleString();
4
- const context = entry.context ? JSON.stringify(entry.context) : '';
5
- return `[${timestamp}] [${entry.level.toUpperCase()}] ${entry.message} ${context}`;
5
+ const context = this.unwrapContext(entry.context ?? {}).join('\n');
6
+ return `[${timestamp}] [${entry.level.toUpperCase()}] ${entry.message}\n${context}`;
7
+ }
8
+ unwrapContext(context, unwrapped = [], level = 1) {
9
+ for (const key in context) {
10
+ if (typeof context[key] === 'object') {
11
+ unwrapped.push(`${' '.repeat(level * this.indent)}${key}`);
12
+ this.unwrapContext(context[key], unwrapped, level + 1);
13
+ continue;
14
+ }
15
+ unwrapped.push(`${' '.repeat(level * this.indent)}${key}=${context[key]}`);
16
+ }
17
+ return unwrapped;
6
18
  }
7
19
  }
package/dist/logger.js CHANGED
@@ -51,7 +51,7 @@ export class Logger {
51
51
  timestamp: new Date().toISOString(),
52
52
  level,
53
53
  message,
54
- ...mergedContext,
54
+ context: mergedContext,
55
55
  };
56
56
  const output = this.formatter.format(entry);
57
57
  for (const transport of this.transports) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricdotnet/logger",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "A logger utility for my personal projects",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",