diff --git a/plugins/console.js b/plugins/console.js index e1fac679ab3a..b2d40cf4e50a 100644 --- a/plugins/console.js +++ b/plugins/console.js @@ -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(); } } diff --git a/src/console.js b/src/console.js new file mode 100644 index 000000000000..fc95e903d377 --- /dev/null +++ b/src/console.js @@ -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 +}; diff --git a/src/raven.js b/src/raven.js index a623a1672c1c..8add46fe758d 100644 --- a/src/raven.js +++ b/src/raven.js @@ -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'); @@ -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+))?(\/.*)/; @@ -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); }); }