@digitraffic/common 2025.4.10-1 → 2025.4.29-1
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.
@@ -55,14 +55,20 @@ describe("dt-logger", () => {
|
|
55
55
|
}
|
56
56
|
expect(loggedLine).toMatchObject(expected);
|
57
57
|
}
|
58
|
-
test("custom values", () => {
|
58
|
+
test("custom date, number, text and '=' values", () => {
|
59
59
|
const date = new Date();
|
60
60
|
assertLog({}, {
|
61
61
|
...LOG_LINE,
|
62
62
|
customDate: date,
|
63
|
+
customNumber: 123,
|
64
|
+
customText: "abc",
|
65
|
+
customEqualsText: "foo=bar",
|
63
66
|
}, {
|
64
|
-
|
67
|
+
method: EXPECTED_LOG_LINE.method,
|
68
|
+
message: `${EXPECTED_LOG_LINE.message} date=${date.toISOString()} number=123 text=abc equalsText=\"foo=bar\"`,
|
65
69
|
date: date.toISOString(),
|
70
|
+
number: 123,
|
71
|
+
text: "abc",
|
66
72
|
});
|
67
73
|
});
|
68
74
|
test("custom count should be a number", () => {
|
@@ -70,7 +76,8 @@ describe("dt-logger", () => {
|
|
70
76
|
...LOG_LINE,
|
71
77
|
customFooCount: 123,
|
72
78
|
}, {
|
73
|
-
|
79
|
+
method: EXPECTED_LOG_LINE.method,
|
80
|
+
message: `${EXPECTED_LOG_LINE.message} fooCount=123`,
|
74
81
|
fooCount: 123,
|
75
82
|
});
|
76
83
|
});
|
@@ -2,7 +2,7 @@ import { Writable } from "stream";
|
|
2
2
|
import { DtLogger } from "../../aws/runtime/dt-logger.js";
|
3
3
|
import { logException } from "../../utils/logging.js";
|
4
4
|
const TEST_METHODNAME = "test.logException";
|
5
|
-
describe("
|
5
|
+
describe("logging-test", () => {
|
6
6
|
function assertLogError(error, expected, includeStack = false) {
|
7
7
|
assertWrite((logger) => {
|
8
8
|
logException(logger, error, includeStack);
|
@@ -40,7 +40,7 @@ describe("dt-logger", () => {
|
|
40
40
|
assertLogError(STRING_ERROR, {
|
41
41
|
type: "Error",
|
42
42
|
method: TEST_METHODNAME,
|
43
|
-
message: `${TEST_METHODNAME}
|
43
|
+
message: `${TEST_METHODNAME} error=${STRING_ERROR} type=Error`,
|
44
44
|
level: "ERROR",
|
45
45
|
});
|
46
46
|
});
|
@@ -49,7 +49,7 @@ describe("dt-logger", () => {
|
|
49
49
|
assertLogError(ERROR, {
|
50
50
|
type: "Error",
|
51
51
|
method: TEST_METHODNAME,
|
52
|
-
message: `${TEST_METHODNAME}
|
52
|
+
message: `${TEST_METHODNAME} error=${ERROR.message} type=Error`,
|
53
53
|
level: "ERROR",
|
54
54
|
});
|
55
55
|
});
|
@@ -58,7 +58,7 @@ describe("dt-logger", () => {
|
|
58
58
|
assertLogError(ERROR, {
|
59
59
|
type: "Error",
|
60
60
|
method: TEST_METHODNAME,
|
61
|
-
message: `${TEST_METHODNAME}
|
61
|
+
message: `${TEST_METHODNAME} error=${ERROR.message} type=Error`,
|
62
62
|
level: "ERROR",
|
63
63
|
stack: true,
|
64
64
|
}, true);
|
@@ -69,7 +69,7 @@ describe("dt-logger", () => {
|
|
69
69
|
assertAxiosError(ERROR, {
|
70
70
|
type: "Error",
|
71
71
|
method: TEST_METHODNAME,
|
72
|
-
message: `${TEST_METHODNAME}
|
72
|
+
message: `${TEST_METHODNAME} error=${ERROR.message} type=Error code=12`,
|
73
73
|
level: "ERROR",
|
74
74
|
code: ERROR.code,
|
75
75
|
});
|
@@ -102,8 +102,10 @@ export class DtLogger {
|
|
102
102
|
: message.error
|
103
103
|
? (message.error instanceof Error) ? message.error.stack : undefined
|
104
104
|
: undefined;
|
105
|
+
const messageFields = removePrefix("custom", message);
|
106
|
+
messageFields.message = this.appendMessageFieldsToMessage(messageFields);
|
105
107
|
const logMessage = {
|
106
|
-
...
|
108
|
+
...messageFields,
|
107
109
|
error,
|
108
110
|
stack,
|
109
111
|
lambdaName: this.lambdaName,
|
@@ -111,6 +113,18 @@ export class DtLogger {
|
|
111
113
|
};
|
112
114
|
this.writeStream.write(JSON.stringify(logMessage) + "\n");
|
113
115
|
}
|
116
|
+
appendMessageFieldsToMessage(message) {
|
117
|
+
// The order is not guaranteed to be alphabetical etc.
|
118
|
+
const fielValuePairs = Object.entries(message)
|
119
|
+
// Remove fields not logged in message field
|
120
|
+
.filter(([key]) => {
|
121
|
+
return !["message", "level", "method", "stack", "error"].includes(key) && !(message.message ?? "").includes(`${key}=`);
|
122
|
+
})
|
123
|
+
// Map value to string
|
124
|
+
.map(([key, value]) => `${key}=${valueToString(value)}`)
|
125
|
+
.join(" ");
|
126
|
+
return `${message.message ?? ""}${fielValuePairs ? " " + fielValuePairs : ""}`;
|
127
|
+
}
|
114
128
|
}
|
115
129
|
/**
|
116
130
|
* Removes given prefixes from the keys of the object.
|
@@ -118,4 +132,22 @@ export class DtLogger {
|
|
118
132
|
function removePrefix(prefix, loggable) {
|
119
133
|
return mapKeys(loggable, (_index, key) => key.startsWith(prefix) ? lowerFirst(key.replace(prefix, "")) : key);
|
120
134
|
}
|
135
|
+
function valueToString(value) {
|
136
|
+
if (value === undefined) {
|
137
|
+
return "undefined";
|
138
|
+
}
|
139
|
+
else if (value === null) {
|
140
|
+
return "null";
|
141
|
+
}
|
142
|
+
else if (value instanceof Date) {
|
143
|
+
return value.toISOString();
|
144
|
+
}
|
145
|
+
else if (typeof value !== "string") {
|
146
|
+
return JSON.stringify(value);
|
147
|
+
}
|
148
|
+
else if (value.includes("=")) {
|
149
|
+
return JSON.stringify(value);
|
150
|
+
}
|
151
|
+
return value;
|
152
|
+
}
|
121
153
|
//# sourceMappingURL=dt-logger.js.map
|
package/dist/utils/logging.js
CHANGED
@@ -53,12 +53,22 @@ export function logException(logger, error, includeStack = false) {
|
|
53
53
|
// In case error is AxiosError, log the custom code property.
|
54
54
|
// eslint-disable-next-line dot-notation
|
55
55
|
const customCode = error["code"];
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
56
|
+
if (customCode) {
|
57
|
+
logger.error({
|
58
|
+
type: "Error",
|
59
|
+
method: `${functionName}.logException`,
|
60
|
+
message: `error=${message}`,
|
61
|
+
customCode,
|
62
|
+
stack,
|
63
|
+
});
|
64
|
+
}
|
65
|
+
else {
|
66
|
+
logger.error({
|
67
|
+
type: "Error",
|
68
|
+
method: `${functionName}.logException`,
|
69
|
+
message: `error=${message}`,
|
70
|
+
stack,
|
71
|
+
});
|
72
|
+
}
|
63
73
|
}
|
64
74
|
//# sourceMappingURL=logging.js.map
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@digitraffic/common",
|
3
|
-
"version": "2025.4.
|
3
|
+
"version": "2025.4.29-1",
|
4
4
|
"description": "",
|
5
5
|
"type": "module",
|
6
6
|
"repository": {
|
@@ -111,7 +111,7 @@
|
|
111
111
|
"@aws-sdk/lib-storage": "^3.777.0",
|
112
112
|
"@date-fns/tz": "1.2.0",
|
113
113
|
"@smithy/fetch-http-handler": "5.0.2",
|
114
|
-
"aws-cdk-lib": "^2.
|
114
|
+
"aws-cdk-lib": "^2.189.0",
|
115
115
|
"change-case": "^5.4.4",
|
116
116
|
"constructs": "~10.4.2",
|
117
117
|
"date-fns": "~4.1.0",
|
@@ -151,7 +151,7 @@
|
|
151
151
|
"@types/node": "22.13.14",
|
152
152
|
"@typescript-eslint/eslint-plugin": "~7.14.1",
|
153
153
|
"@typescript-eslint/parser": "^7.18.0",
|
154
|
-
"aws-cdk-lib": "^2.
|
154
|
+
"aws-cdk-lib": "^2.189.0",
|
155
155
|
"aws-sdk": "^2.1692.0",
|
156
156
|
"change-case": "^5.4.4",
|
157
157
|
"constructs": "~10.4.2",
|