Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): write a truncated top-level array as one field

Yudhi Armyndharis1807723f0d56
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index c5755b112..5dbbe47d4 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -302,6 +302,24 @@ describe('LoggerService', () => {
});
});
+ describe('an array given as the metadata itself', () => {
+ const parsed = (): Record<string, unknown> => getLogOutput(consoleSpy) as unknown as Record<string, unknown>;
+
+ it('is written as one [Truncated] field when it is longer than the limit', () => {
+ logger.log('saved', new Array<number>(1_000_001).fill(0) as never);
+
+ expect(parsed()).toMatchObject({ message: 'saved', metadata: '[Truncated]' });
+ expect(parsed()).not.toHaveProperty('0');
+ });
+
+ it('keeps one field per entry when it is within the limit, as before', () => {
+ logger.log('saved', [10, 20] as never);
+
+ expect(parsed()).toMatchObject({ message: 'saved', 0: 10, 1: 20 });
+ expect(parsed()).not.toHaveProperty('metadata');
+ });
+ });
+
describe('an array subclass', () => {
class Rows extends Array<unknown> {
toJSON(): string[] {
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 0b07c0668..c76e126f4 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -232,9 +232,9 @@ export class LoggerService implements NestLoggerService {
};
let output: string;
try {
- output = render(
- typeof context === 'object' && context !== null ? (redactSecrets(context) as Record<string, unknown>) : {},
- );
+ const copy = typeof context === 'object' && context !== null ? redactSecrets(context) : {};
+ // An array over the length limit comes back as the marker text: one field, not a record to spread.
+ output = render(typeof copy === 'string' ? { metadata: copy } : (copy as Record<string, unknown>));
} catch {
// A getter or proxy trap in the caller's metadata threw. The line is still worth writing, without that metadata.
output = render({ metadata: '[Unreadable]' });
diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.ts
index 61bec1631..e8b8c7203 100644
--- a/src/engine/adapters/baileys-logger.spec.ts
+++ b/src/engine/adapters/baileys-logger.spec.ts
@@ -130,6 +130,20 @@ describe('createBaileysLogger writes any record the library hands it', () => {
expect(line).toContain('"password":"[redacted]","visible":"v"');
});
+ it('writes a record that is an array longer than the limit as one [Truncated] field', () => {
+ const line = write(new Array<number>(1_000_001).fill(0), 'x');
+
+ expect(line).toMatchObject({ metadata: '[Truncated]', msg: 'x' });
+ expect(line).not.toHaveProperty('0');
+ });
+
+ it('keeps one field per entry for a record that is an array within the limit, as before', () => {
+ const line = write([10, 20], 'x');
+
+ expect(line).toMatchObject({ 0: 10, 1: 20, msg: 'x' });
+ expect(line).not.toHaveProperty('metadata');
+ });
+
it('writes an array subclass as the plain array it holds', () => {
class Rows extends Array<unknown> {
toJSON(): string[] {
diff --git a/src/engine/adapters/baileys-logger.ts b/src/engine/adapters/baileys-logger.ts
index 1b5028055..6379e885e 100644
--- a/src/engine/adapters/baileys-logger.ts
+++ b/src/engine/adapters/baileys-logger.ts
@@ -59,10 +59,15 @@ export function createBaileysLogger(): BaileysLogger {
const head = { ts: new Date().toISOString(), level: lvl, context: 'baileys-wire' };
let line: string;
try {
+ // An array over the length limit comes back as the marker text: one field, not a record to spread.
+ const copy = typeof obj === 'string' ? undefined : redactSecrets(obj, REDACTION);
const rec =
typeof obj === 'string'
? { msg: obj }
- : { ...(redactSecrets(obj, REDACTION) as Record<string, unknown>), ...(msg ? { msg } : {}) };
+ : {
+ ...(typeof copy === 'string' ? { metadata: copy } : (copy as Record<string, unknown>)),
+ ...(msg ? { msg } : {}),
+ };
line = JSON.stringify({ ...head, ...rec });
} catch {
// A getter or proxy trap in the record threw. This runs inside Baileys and must not throw: write the message alone.