Skip to content

Commit 996e09a

Browse files
committed
Merge pull request #566 from getsentry/fix-console-plugin
Fix console plugin circular dep causing bad CDN build
2 parents 42ab239 + 5c8a989 commit 996e09a

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

plugins/console.js

+6-31
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,21 @@
1111
*/
1212
'use strict';
1313

14+
var wrapConsoleMethod = require('../src/console').wrapMethod;
15+
1416
function consolePlugin(Raven, console, pluginOptions) {
1517
console = console || window.console || {};
1618
pluginOptions = pluginOptions || {};
1719

18-
var originalConsole = console,
19-
logLevels = pluginOptions.levels || ['debug', 'info', 'warn', 'error'],
20+
var logLevels = pluginOptions.levels || ['debug', 'info', 'warn', 'error'],
2021
level = logLevels.pop();
2122

22-
var logForGivenLevel = function(l) {
23-
var originalConsoleLevel = console[l];
24-
25-
// warning level is the only level that doesn't map up
26-
// correctly with what Sentry expects.
27-
if (l === 'warn') l = 'warning';
28-
return function () {
29-
var args = [].slice.call(arguments);
30-
31-
var msg = '' + args.join(' ');
32-
var data = {level: l, logger: 'console', extra: { 'arguments': args }};
33-
if (pluginOptions.callback) {
34-
pluginOptions.callback(msg, data);
35-
} else {
36-
Raven.captureMessage(msg, data);
37-
}
38-
39-
// this fails for some browsers. :(
40-
if (originalConsoleLevel) {
41-
// IE9 doesn't allow calling apply on console functions directly
42-
// See: https://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function#answer-5473193
43-
Function.prototype.apply.call(
44-
originalConsoleLevel,
45-
originalConsole,
46-
args
47-
);
48-
}
49-
};
23+
var callback = function (msg, data) {
24+
Raven.captureMessage(msg, data);
5025
};
5126

5227
while(level) {
53-
console[level] = logForGivenLevel(level);
28+
wrapConsoleMethod(console, level, callback);
5429
level = logLevels.pop();
5530
}
5631
}

src/console.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
var wrapMethod = function(console, level, callback) {
4+
var originalConsoleLevel = console[level];
5+
var originalConsole = console;
6+
7+
if (!(level in console)) {
8+
return;
9+
}
10+
11+
var sentryLevel = level === 'warn'
12+
? 'warning'
13+
: level;
14+
15+
console[level] = function () {
16+
var args = [].slice.call(arguments);
17+
18+
var msg = '' + args.join(' ');
19+
var data = {level: sentryLevel, logger: 'console', extra: {'arguments': args}};
20+
callback && callback(msg, data);
21+
22+
// this fails for some browsers. :(
23+
if (originalConsoleLevel) {
24+
// IE9 doesn't allow calling apply on console functions directly
25+
// See: https://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function#answer-5473193
26+
Function.prototype.apply.call(
27+
originalConsoleLevel,
28+
originalConsole,
29+
args
30+
);
31+
}
32+
};
33+
};
34+
35+
module.exports = {
36+
wrapMethod: wrapMethod
37+
};

src/raven.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
'use strict';
33

44
var TraceKit = require('../vendor/TraceKit/tracekit');
5-
var consolePlugin = require('../plugins/console');
65
var RavenConfigError = require('./configError');
76
var utils = require('./utils');
87

@@ -20,6 +19,8 @@ var uuid4 = utils.uuid4;
2019
var htmlTreeAsString = utils.htmlTreeAsString;
2120
var parseUrl = utils.parseUrl;
2221

22+
var wrapConsoleMethod = require('./console').wrapMethod;
23+
2324
var dsnKeys = 'source protocol user pass host port path'.split(' '),
2425
dsnPattern = /^(?:(\w+):)?\/\/(?:(\w+)(:\w+)?@)?([\w\.-]+)(?::(\d+))?(\/.*)/;
2526

@@ -917,16 +918,17 @@ Raven.prototype = {
917918
}
918919

919920
// console
921+
var consoleMethodCallback = function (msg, data) {
922+
self.captureBreadcrumb({
923+
message: msg,
924+
level: data.level,
925+
category: 'console'
926+
});
927+
};
928+
920929
if ('console' in window && console.log) {
921-
consolePlugin(self, console, {
922-
levels: ['debug', 'info', 'warn', 'error', 'log'],
923-
callback: function (msg, data) {
924-
self.captureBreadcrumb({
925-
message: msg,
926-
level: data.level,
927-
category: 'console'
928-
});
929-
}
930+
each(['debug', 'info', 'warn', 'error', 'log'], function (_, level) {
931+
wrapConsoleMethod(console, level, consoleMethodCallback);
930932
});
931933
}
932934

0 commit comments

Comments
 (0)