@adobe/spacecat-shared-utils 1.19.8 → 1.20.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 CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-utils-v1.20.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.19.8...@adobe/spacecat-shared-utils-v1.20.0) (2024-09-19)
2
+
3
+
4
+ ### Features
5
+
6
+ * SITES-24835:[Importer] Tracing: logging infrastructure to support tracing an import job across all Lambdas ([#368](https://github.com/adobe/spacecat-shared/issues/368)) ([a425a4d](https://github.com/adobe/spacecat-shared/commit/a425a4def31d8511b628211441ecf29b8a15de4e))
7
+
1
8
  # [@adobe/spacecat-shared-utils-v1.19.8](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.19.7...@adobe/spacecat-shared-utils-v1.19.8) (2024-09-14)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.19.8",
3
+ "version": "1.20.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
package/src/index.js CHANGED
@@ -38,6 +38,8 @@ export {
38
38
  export { sqsWrapper } from './sqs.js';
39
39
  export { sqsEventAdapter } from './sqs.js';
40
40
 
41
+ export { logWrapper } from './log-wrapper.js';
42
+
41
43
  export {
42
44
  composeBaseURL,
43
45
  composeAuditURL,
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /**
14
+ * A higher-order function that wraps a given function and enhances logging by appending
15
+ * a `jobId` to log messages when available. This improves traceability of logs associated
16
+ * with specific jobs or processes.
17
+ *
18
+ * The wrapper checks if a `log` object exists in the `context` and whether the `message`
19
+ * contains a `jobId`. If found, log methods (e.g., `info`, `error`, etc.) will prepend the
20
+ * `jobId` to all log statements where `context.contextualLog` is used. If no `jobId` is found,
21
+ * logging will remain unchanged.
22
+ *
23
+ * @param {function} fn - The original function to be wrapped, called with the provided
24
+ * message and context after logging enhancement.
25
+ * @returns {function(object, object): Promise<Response>} - A wrapped function that enhances
26
+ * logging and returns the result of the original function.
27
+ *
28
+ * `context.contextualLog` will include logging methods with `jobId` prefixed, or fall back
29
+ * to the existing `log` object if no `jobId` is provided.
30
+ */
31
+ export function logWrapper(fn) {
32
+ return async (message, context) => {
33
+ const { log } = context;
34
+
35
+ if (log && !context.contextualLog) {
36
+ if (typeof message === 'object' && 'jobId' in message) {
37
+ const { jobId } = message;
38
+ const jobIdMarker = `[jobId=${jobId}]`;
39
+
40
+ // Define log levels
41
+ const logLevels = ['info', 'error', 'debug', 'warn', 'trace', 'verbose', 'silly', 'fatal'];
42
+
43
+ // Enhance the log object to include jobId in all log statements
44
+ context.contextualLog = logLevels.reduce((accumulator, level) => {
45
+ if (typeof log[level] === 'function') {
46
+ accumulator[level] = (...args) => log[level](jobIdMarker, ...args);
47
+ }
48
+ return accumulator;
49
+ }, {});
50
+ } else {
51
+ log.debug('No jobId found in the provided message. Log entries will be recorded without a jobId.');
52
+ context.contextualLog = log;
53
+ }
54
+ }
55
+
56
+ return fn(message, context);
57
+ };
58
+ }