@emartech/json-logger 9.1.0 → 9.2.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 +4 -1
- package/dist/logger/logger.d.ts +2 -1
- package/dist/logger/logger.js +65 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -193,6 +193,8 @@ The separate steps of the logging process can be configured here.
|
|
|
193
193
|
These modifications affect all the instances of the library.
|
|
194
194
|
With transformers we can alter the data to be logged before passing to the formatter and then to the output.
|
|
195
195
|
It is a perfect place to add the name of the machine is running on or the request id associated with the current thread stored on a continuation local storage.
|
|
196
|
+
With the 'enhancedStackTrace' option set to true, the error stack traces will include the causes of the errors as well (if any) including their stack traces
|
|
197
|
+
with a significantly improved truncation limit.
|
|
196
198
|
|
|
197
199
|
```javascript
|
|
198
200
|
const { createLogger } = require('@emartech/json-logger');
|
|
@@ -201,7 +203,8 @@ createLogger.configure({
|
|
|
201
203
|
formatter: JSON.stringify,
|
|
202
204
|
output: console.log,
|
|
203
205
|
transformers: [],
|
|
204
|
-
outputFormat: 'ecs'
|
|
206
|
+
outputFormat: 'ecs',
|
|
207
|
+
enhancedStackTrace: true
|
|
205
208
|
});
|
|
206
209
|
|
|
207
210
|
```
|
package/dist/logger/logger.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface LoggerConfig {
|
|
|
4
4
|
output: Function;
|
|
5
5
|
transformers: Function[];
|
|
6
6
|
outputFormat: string;
|
|
7
|
+
enhancedStackTrace: boolean;
|
|
7
8
|
}
|
|
8
9
|
export declare class Logger {
|
|
9
10
|
private readonly namespace;
|
|
@@ -29,6 +30,6 @@ export declare class Logger {
|
|
|
29
30
|
private shortenData;
|
|
30
31
|
private getErrorDetails;
|
|
31
32
|
private getBaseErrorDetails;
|
|
32
|
-
private
|
|
33
|
+
private getEnhancedStackTrace;
|
|
33
34
|
private getAxiosErrorDetails;
|
|
34
35
|
}
|
package/dist/logger/logger.js
CHANGED
|
@@ -7,8 +7,9 @@ const json_1 = require("../formatter/json");
|
|
|
7
7
|
const console_1 = require("../output/console");
|
|
8
8
|
const timer_1 = require("../timer/timer");
|
|
9
9
|
const STACK_TRACE_LIMIT = 3000;
|
|
10
|
+
const ENHANCED_STACK_TRACE_LIMIT = 15000;
|
|
10
11
|
const DATA_LIMIT = 3000;
|
|
11
|
-
const allowedKeys = ['output', 'formatter', 'transformers', 'outputFormat'];
|
|
12
|
+
const allowedKeys = ['output', 'formatter', 'transformers', 'outputFormat', 'enhancedStackTrace'];
|
|
12
13
|
/* eslint-enable @typescript-eslint/no-unsafe-function-type */
|
|
13
14
|
class Logger {
|
|
14
15
|
namespace;
|
|
@@ -33,6 +34,7 @@ class Logger {
|
|
|
33
34
|
output: console_1.consoleOutput,
|
|
34
35
|
transformers: [],
|
|
35
36
|
outputFormat: 'ecs',
|
|
37
|
+
enhancedStackTrace: false,
|
|
36
38
|
};
|
|
37
39
|
isEnabled() {
|
|
38
40
|
return this.enabled;
|
|
@@ -117,30 +119,82 @@ class Logger {
|
|
|
117
119
|
return (0, lodash_1.merge)(this.getBaseErrorDetails(error), this.getAxiosErrorDetails(error));
|
|
118
120
|
}
|
|
119
121
|
getBaseErrorDetails(error) {
|
|
122
|
+
const shortenedData = this.shortenData(error.data);
|
|
120
123
|
if (Logger.config.outputFormat === 'legacy') {
|
|
121
124
|
return {
|
|
122
125
|
error_name: error.name,
|
|
123
126
|
error_stack: this.shortenStackTrace(error.stack || ''),
|
|
124
127
|
error_message: error.message,
|
|
125
|
-
error_data:
|
|
128
|
+
error_data: shortenedData,
|
|
126
129
|
};
|
|
127
130
|
}
|
|
131
|
+
const stackTrace = Logger.config.enhancedStackTrace
|
|
132
|
+
? this.getEnhancedStackTrace(error)
|
|
133
|
+
: this.shortenStackTrace(error.stack || '');
|
|
128
134
|
return {
|
|
129
|
-
error:
|
|
135
|
+
error: {
|
|
136
|
+
type: error.name,
|
|
137
|
+
message: error.message,
|
|
138
|
+
...(shortenedData && { context: shortenedData }),
|
|
139
|
+
stack_trace: stackTrace,
|
|
140
|
+
},
|
|
130
141
|
event: {
|
|
131
142
|
reason: error.message,
|
|
132
143
|
},
|
|
133
144
|
};
|
|
134
145
|
}
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
146
|
+
getEnhancedStackTrace(error) {
|
|
147
|
+
const getNumberOfCommonFrames = (ownTrace, enclosingTrace) => {
|
|
148
|
+
let m = ownTrace.length - 1;
|
|
149
|
+
let n = enclosingTrace.length - 1;
|
|
150
|
+
while (m > 0 && n > 0 && ownTrace[m] === enclosingTrace[n]) {
|
|
151
|
+
m--;
|
|
152
|
+
n--;
|
|
153
|
+
}
|
|
154
|
+
return ownTrace.length - 1 - m;
|
|
143
155
|
};
|
|
156
|
+
const getEnclosedStackTrace = (error, enclosingTrace, caption) => {
|
|
157
|
+
const output = [];
|
|
158
|
+
let errorName;
|
|
159
|
+
let errorStack;
|
|
160
|
+
const errorStackLines = error.stack ? error.stack.split('\n') : [];
|
|
161
|
+
const firstLine = errorStackLines.at(0);
|
|
162
|
+
if (error.stack) {
|
|
163
|
+
if (firstLine?.includes(error.name)) {
|
|
164
|
+
errorName = firstLine;
|
|
165
|
+
errorStack = errorStackLines.slice(1);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
errorName = error.name;
|
|
169
|
+
errorStack = errorStackLines;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
errorName = error.name;
|
|
174
|
+
errorStack = [];
|
|
175
|
+
}
|
|
176
|
+
const commonFrames = getNumberOfCommonFrames(errorStack, enclosingTrace);
|
|
177
|
+
const uniqueFrames = errorStack.length - commonFrames;
|
|
178
|
+
output.push(caption + errorName);
|
|
179
|
+
errorStack.slice(0, uniqueFrames).forEach((line) => output.push(line));
|
|
180
|
+
if (commonFrames > 0) {
|
|
181
|
+
output.push(`\t... ${commonFrames} more`);
|
|
182
|
+
}
|
|
183
|
+
if (error.cause instanceof Error) {
|
|
184
|
+
output.push(...getEnclosedStackTrace(error.cause, errorStackLines, 'Caused by: '));
|
|
185
|
+
}
|
|
186
|
+
return output;
|
|
187
|
+
};
|
|
188
|
+
const stackTrace = getEnclosedStackTrace(error, [], '');
|
|
189
|
+
const joinedStackTrace = stackTrace.join('\n');
|
|
190
|
+
let resultStackTraceStr;
|
|
191
|
+
if (joinedStackTrace.length > ENHANCED_STACK_TRACE_LIMIT) {
|
|
192
|
+
resultStackTraceStr = joinedStackTrace.substring(0, ENHANCED_STACK_TRACE_LIMIT) + ' ...';
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
resultStackTraceStr = joinedStackTrace;
|
|
196
|
+
}
|
|
197
|
+
return resultStackTraceStr;
|
|
144
198
|
}
|
|
145
199
|
getAxiosErrorDetails(error) {
|
|
146
200
|
if (!error.isAxiosError) {
|
package/package.json
CHANGED