Skip to content

Commit

Permalink
fix(console instrumentation): revert to former log parsing behavior (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
codecapitano authored Nov 20, 2024
1 parent 711960a commit 6333f8a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('ConsoleInstrumentation', () => {
);
expect((mockTransport.items[1] as TransportItem<ExceptionEvent>)?.payload.type).toBe('Error');
expect((mockTransport.items[1] as TransportItem<ExceptionEvent>)?.payload.value).toBe(
'console.error: with object {"foo":"bar","baz":"bam"}'
'console.error: with object [object Object]'
);
});

Expand All @@ -77,7 +77,7 @@ describe('ConsoleInstrumentation', () => {
console.error('with circular refs object', objWithCircularRef);

expect((mockTransport.items[0] as TransportItem<ExceptionEvent>)?.payload.value).toBe(
'console.error: with circular refs object {"foo":"bar","baz":"bam","circular":null}'
'console.error: with circular refs object [object Object]'
);
});

Expand Down Expand Up @@ -106,7 +106,7 @@ describe('ConsoleInstrumentation', () => {

expect((mockTransport.items[0] as TransportItem<LogEvent>)?.payload.message).toBe('console.error log no 1');
expect((mockTransport.items[1] as TransportItem<LogEvent>)?.payload.message).toBe(
'console.error log with object {"foo":"bar","baz":"bam"}'
'console.error log with object [object Object]'
);
});

Expand Down
12 changes: 3 additions & 9 deletions packages/web-sdk/src/instrumentations/console/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { allLogLevels, BaseInstrumentation, isArray, isObject, LogLevel, VERSION } from '@grafana/faro-core';

import { stringifyExternalJson } from '../../utils';
import { allLogLevels, BaseInstrumentation, defaultLogArgsSerializer, LogLevel, VERSION } from '@grafana/faro-core';

import type { ConsoleInstrumentationOptions } from './types';

Expand All @@ -27,9 +25,9 @@ export class ConsoleInstrumentation extends BaseInstrumentation {
console[level] = (...args) => {
try {
if (level === LogLevel.ERROR && !this.options?.consoleErrorAsLog) {
this.api.pushError(new Error('console.error: ' + formatConsoleArgs(args)));
this.api.pushError(new Error('console.error: ' + defaultLogArgsSerializer(args)));
} else {
this.api.pushLog([formatConsoleArgs(args)], { level });
this.api.pushLog(args, { level });
}
} catch (err) {
this.logError(err);
Expand All @@ -40,7 +38,3 @@ export class ConsoleInstrumentation extends BaseInstrumentation {
});
}
}

function formatConsoleArgs(args: [any?, ...any[]]) {
return args.map((arg) => (isObject(arg) || isArray(arg) ? stringifyExternalJson(arg) : arg)).join(' ');
}

0 comments on commit 6333f8a

Please sign in to comment.