Skip to content

Commit 941a9c5

Browse files
committed
feat(sdk-logs): add configurable depth option to ConsoleLogRecordExporter
Add optional depth parameter to ConsoleLogRecordExporter constructor to control console.dir output depth. Defaults to 3 for backward compatibility. - Add ConsoleLogRecordOptions interface with depth parameter - Add constructor to accept options - Use configurable depth instead of hardcoded value - Add tests for default, custom numeric, and null depth values
1 parent 13c0790 commit 941a9c5

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

experimental/packages/sdk-logs/src/export/ConsoleLogRecordExporter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import {
2323
import type { ReadableLogRecord } from './ReadableLogRecord';
2424
import type { LogRecordExporter } from './LogRecordExporter';
2525

26+
interface ConsoleLogRecordOptions {
27+
depth?: number | null;
28+
}
29+
2630
/**
2731
* This is implementation of {@link LogRecordExporter} that prints LogRecords to the
2832
* console. This class can be used for diagnostic purposes.
@@ -32,6 +36,12 @@ import type { LogRecordExporter } from './LogRecordExporter';
3236

3337
/* eslint-disable no-console */
3438
export class ConsoleLogRecordExporter implements LogRecordExporter {
39+
protected _depth: number | null;
40+
41+
constructor(options?: ConsoleLogRecordOptions) {
42+
this._depth = typeof options?.depth !== 'undefined' ? options?.depth : 3;
43+
}
44+
3545
/**
3646
* Export logs.
3747
* @param logs
@@ -82,7 +92,7 @@ export class ConsoleLogRecordExporter implements LogRecordExporter {
8292
done?: (result: ExportResult) => void
8393
): void {
8494
for (const logRecord of logRecords) {
85-
console.dir(this._exportInfo(logRecord), { depth: 3 });
95+
console.dir(this._exportInfo(logRecord), { depth: this._depth });
8696
}
8797
done?.({ code: ExportResultCode.SUCCESS });
8898
}

experimental/packages/sdk-logs/test/common/export/ConsoleLogRecordExporter.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,47 @@ describe('ConsoleLogRecordExporter', () => {
9191
assert.ok(spyExport.calledOnce);
9292
});
9393
});
94+
95+
it('should use default depth of 3', () => {
96+
const consoleExporter = new ConsoleLogRecordExporter();
97+
const spyConsole = sinon.spy(console, 'dir');
98+
const provider = new LoggerProvider({
99+
processors: [new SimpleLogRecordProcessor(consoleExporter)],
100+
});
101+
102+
provider.getLogger('test').emit({ body: 'test' });
103+
104+
const consoleArgs = spyConsole.args[0];
105+
const options = consoleArgs[1];
106+
assert.strictEqual(options?.depth, 3);
107+
});
108+
109+
it('should use custom depth when provided', () => {
110+
const consoleExporter = new ConsoleLogRecordExporter({ depth: 5 });
111+
const spyConsole = sinon.spy(console, 'dir');
112+
const provider = new LoggerProvider({
113+
processors: [new SimpleLogRecordProcessor(consoleExporter)],
114+
});
115+
116+
provider.getLogger('test').emit({ body: 'test' });
117+
118+
const consoleArgs = spyConsole.args[0];
119+
const options = consoleArgs[1];
120+
assert.strictEqual(options?.depth, 5);
121+
});
122+
123+
it('should use null depth when explicitly provided', () => {
124+
const consoleExporter = new ConsoleLogRecordExporter({ depth: null });
125+
const spyConsole = sinon.spy(console, 'dir');
126+
const provider = new LoggerProvider({
127+
processors: [new SimpleLogRecordProcessor(consoleExporter)],
128+
});
129+
130+
provider.getLogger('test').emit({ body: 'test' });
131+
132+
const consoleArgs = spyConsole.args[0];
133+
const options = consoleArgs[1];
134+
assert.strictEqual(options?.depth, null);
135+
});
94136
});
95137
});

0 commit comments

Comments
 (0)