Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions packages/node/src/integrations/console.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as util from 'node:util';
import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import { addConsoleInstrumentationHandler, severityLevelFromString } from '@sentry/utils';
import { addConsoleInstrumentationHandler, severityLevelFromString, truncate } from '@sentry/utils';

const INTEGRATION_NAME = 'Console';

const _consoleIntegration = (() => {
/**
* Capture console logs as breadcrumbs.
*/
export const consoleIntegration = defineIntegration(() => {
return {
name: INTEGRATION_NAME,
setup(client) {
Expand All @@ -18,7 +20,7 @@ const _consoleIntegration = (() => {
{
category: 'console',
level: severityLevelFromString(level),
message: util.format.apply(undefined, args),
message: truncate(util.format.apply(undefined, args), 2048), // 2KB
},
{
input: [...args],
Expand All @@ -28,9 +30,4 @@ const _consoleIntegration = (() => {
});
},
};
}) satisfies IntegrationFn;

/**
* Capture console logs as breadcrumbs.
*/
export const consoleIntegration = defineIntegration(_consoleIntegration);
});
60 changes: 60 additions & 0 deletions packages/node/test/integration/console.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as SentryCore from '@sentry/core';
import { resetInstrumentationHandlers } from '@sentry/utils';
import { getClient } from '../../src';
import type { NodeClient } from '../../src';
import { consoleIntegration } from '../../src/integrations/console';

const addBreadcrumbSpy = jest.spyOn(SentryCore, 'addBreadcrumb');

jest.spyOn(console, 'log').mockImplementation(() => {
// noop so that we don't spam the logs
});

afterEach(() => {
jest.clearAllMocks();
resetInstrumentationHandlers();
});

describe('Console integration', () => {
it('should add a breadcrumb on console.log', () => {
consoleIntegration().setup?.(getClient() as NodeClient);

// eslint-disable-next-line no-console
console.log('test');

expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1);
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
{
category: 'console',
level: 'log',
message: 'test',
},
{
input: ['test'],
level: 'log',
},
);
});

it('should truncate breadcrumbs with more than 2 KB message size', () => {
consoleIntegration().setup?.(getClient() as NodeClient);

const longMsg = 'A'.repeat(10_000);

// eslint-disable-next-line no-console
console.log(longMsg);

expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1);
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
{
category: 'console',
level: 'log',
message: `${'A'.repeat(2048)}...`,
},
{
input: [longMsg],
level: 'log',
},
);
});
});
Loading