Skip to content

Commit a6c653e

Browse files
committed
refine based on implementation
1 parent a1b1ade commit a6c653e

File tree

3 files changed

+13
-56
lines changed

3 files changed

+13
-56
lines changed

packages/core/src/integrations/consola.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,14 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
189189
const { normalizeDepth = 3, normalizeMaxBreadth = 1_000 } = client.getOptions();
190190

191191
// Format the log message using the same approach as consola's basic reporter
192-
const message =
193-
consolaMessage || (args && args.length > 0 ? formatConsoleArgs(args, normalizeDepth, normalizeMaxBreadth) : '');
192+
const messageParts = [];
193+
if (consolaMessage) {
194+
messageParts.push(consolaMessage);
195+
}
196+
if (args && args.length > 0) {
197+
messageParts.push(formatConsoleArgs(args, normalizeDepth, normalizeMaxBreadth));
198+
}
199+
const message = messageParts.join(' ');
194200

195201
// Build attributes
196202
const attributes: Record<string, unknown> = {
@@ -210,7 +216,6 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
210216
attributes['consola.level'] = level;
211217
}
212218

213-
// Capture the log
214219
_INTERNAL_captureLog({
215220
level: logSeverityLevel,
216221
message,
@@ -244,7 +249,7 @@ const CONSOLA_TYPE_TO_LOG_SEVERITY_LEVEL_MAP: Record<string, LogSeverityLevel> =
244249

245250
// Mapping from consola log levels (numbers) to Sentry log severity levels
246251
const CONSOLA_LEVEL_TO_LOG_SEVERITY_LEVEL_MAP: Record<number, LogSeverityLevel> = {
247-
0: 'error', // Fatal and Error
252+
0: 'fatal', // Fatal and Error
248253
1: 'warn', // Warnings
249254
2: 'info', // Normal logs
250255
3: 'info', // Informational logs, success, fail, ready, start, ...

packages/core/src/logs/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function formatConsoleArgs(values: unknown[], normalizeDepth: number, nor
3030
* @param normalizeMaxBreadth - The max breadth to normalize the values.
3131
* @returns The joined string.
3232
*/
33-
function safeJoinConsoleArgs(values: unknown[], normalizeDepth: number, normalizeMaxBreadth: number): string {
33+
export function safeJoinConsoleArgs(values: unknown[], normalizeDepth: number, normalizeMaxBreadth: number): string {
3434
return values
3535
.map(value =>
3636
isPrimitive(value) ? String(value) : JSON.stringify(normalize(value, normalizeDepth, normalizeMaxBreadth)),

packages/core/test/lib/integrations/consola.test.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ vi.mock('../../../src/logs/exports', () => ({
1010
_INTERNAL_captureLog: vi.fn(),
1111
}));
1212

13-
vi.mock('../../../src/logs/utils', () => ({
14-
formatConsoleArgs: vi.fn(),
13+
vi.mock('../../../src/logs/utils', async actual => ({
14+
formatConsoleArgs: vi.fn(((await actual()) as any).formatConsoleArgs),
1515
}));
1616

1717
vi.mock('../../../src/currentScopes', () => ({
@@ -33,7 +33,6 @@ describe('createConsolaReporter', () => {
3333
});
3434

3535
vi.mocked(getClient).mockReturnValue(mockClient);
36-
vi.mocked(formatConsoleArgs).mockImplementation(args => args.join(' '));
3736
});
3837

3938
afterEach(() => {
@@ -48,37 +47,6 @@ describe('createConsolaReporter', () => {
4847
log: expect.any(Function),
4948
});
5049
});
51-
52-
it('should use provided client for normalization options', () => {
53-
const customClient = new TestClient({
54-
...getDefaultTestClientOptions({ dsn: 'https://custom@domain/123' }),
55-
enableLogs: true,
56-
normalizeDepth: 5,
57-
normalizeMaxBreadth: 2000,
58-
});
59-
60-
const reporter = createConsolaReporter({ client: customClient });
61-
62-
reporter.log({
63-
type: 'info',
64-
args: ['test', { complex: 'object' }],
65-
});
66-
67-
expect(formatConsoleArgs).toHaveBeenCalledWith(['test', { complex: 'object' }], 5, 2000);
68-
});
69-
70-
it('should not capture logs when no client is available', () => {
71-
vi.mocked(getClient).mockReturnValue(undefined);
72-
73-
const reporter = createConsolaReporter();
74-
75-
reporter.log({
76-
type: 'error',
77-
message: 'Should not be captured',
78-
});
79-
80-
expect(_INTERNAL_captureLog).not.toHaveBeenCalled();
81-
});
8250
});
8351

8452
describe('log capturing', () => {
@@ -107,7 +75,6 @@ describe('createConsolaReporter', () => {
10775
'consola.tag': 'test',
10876
'consola.type': 'error',
10977
'consola.level': 0,
110-
'consola.timestamp': '2023-01-01T00:00:00.000Z',
11178
},
11279
});
11380
});
@@ -232,10 +199,9 @@ describe('createConsolaReporter', () => {
232199

233200
sentryReporter.log(logObj);
234201

235-
expect(formatConsoleArgs).toHaveBeenCalledWith(['Message', circular], 3, 1000);
236202
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
237203
level: 'info',
238-
message: 'Message [object Object]',
204+
message: 'Message {"self":"[Circular ~]"}',
239205
attributes: {
240206
'sentry.origin': 'auto.logging.consola',
241207
'consola.type': 'info',
@@ -290,20 +256,6 @@ describe('createConsolaReporter', () => {
290256
});
291257
});
292258
});
293-
294-
it('should handle errors in reporter gracefully', () => {
295-
vi.mocked(_INTERNAL_captureLog).mockImplementation(() => {
296-
throw new Error('Capture error');
297-
});
298-
299-
// Should not throw
300-
expect(() => {
301-
sentryReporter.log({
302-
type: 'error',
303-
message: 'Test message',
304-
});
305-
}).not.toThrow();
306-
});
307259
});
308260

309261
describe('level filtering', () => {

0 commit comments

Comments
 (0)