Skip to content

Commit

Permalink
fix breadcrumbs + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
billyvg committed Sep 19, 2024
1 parent 2c0fd0b commit 834fe07
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/replay-internal/src/util/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addBreadcrumb, captureException } from '@sentry/core';
import type { ConsoleLevel, SeverityLevel } from '@sentry/types';
import { logger as coreLogger } from '@sentry/utils';
import { logger as coreLogger, severityLevelFromString } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';

Expand Down Expand Up @@ -64,13 +64,13 @@ function makeReplayLogger(): ReplayLogger {
_logger[name] = (...args: unknown[]) => {
coreLogger[name](PREFIX, ...args);
if (_trace) {
_addBreadcrumb(args[0]);
_addBreadcrumb(args.join(''), severityLevelFromString(name));
}
};
});

_logger.exception = (error: unknown, ...message: unknown[]) => {
if (message && _logger.error) {
if (message.length && _logger.error) {
_logger.error(...message);
}

Expand All @@ -81,7 +81,7 @@ function makeReplayLogger(): ReplayLogger {
} else if (_trace) {
// No need for a breadcrumb if `_capture` is enabled since it should be
// captured as an exception
_addBreadcrumb(error);
_addBreadcrumb(error, 'error');
}
};

Expand Down
88 changes: 88 additions & 0 deletions packages/replay-internal/test/unit/util/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { beforeEach, describe, expect, it } from 'vitest';

import * as SentryCore from '@sentry/core';
import { logger as coreLogger } from '@sentry/utils';
import { logger } from '../../../src/util/logger';

const mockCaptureException = vi.spyOn(SentryCore, 'captureException');
const mockAddBreadcrumb = vi.spyOn(SentryCore, 'addBreadcrumb');
const mockLogError = vi.spyOn(coreLogger, 'error');
vi.spyOn(coreLogger, 'info');
vi.spyOn(coreLogger, 'log');
vi.spyOn(coreLogger, 'warn');

describe('logger', () => {
beforeEach(() => {
vi.clearAllMocks();
});

describe.each([
[false, false],
[false, true],
[true, false],
[true, true],
])('with options: captureExceptions:%s, traceInternals:%s', (captureExceptions, traceInternals) => {
beforeEach(() => {
logger.setConfig({
captureExceptions,
traceInternals,
});
});

it.each([
['info', 'info', 'info message'],
['log', 'log', 'log message'],
['warn', 'warning', 'warn message'],
['error', 'error', 'error message'],
])('%s', (fn, level, message) => {
logger[fn](message);
expect(coreLogger[fn]).toHaveBeenCalledWith('[Replay] ', message);

if (traceInternals) {
expect(mockAddBreadcrumb).toHaveBeenLastCalledWith(
{
category: 'console',
data: { logger: 'replay' },
level,
message: `[Replay] ${message}`,
},
{ level },
);
}
});

it('logs exceptions with a message', () => {
const err = new Error('An error');
logger.exception(err, 'a message');
if (captureExceptions) {
expect(mockCaptureException).toHaveBeenCalledWith(err);
}
expect(mockLogError).toHaveBeenCalledWith('[Replay] ', 'a message');
expect(mockLogError).toHaveBeenLastCalledWith('[Replay] ', err);
expect(mockLogError).toHaveBeenCalledTimes(2);

if (traceInternals) {
expect(mockAddBreadcrumb).toHaveBeenCalledWith(
{
category: 'console',
data: { logger: 'replay' },
level: 'error',
message: '[Replay] a message',
},
{ level: 'error' },
);
}
});

it('logs exceptions without a message', () => {
const err = new Error('An error');
logger.exception(err);
if (captureExceptions) {
expect(mockCaptureException).toHaveBeenCalledWith(err);
expect(mockAddBreadcrumb).not.toHaveBeenCalled();
}
expect(mockLogError).toHaveBeenCalledTimes(1);
expect(mockLogError).toHaveBeenLastCalledWith('[Replay] ', err);
});
});
});

0 comments on commit 834fe07

Please sign in to comment.