lhisp-logger 3.1.4 → 3.1.6
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/package.json
CHANGED
|
@@ -4,12 +4,41 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.deepSerializeAxiosErrors = deepSerializeAxiosErrors;
|
|
5
5
|
const axiosErrorSerializer_1 = require("./axiosErrorSerializer");
|
|
6
6
|
const isAxiosError_1 = require("./isAxiosError");
|
|
7
|
-
const
|
|
7
|
+
const DEFAULT_MAX_STRING_LENGTH = 128;
|
|
8
|
+
const ENV_MAX_STRING_LENGTH = Number(process.env.LOG_MAX_STRING_LENGTH);
|
|
9
|
+
const MAX_STRING_LENGTH = Number.isFinite(ENV_MAX_STRING_LENGTH) && ENV_MAX_STRING_LENGTH > 0
|
|
10
|
+
? Math.floor(ENV_MAX_STRING_LENGTH)
|
|
11
|
+
: DEFAULT_MAX_STRING_LENGTH;
|
|
8
12
|
function isProbablyBase64(value) {
|
|
9
13
|
if (!value || value.length < 16 || value.length % 4 !== 0)
|
|
10
14
|
return false;
|
|
11
|
-
//
|
|
12
|
-
|
|
15
|
+
// Keep this check iterative to avoid regex engine recursion on pathological inputs.
|
|
16
|
+
// Padding can only appear at the end and can be at most two '=' chars.
|
|
17
|
+
let paddingStart = -1;
|
|
18
|
+
for (let i = 0; i < value.length; i++) {
|
|
19
|
+
const code = value.charCodeAt(i);
|
|
20
|
+
const isUpper = code >= 65 && code <= 90; // A-Z
|
|
21
|
+
const isLower = code >= 97 && code <= 122; // a-z
|
|
22
|
+
const isDigit = code >= 48 && code <= 57; // 0-9
|
|
23
|
+
const isPlus = code === 43; // +
|
|
24
|
+
const isSlash = code === 47; // /
|
|
25
|
+
const isPad = code === 61; // =
|
|
26
|
+
if (isPad) {
|
|
27
|
+
if (paddingStart === -1)
|
|
28
|
+
paddingStart = i;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
// Non-padding chars after padding started are invalid.
|
|
32
|
+
if (paddingStart !== -1)
|
|
33
|
+
return false;
|
|
34
|
+
if (!(isUpper || isLower || isDigit || isPlus || isSlash)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (paddingStart === -1)
|
|
39
|
+
return true;
|
|
40
|
+
const padCount = value.length - paddingStart;
|
|
41
|
+
return padCount === 1 || padCount === 2;
|
|
13
42
|
}
|
|
14
43
|
function truncateString(value, maxLength) {
|
|
15
44
|
if (value.length <= maxLength)
|
|
@@ -20,7 +49,11 @@ function truncateString(value, maxLength) {
|
|
|
20
49
|
// Handles arrays, plain objects, and protects against circular structures.
|
|
21
50
|
function deepSerializeAxiosErrors(value, seen) {
|
|
22
51
|
if (typeof value === "string") {
|
|
23
|
-
|
|
52
|
+
// Avoid expensive checks for very long payload-like strings.
|
|
53
|
+
if (value.length > MAX_STRING_LENGTH) {
|
|
54
|
+
return truncateString(value, MAX_STRING_LENGTH);
|
|
55
|
+
}
|
|
56
|
+
if (isProbablyBase64(value)) {
|
|
24
57
|
return truncateString(value, MAX_STRING_LENGTH);
|
|
25
58
|
}
|
|
26
59
|
return value;
|
package/src/lhisp-logger.js
CHANGED
|
@@ -73,6 +73,10 @@ if (lokiEnabled) {
|
|
|
73
73
|
}
|
|
74
74
|
const name = process.env.LOG_NAME || undefined;
|
|
75
75
|
const transport = pino_1.default.transport({ targets });
|
|
76
|
+
const serializeError = (error) => {
|
|
77
|
+
const base = (0, isAxiosError_1.isAxiosError)(error) ? (0, axiosErrorSerializer_1.axiosErrorSerializer)(error) : pino_1.default.stdSerializers.err(error);
|
|
78
|
+
return (0, deepSerializeAxiosErrors_1.deepSerializeAxiosErrors)(base);
|
|
79
|
+
};
|
|
76
80
|
const logger = (0, pino_1.default)({
|
|
77
81
|
name: appName,
|
|
78
82
|
level,
|
|
@@ -83,8 +87,8 @@ const logger = (0, pino_1.default)({
|
|
|
83
87
|
},
|
|
84
88
|
},
|
|
85
89
|
serializers: {
|
|
86
|
-
err:
|
|
87
|
-
error:
|
|
90
|
+
err: serializeError,
|
|
91
|
+
error: serializeError,
|
|
88
92
|
axiosError: axiosErrorSerializer_1.axiosErrorSerializer,
|
|
89
93
|
},
|
|
90
94
|
formatters: {
|