Skip to content

Commit a0e1f14

Browse files
committed
feat: Rework console integration
1 parent 8966bf7 commit a0e1f14

File tree

1 file changed

+38
-67
lines changed

1 file changed

+38
-67
lines changed

packages/node/src/integrations/console.ts

+38-67
Original file line numberDiff line numberDiff line change
@@ -18,82 +18,53 @@ export class Console implements Integration {
1818
* @inheritDoc
1919
*/
2020
public setupOnce(): void {
21-
const nativeModule = require('module');
22-
fill(nativeModule, '_load', loadWrapper(nativeModule));
23-
// special case: since console is built-in and app-level code won't require() it, do that here
24-
require('console');
21+
const consoleModule = require('console');
22+
for (const level of ['debug', 'info', 'warn', 'error', 'log']) {
23+
fill(consoleModule, level, createConsoleWrapper(level));
24+
}
2525
}
2626
}
2727

28-
/**
29-
* Wrapper function for internal _load calls within `require`
30-
*/
31-
function loadWrapper(nativeModule: any): any {
32-
// We need to use some functional-style currying to pass values around
33-
// as we cannot rely on `bind`, because this has to preserve correct
34-
// context for native calls
35-
return function(originalLoad: () => any): any {
36-
return function(moduleId: string): any {
37-
const originalModule = originalLoad.apply(nativeModule, arguments);
38-
39-
if (moduleId !== 'console' || originalModule.__sentry__) {
40-
return originalModule;
41-
}
42-
43-
['debug', 'info', 'warn', 'error', 'log'].forEach(consoleWrapper(originalModule));
44-
45-
originalModule.__sentry__ = true;
46-
return originalModule;
47-
};
48-
};
49-
}
50-
5128
/**
5229
* Wrapper function that'll be used for every console level
5330
*/
54-
function consoleWrapper(originalModule: any): any {
55-
return function(level: string): any {
56-
if (!(level in originalModule)) {
57-
return;
58-
}
31+
function createConsoleWrapper(level: string): (originalConsoleMethod: () => void) => void {
32+
return function consoleWrapper(originalConsoleMethod: () => void): () => void {
33+
let sentryLevel: Severity;
5934

60-
fill(originalModule, level, function(originalConsoleLevel: () => any): any {
61-
let sentryLevel: Severity;
35+
switch (level) {
36+
case 'debug':
37+
sentryLevel = Severity.Debug;
38+
break;
39+
case 'error':
40+
sentryLevel = Severity.Error;
41+
break;
42+
case 'info':
43+
sentryLevel = Severity.Info;
44+
break;
45+
case 'warn':
46+
sentryLevel = Severity.Warning;
47+
break;
48+
default:
49+
sentryLevel = Severity.Log;
50+
}
6251

63-
switch (level) {
64-
case 'debug':
65-
sentryLevel = Severity.Debug;
66-
break;
67-
case 'error':
68-
sentryLevel = Severity.Error;
69-
break;
70-
case 'info':
71-
sentryLevel = Severity.Info;
72-
break;
73-
case 'warn':
74-
sentryLevel = Severity.Warning;
75-
break;
76-
default:
77-
sentryLevel = Severity.Log;
52+
return function(this: typeof console): void {
53+
if (getCurrentHub().getIntegration(Console)) {
54+
getCurrentHub().addBreadcrumb(
55+
{
56+
category: 'console',
57+
level: sentryLevel,
58+
message: util.format.apply(undefined, arguments),
59+
},
60+
{
61+
input: [...arguments],
62+
level,
63+
},
64+
);
7865
}
7966

80-
return function(): any {
81-
if (getCurrentHub().getIntegration(Console)) {
82-
getCurrentHub().addBreadcrumb(
83-
{
84-
category: 'console',
85-
level: sentryLevel,
86-
message: util.format.apply(undefined, arguments),
87-
},
88-
{
89-
input: [...arguments],
90-
level,
91-
},
92-
);
93-
}
94-
95-
originalConsoleLevel.apply(originalModule, arguments);
96-
};
97-
});
67+
originalConsoleMethod.apply(this, arguments);
68+
};
9869
};
9970
}

0 commit comments

Comments
 (0)