File tree Expand file tree Collapse file tree 2 files changed +67
-10
lines changed Expand file tree Collapse file tree 2 files changed +67
-10
lines changed Original file line number Diff line number Diff line change 11import * as util from 'node:util' ;
22import { addBreadcrumb , defineIntegration , getClient } from '@sentry/core' ;
3- import type { IntegrationFn } from '@sentry/types' ;
4- import { addConsoleInstrumentationHandler , severityLevelFromString } from '@sentry/utils' ;
3+ import { addConsoleInstrumentationHandler , severityLevelFromString , truncate } from '@sentry/utils' ;
54
65const INTEGRATION_NAME = 'Console' ;
76
8- const _consoleIntegration = ( ( ) => {
7+ /**
8+ * Capture console logs as breadcrumbs.
9+ */
10+ export const consoleIntegration = defineIntegration ( ( ) => {
911 return {
1012 name : INTEGRATION_NAME ,
1113 setup ( client ) {
@@ -18,7 +20,7 @@ const _consoleIntegration = (() => {
1820 {
1921 category : 'console' ,
2022 level : severityLevelFromString ( level ) ,
21- message : util . format . apply ( undefined , args ) ,
23+ message : truncate ( util . format . apply ( undefined , args ) , 2048 ) , // 2KB
2224 } ,
2325 {
2426 input : [ ...args ] ,
@@ -28,9 +30,4 @@ const _consoleIntegration = (() => {
2830 } ) ;
2931 } ,
3032 } ;
31- } ) satisfies IntegrationFn ;
32-
33- /**
34- * Capture console logs as breadcrumbs.
35- */
36- export const consoleIntegration = defineIntegration ( _consoleIntegration ) ;
33+ } ) ;
Original file line number Diff line number Diff line change 1+ import * as SentryCore from '@sentry/core' ;
2+ import { resetInstrumentationHandlers } from '@sentry/utils' ;
3+ import { getClient } from '../../src' ;
4+ import type { NodeClient } from '../../src' ;
5+ import { consoleIntegration } from '../../src/integrations/console' ;
6+
7+ const addBreadcrumbSpy = jest . spyOn ( SentryCore , 'addBreadcrumb' ) ;
8+
9+ jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => {
10+ // noop so that we don't spam the logs
11+ } ) ;
12+
13+ afterEach ( ( ) => {
14+ jest . clearAllMocks ( ) ;
15+ resetInstrumentationHandlers ( ) ;
16+ } ) ;
17+
18+ describe ( 'Console integration' , ( ) => {
19+ it ( 'should add a breadcrumb on console.log' , ( ) => {
20+ consoleIntegration ( ) . setup ?.( getClient ( ) as NodeClient ) ;
21+
22+ // eslint-disable-next-line no-console
23+ console . log ( 'test' ) ;
24+
25+ expect ( addBreadcrumbSpy ) . toHaveBeenCalledTimes ( 1 ) ;
26+ expect ( addBreadcrumbSpy ) . toHaveBeenCalledWith (
27+ {
28+ category : 'console' ,
29+ level : 'log' ,
30+ message : 'test' ,
31+ } ,
32+ {
33+ input : [ 'test' ] ,
34+ level : 'log' ,
35+ } ,
36+ ) ;
37+ } ) ;
38+
39+ it ( 'should truncate breadcrumbs with more than 2 KB message size' , ( ) => {
40+ consoleIntegration ( ) . setup ?.( getClient ( ) as NodeClient ) ;
41+
42+ const longMsg = 'A' . repeat ( 10_000 ) ;
43+
44+ // eslint-disable-next-line no-console
45+ console . log ( longMsg ) ;
46+
47+ expect ( addBreadcrumbSpy ) . toHaveBeenCalledTimes ( 1 ) ;
48+ expect ( addBreadcrumbSpy ) . toHaveBeenCalledWith (
49+ {
50+ category : 'console' ,
51+ level : 'log' ,
52+ message : `${ 'A' . repeat ( 2048 ) } ...` ,
53+ } ,
54+ {
55+ input : [ longMsg ] ,
56+ level : 'log' ,
57+ } ,
58+ ) ;
59+ } ) ;
60+ } ) ;
You can’t perform that action at this time.
0 commit comments