@hkdigital/lib-core 0.3.7 → 0.3.8

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.
@@ -15,32 +15,35 @@ export class PinoAdapter {
15
15
  * @param {Object} [options] - Pino configuration options
16
16
  */
17
17
  constructor(options = {}) {
18
- const defaultOptions = dev
19
- ? {
20
- level: 'debug',
21
- serializers: {
22
- err: (err) => {
23
- const chain = [];
24
- let current = err;
25
- let isFirst = true;
18
+ const baseOptions = {
19
+ serializers: {
20
+ err: (err) => {
21
+ const chain = [];
22
+ let current = err;
23
+ let isFirst = true;
26
24
 
27
- while (current) {
28
- const serialized = {
29
- name: current.name,
30
- message: current.message,
31
- ...(isFirst &&
32
- this.pino.level === 'debug' && {
33
- stack: current.stack
34
- })
35
- };
36
- chain.push(serialized);
37
- current = current.cause;
38
- isFirst = false;
39
- }
25
+ while (current) {
26
+ const serialized = {
27
+ name: current.name,
28
+ message: current.message,
29
+ ...(isFirst &&
30
+ this.pino.level === 'debug' && {
31
+ stack: current.stack
32
+ })
33
+ };
34
+ chain.push(serialized);
35
+ current = current.cause;
36
+ isFirst = false;
37
+ }
40
38
 
41
- return { errorChain: chain };
42
- }
43
- },
39
+ return { errorChain: chain };
40
+ }
41
+ }
42
+ };
43
+
44
+ const devOptions = dev
45
+ ? {
46
+ level: 'debug',
44
47
  transport: {
45
48
  target: 'pino-pretty',
46
49
  options: {
@@ -50,7 +53,7 @@ export class PinoAdapter {
50
53
  }
51
54
  : {};
52
55
 
53
- this.pino = pino({ ...defaultOptions, ...options });
56
+ this.pino = pino({ ...baseOptions, ...devOptions, ...options });
54
57
  }
55
58
 
56
59
  /**
@@ -63,10 +66,28 @@ export class PinoAdapter {
63
66
 
64
67
  const logData = {
65
68
  source,
66
- timestamp,
67
- ...(details && { details })
69
+ timestamp
68
70
  };
69
71
 
72
+ // Check if details contains an error and promote it to err property for pino serializer
73
+ if (details) {
74
+ if (details instanceof Error) {
75
+ // details is directly an error
76
+ logData.err = details;
77
+ } else if (details.error instanceof Error) {
78
+ // details has an error property
79
+ logData.err = details.error;
80
+ // Include other details except the error
81
+ const { error, ...otherDetails } = details;
82
+ if (Object.keys(otherDetails).length > 0) {
83
+ logData.details = otherDetails;
84
+ }
85
+ } else {
86
+ // No error found in details, include all details
87
+ logData.details = details;
88
+ }
89
+ }
90
+
70
91
  this.pino[level](logData, message);
71
92
  }
72
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"