Skip to content

Fix console plugin circular dep causing bad CDN build #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2016
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
37 changes: 6 additions & 31 deletions plugins/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,21 @@
*/
'use strict';

var wrapConsoleMethod = require('../src/console').wrapMethod;

function consolePlugin(Raven, console, pluginOptions) {
console = console || window.console || {};
pluginOptions = pluginOptions || {};

var originalConsole = console,
logLevels = pluginOptions.levels || ['debug', 'info', 'warn', 'error'],
var logLevels = pluginOptions.levels || ['debug', 'info', 'warn', 'error'],
level = logLevels.pop();

var logForGivenLevel = function(l) {
var originalConsoleLevel = console[l];

// warning level is the only level that doesn't map up
// correctly with what Sentry expects.
if (l === 'warn') l = 'warning';
return function () {
var args = [].slice.call(arguments);

var msg = '' + args.join(' ');
var data = {level: l, logger: 'console', extra: { 'arguments': args }};
if (pluginOptions.callback) {
pluginOptions.callback(msg, data);
} else {
Raven.captureMessage(msg, data);
}

// this fails for some browsers. :(
if (originalConsoleLevel) {
// IE9 doesn't allow calling apply on console functions directly
// See: https://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function#answer-5473193
Function.prototype.apply.call(
originalConsoleLevel,
originalConsole,
args
);
}
};
var callback = function (msg, data) {
Raven.captureMessage(msg, data);
};

while(level) {
console[level] = logForGivenLevel(level);
wrapConsoleMethod(console, level, callback);
level = logLevels.pop();
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

var wrapMethod = function(console, level, callback) {
var originalConsoleLevel = console[level];
var originalConsole = console;

if (!(level in console)) {
return;
}

var sentryLevel = level === 'warn'
? 'warning'
: level;

console[level] = function () {
var args = [].slice.call(arguments);

var msg = '' + args.join(' ');
var data = {level: sentryLevel, logger: 'console', extra: {'arguments': args}};
callback && callback(msg, data);

// this fails for some browsers. :(
if (originalConsoleLevel) {
// IE9 doesn't allow calling apply on console functions directly
// See: https://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function#answer-5473193
Function.prototype.apply.call(
originalConsoleLevel,
originalConsole,
args
);
}
};
};

module.exports = {
wrapMethod: wrapMethod
};
22 changes: 12 additions & 10 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';

var TraceKit = require('../vendor/TraceKit/tracekit');
var consolePlugin = require('../plugins/console');
var RavenConfigError = require('./configError');
var utils = require('./utils');

Expand All @@ -20,6 +19,8 @@ var uuid4 = utils.uuid4;
var htmlTreeAsString = utils.htmlTreeAsString;
var parseUrl = utils.parseUrl;

var wrapConsoleMethod = require('./console').wrapMethod;

var dsnKeys = 'source protocol user pass host port path'.split(' '),
dsnPattern = /^(?:(\w+):)?\/\/(?:(\w+)(:\w+)?@)?([\w\.-]+)(?::(\d+))?(\/.*)/;

Expand Down Expand Up @@ -917,16 +918,17 @@ Raven.prototype = {
}

// console
var consoleMethodCallback = function (msg, data) {
self.captureBreadcrumb({
message: msg,
level: data.level,
category: 'console'
});
};

if ('console' in window && console.log) {
consolePlugin(self, console, {
levels: ['debug', 'info', 'warn', 'error', 'log'],
callback: function (msg, data) {
self.captureBreadcrumb({
message: msg,
level: data.level,
category: 'console'
});
}
each(['debug', 'info', 'warn', 'error', 'log'], function (_, level) {
wrapConsoleMethod(console, level, consoleMethodCallback);
});
}

Expand Down