@adobe/spacecat-shared-utils 1.19.8 → 1.20.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.
- package/CHANGELOG.md +14 -0
- package/package.json +1 -1
- package/src/index.js +2 -0
- package/src/log-wrapper.js +58 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-utils-v1.20.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.20.0...@adobe/spacecat-shared-utils-v1.20.1) (2024-09-19)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* add null check for empty message in logWrapper ([#377](https://github.com/adobe/spacecat-shared/issues/377)) ([231ceea](https://github.com/adobe/spacecat-shared/commit/231ceeaaad518c2358c0df728911a991cfde563c))
|
|
7
|
+
|
|
8
|
+
# [@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)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* 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))
|
|
14
|
+
|
|
1
15
|
# [@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
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -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' && message !== null && '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
|
+
}
|