@adobe/spacecat-shared-utils 1.85.1 → 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 CHANGED
@@ -1,3 +1,10 @@
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
+
1
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)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.85.1",
3
+ "version": "1.85.2",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "exports": {
@@ -13,92 +13,64 @@
13
13
  import { getTraceId } from './xray.js';
14
14
 
15
15
  /**
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 `severity`, `jobId`and `traceId`
31
- * 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.
32
24
  *
33
- * All log messages are automatically converted to structured JSON format:
34
- * - String messages become: { severity: "info", message: "...", jobId: "...", traceId: "..." }
35
- * - Object messages are merged with:
36
- * { severity: "info", ...yourObject, jobId: "...", traceId: "..." }
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.
37
29
  *
38
- * @param {function} fn - The original function to be wrapped
39
- * @returns {function(object, object): Promise<Response>} - A wrapped function with JSON logging
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.
40
32
  */
41
33
  export function logWrapper(fn) {
42
34
  return async (message, context) => {
43
35
  const { log } = context;
44
36
 
45
37
  if (log && !context.contextualLog) {
46
- const markers = {};
38
+ const markers = [];
47
39
 
48
40
  // Extract jobId from message if available
49
41
  if (typeof message === 'object' && message !== null && 'jobId' in message) {
50
- markers.jobId = message.jobId;
42
+ const { jobId } = message;
43
+ markers.push(`[jobId=${jobId}]`);
51
44
  }
52
45
 
53
46
  // Extract traceId from AWS X-Ray
54
47
  const traceId = getTraceId();
55
48
  if (traceId) {
56
- markers.traceId = traceId;
49
+ markers.push(`[traceId=${traceId}]`);
57
50
  }
58
51
 
59
- // Define log levels
60
- const logLevels = ['info', 'error', 'debug', 'warn', 'trace', 'verbose', 'silly', 'fatal'];
52
+ // If we have markers, enhance the log object directly
53
+ if (markers.length > 0) {
54
+ const markerString = markers.join(' ');
61
55
 
62
- // Wrap all log methods to output structured JSON
63
- context.log = logLevels.reduce((accumulator, level) => {
64
- if (typeof log[level] === 'function') {
65
- accumulator[level] = (...args) => {
66
- // If first argument is a plain object, merge with markers
67
- if (args.length > 0 && isPlainObject(args[0])) {
68
- return log[level](JSON.stringify({ severity: level, ...markers, ...args[0] }));
69
- }
56
+ // Define log levels
57
+ const logLevels = ['info', 'error', 'debug', 'warn', 'trace', 'verbose', 'silly', 'fatal'];
70
58
 
71
- // If first argument is a string, convert to structured format
72
- if (args.length > 0 && typeof args[0] === 'string') {
73
- const logObject = {
74
- severity: level,
75
- ...markers,
76
- message: args[0],
77
- };
78
-
79
- // If second argument is a plain object, merge it into the log object
80
- if (args.length > 1 && isPlainObject(args[1])) {
81
- Object.assign(logObject, args[1]);
82
-
83
- // If there are more arguments after the object, add them as 'data'
84
- if (args.length > 2) {
85
- logObject.data = args.slice(2);
86
- }
87
- } else if (args.length > 1) {
88
- // If there are additional arguments but second is not a plain object,
89
- // add all additional args as 'data'
90
- 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);
91
67
  }
92
-
93
- return log[level](JSON.stringify(logObject));
94
- }
95
-
96
- // For other types (arrays, primitives, Error objects), wrap in object
97
- return log[level](JSON.stringify({ severity: level, ...markers, data: args }));
98
- };
99
- }
100
- return accumulator;
101
- }, {});
68
+ return log[level](...args);
69
+ };
70
+ }
71
+ return accumulator;
72
+ }, {});
73
+ }
102
74
 
103
75
  // Mark that we've processed this context
104
76
  context.contextualLog = context.log;