Skip to content

Commit d55ecc4

Browse files
fix(utils): correct logWrapper format string handling
The logWrapper was prepending trace/job IDs as a separate argument instead of concatenating them with the format string. This caused format placeholders (%s, %d, etc.) to not be interpolated, appearing literally in logs. Changes: - Merge marker string with format string (first arg) instead of prepending as separate arg - Add tests to verify format string placeholder interpolation works correctly Before: log.info('[traceId=123]', 'Found %d items', 42) After: log.info('[traceId=123] Found %d items', 42) Fixes format string interpolation in all log levels (info, error, debug, warn, etc.) Related: #1154
1 parent 41d94f2 commit d55ecc4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/spacecat-shared-utils/src/log-wrapper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ export function logWrapper(fn) {
5959
// Enhance context.log directly to include markers in all log statements
6060
context.log = logLevels.reduce((accumulator, level) => {
6161
if (typeof log[level] === 'function') {
62-
accumulator[level] = (...args) => log[level](markerString, ...args);
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);
67+
}
68+
return log[level](...args);
69+
};
6370
}
6471
return accumulator;
6572
}, {});

packages/spacecat-shared-utils/test/log-wrapper.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,31 @@ describe('logWrapper tests', () => {
234234
// context.log should remain unchanged
235235
expect(mockContext.log).to.equal(originalLog);
236236
});
237+
238+
// Test format string interpolation
239+
logLevels.forEach((level) => {
240+
it(`should properly handle format strings with placeholders in ${level} log`, async () => {
241+
getTraceIdStub.returns('1-abc-def');
242+
const originalLog = mockContext.log;
243+
const wrappedFn = logWrapper(mockFnFromSqs);
244+
245+
await wrappedFn(message, mockContext);
246+
247+
// Log with format string placeholders
248+
mockContext.log[level]('Found %d items for site %s', 42, 'example.com');
249+
250+
// Verify that the enhanced log was called with the correct format string and args
251+
expect(originalLog[level].calledOnce).to.be.true;
252+
const callArgs = originalLog[level].getCall(0).args;
253+
254+
// First arg should be the format string with markers prepended
255+
expect(callArgs[0]).to.include('[jobId=');
256+
expect(callArgs[0]).to.include('[traceId=1-abc-def]');
257+
expect(callArgs[0]).to.include('Found %d items for site %s');
258+
259+
// Subsequent args should be the format string arguments
260+
expect(callArgs[1]).to.equal(42);
261+
expect(callArgs[2]).to.equal('example.com');
262+
});
263+
});
237264
});

0 commit comments

Comments
 (0)