@adobe/spacecat-shared-utils 1.82.3 → 1.83.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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/log-wrapper.js +62 -37
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-utils-v1.83.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.82.3...@adobe/spacecat-shared-utils-v1.83.0) (2025-12-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* Implement Structured (JSON) Logging for Spacecat Audits ([#1232](https://github.com/adobe/spacecat-shared/issues/1232)) ([7eae4d6](https://github.com/adobe/spacecat-shared/commit/7eae4d62fe9f0592f8124082fc66e9754803dd2b))
|
|
7
|
+
|
|
1
8
|
# [@adobe/spacecat-shared-utils-v1.82.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.82.2...@adobe/spacecat-shared-utils-v1.82.3) (2025-12-10)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/log-wrapper.js
CHANGED
|
@@ -13,64 +13,89 @@
|
|
|
13
13
|
import { getTraceId } from './xray.js';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
* Check if a value is a plain object (not Array, not Error, not null, not other special objects)
|
|
17
|
+
* @param {*} value - The value to check
|
|
18
|
+
* @returns {boolean} - True if the value is a plain object
|
|
19
|
+
*/
|
|
20
|
+
function isPlainObject(value) {
|
|
21
|
+
return typeof value === 'object'
|
|
22
|
+
&& value !== null
|
|
23
|
+
&& !Array.isArray(value)
|
|
24
|
+
&& !(value instanceof Error)
|
|
25
|
+
&& value.constructor === Object;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A higher-order function that wraps a given function and enhances logging by converting
|
|
30
|
+
* all logs to JSON format and appending `jobId` and `traceId` to log messages when available.
|
|
24
31
|
*
|
|
25
|
-
*
|
|
26
|
-
* message
|
|
27
|
-
*
|
|
28
|
-
* logging and returns the result of the original function.
|
|
32
|
+
* All log messages are automatically converted to structured JSON format:
|
|
33
|
+
* - String messages become: { message: "...", jobId: "...", traceId: "..." }
|
|
34
|
+
* - Object messages are merged with: { ...yourObject, jobId: "...", traceId: "..." }
|
|
29
35
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
36
|
+
* @param {function} fn - The original function to be wrapped
|
|
37
|
+
* @returns {function(object, object): Promise<Response>} - A wrapped function with JSON logging
|
|
32
38
|
*/
|
|
33
39
|
export function logWrapper(fn) {
|
|
34
40
|
return async (message, context) => {
|
|
35
41
|
const { log } = context;
|
|
36
42
|
|
|
37
43
|
if (log && !context.contextualLog) {
|
|
38
|
-
const markers =
|
|
44
|
+
const markers = {};
|
|
39
45
|
|
|
40
46
|
// Extract jobId from message if available
|
|
41
47
|
if (typeof message === 'object' && message !== null && 'jobId' in message) {
|
|
42
|
-
|
|
43
|
-
markers.push(`[jobId=${jobId}]`);
|
|
48
|
+
markers.jobId = message.jobId;
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
// Extract traceId from AWS X-Ray
|
|
47
52
|
const traceId = getTraceId();
|
|
48
53
|
if (traceId) {
|
|
49
|
-
markers.
|
|
54
|
+
markers.traceId = traceId;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
const markerString = markers.join(' ');
|
|
57
|
+
// Define log levels
|
|
58
|
+
const logLevels = ['info', 'error', 'debug', 'warn', 'trace', 'verbose', 'silly', 'fatal'];
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
// Wrap all log methods to output structured JSON
|
|
61
|
+
context.log = logLevels.reduce((accumulator, level) => {
|
|
62
|
+
if (typeof log[level] === 'function') {
|
|
63
|
+
accumulator[level] = (...args) => {
|
|
64
|
+
// If first argument is a plain object, merge with markers
|
|
65
|
+
if (args.length > 0 && isPlainObject(args[0])) {
|
|
66
|
+
return log[level]({ ...markers, ...args[0] });
|
|
67
|
+
}
|
|
58
68
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
// If first argument is a string, convert to structured format
|
|
70
|
+
if (args.length > 0 && typeof args[0] === 'string') {
|
|
71
|
+
const logObject = {
|
|
72
|
+
...markers,
|
|
73
|
+
message: args[0],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// If second argument is a plain object, merge it into the log object
|
|
77
|
+
if (args.length > 1 && isPlainObject(args[1])) {
|
|
78
|
+
Object.assign(logObject, args[1]);
|
|
79
|
+
|
|
80
|
+
// If there are more arguments after the object, add them as 'data'
|
|
81
|
+
if (args.length > 2) {
|
|
82
|
+
logObject.data = args.slice(2);
|
|
83
|
+
}
|
|
84
|
+
} else if (args.length > 1) {
|
|
85
|
+
// If there are additional arguments but second is not a plain object,
|
|
86
|
+
// add all additional args as 'data'
|
|
87
|
+
logObject.data = args.slice(1);
|
|
67
88
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
89
|
+
|
|
90
|
+
return log[level](logObject);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// For other types (arrays, primitives, Error objects), wrap in object
|
|
94
|
+
return log[level]({ ...markers, data: args });
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return accumulator;
|
|
98
|
+
}, {});
|
|
74
99
|
|
|
75
100
|
// Mark that we've processed this context
|
|
76
101
|
context.contextualLog = context.log;
|