@adobe/spacecat-shared-utils 1.85.0 → 1.85.2
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 +14 -0
- package/package.json +1 -1
- package/src/log-wrapper.js +37 -62
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-utils-v1.85.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.85.1...@adobe/spacecat-shared-utils-v1.85.2) (2025-12-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* Implement Structured (JSON) Logging for Spacecat Audits - rollback ([#1239](https://github.com/adobe/spacecat-shared/issues/1239)) ([1f174d7](https://github.com/adobe/spacecat-shared/commit/1f174d7dd188dbdc610b75bf58644992925755b1))
|
|
7
|
+
|
|
8
|
+
# [@adobe/spacecat-shared-utils-v1.85.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.85.0...@adobe/spacecat-shared-utils-v1.85.1) (2025-12-11)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* Structured (JSON) for Logging should be JSON string ([#1237](https://github.com/adobe/spacecat-shared/issues/1237)) ([cfcee6e](https://github.com/adobe/spacecat-shared/commit/cfcee6e4315aa518c52e4ca50b99b0cb762f5a61))
|
|
14
|
+
|
|
1
15
|
# [@adobe/spacecat-shared-utils-v1.85.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.84.0...@adobe/spacecat-shared-utils-v1.85.0) (2025-12-11)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/log-wrapper.js
CHANGED
|
@@ -13,89 +13,64 @@
|
|
|
13
13
|
import { getTraceId } from './xray.js';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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.
|
|
16
|
+
* A higher-order function that wraps a given function and enhances logging by appending
|
|
17
|
+
* a `jobId` and `traceId` to log messages when available. This improves traceability of logs
|
|
18
|
+
* associated with specific jobs or processes.
|
|
19
|
+
*
|
|
20
|
+
* The wrapper checks if a `log` object exists in the `context` and whether the `message`
|
|
21
|
+
* contains a `jobId`. It also extracts the AWS X-Ray trace ID if available. If found, log
|
|
22
|
+
* methods (e.g., `info`, `error`, etc.) will prepend the `jobId` and/or `traceId` to all log
|
|
23
|
+
* statements. All existing code using `context.log` will automatically include these markers.
|
|
31
24
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
25
|
+
* @param {function} fn - The original function to be wrapped, called with the provided
|
|
26
|
+
* message and context after logging enhancement.
|
|
27
|
+
* @returns {function(object, object): Promise<Response>} - A wrapped function that enhances
|
|
28
|
+
* logging and returns the result of the original function.
|
|
35
29
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
30
|
+
* `context.log` will be enhanced in place to include `jobId` and/or `traceId` prefixed to all
|
|
31
|
+
* log messages. No code changes needed - existing `context.log` calls work automatically.
|
|
38
32
|
*/
|
|
39
33
|
export function logWrapper(fn) {
|
|
40
34
|
return async (message, context) => {
|
|
41
35
|
const { log } = context;
|
|
42
36
|
|
|
43
37
|
if (log && !context.contextualLog) {
|
|
44
|
-
const markers =
|
|
38
|
+
const markers = [];
|
|
45
39
|
|
|
46
40
|
// Extract jobId from message if available
|
|
47
41
|
if (typeof message === 'object' && message !== null && 'jobId' in message) {
|
|
48
|
-
|
|
42
|
+
const { jobId } = message;
|
|
43
|
+
markers.push(`[jobId=${jobId}]`);
|
|
49
44
|
}
|
|
50
45
|
|
|
51
46
|
// Extract traceId from AWS X-Ray
|
|
52
47
|
const traceId = getTraceId();
|
|
53
48
|
if (traceId) {
|
|
54
|
-
markers.traceId
|
|
49
|
+
markers.push(`[traceId=${traceId}]`);
|
|
55
50
|
}
|
|
56
51
|
|
|
57
|
-
//
|
|
58
|
-
|
|
52
|
+
// If we have markers, enhance the log object directly
|
|
53
|
+
if (markers.length > 0) {
|
|
54
|
+
const markerString = markers.join(' ');
|
|
59
55
|
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
}
|
|
56
|
+
// Define log levels
|
|
57
|
+
const logLevels = ['info', 'error', 'debug', 'warn', 'trace', 'verbose', 'silly', 'fatal'];
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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);
|
|
59
|
+
// Enhance context.log directly to include markers in all log statements
|
|
60
|
+
context.log = logLevels.reduce((accumulator, level) => {
|
|
61
|
+
if (typeof log[level] === 'function') {
|
|
62
|
+
accumulator[level] = (...args) => {
|
|
63
|
+
// If first argument is a string (format string), prepend the marker to it
|
|
64
|
+
if (args.length > 0 && typeof args[0] === 'string') {
|
|
65
|
+
const enhancedArgs = [`${markerString} ${args[0]}`, ...args.slice(1)];
|
|
66
|
+
return log[level](...enhancedArgs);
|
|
88
67
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
return accumulator;
|
|
98
|
-
}, {});
|
|
68
|
+
return log[level](...args);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return accumulator;
|
|
72
|
+
}, {});
|
|
73
|
+
}
|
|
99
74
|
|
|
100
75
|
// Mark that we've processed this context
|
|
101
76
|
context.contextualLog = context.log;
|