9

So if you open up the inspector, you get this (if you're unlucky):

enter image description here

I'm building a tiny JS component which displays debugging information - is there any way to read the number of encountered errors and warnings so far?

A hacky solution I could come up with involves a bit of trickery by replacing the console.(error|log|warn) functions with my own, but I'm yet to test if it works for all cases (e.g. outside of code I own).

Is there a better way to do this?

maligree
  • 5,939
  • 10
  • 34
  • 51
  • You need some kind of wrapper around the code to catch otherwise-uncaught exceptions or you need to tie into the console API somehow. – ssube Jun 17 '16 at 15:11
  • You can probably use [devtools](https://developer.chrome.com/extensions/devtools) using a chrome extension. – mash Jul 27 '16 at 22:42
  • 1
    maybe you can use window.onerror = function (msg, url, lineNo, columnNo, error) – Pawel Dubiel Jul 31 '16 at 03:06
  • I found this article that may help using window.onerror():https://danlimerick.wordpress.com/2014/01/18/how-to-catch-javascript-errors-with-window-onerror-even-on-chrome-and-firefox/ – Duly Kinsky Jul 31 '16 at 15:22

1 Answers1

1

As noted in this answer, it's generally not a good idea to change the behavior of native objects/methods. However, the following code should get you what you need in a fairly innocuous manner:

// Add this IIFE to your codebase:
(() => {
 // Get all of the property names of the console:
 const methodsToTrack = Object.keys(window.console);
 // Create an object to collect total usage tallies in:
 const usageRegistry = {};
 for (let i = 0, j = methodsToTrack.length; i < j; i++) {
  let methodName = methodsToTrack[i];
  // If the property is not a method, don't touch it:
  if(typeof window.console[methodName] !== 'function') {
   continue;
  }
  // Cache the original console method here:
  let consoleMethod = window.console[methodName];
  // Overwrite console's method to increment the counter:
  window.console[methodName] = function () {
   // Defining registry properties here, so the registry only contains values for methods that were accessed:
   usageRegistry[methodName] = usageRegistry[methodName] || 0;
   // Execute the original method's behavior, capturing the returned value (if any) in a var, to return it at the end:
   const returnedValue = consoleMethod(...arguments);
   // Increment the usage registry for the executed method:
   usageRegistry[methodName]++;
   // Return the value the console's method would have returned, so the new method has the same signature as the old.
   return returnedValue;
  };

 }
 // Define a funciton to output the totals to a console log, then clean up after itself:
 window.showConsoleTallies = function () {
  window.console.log(usageRegistry);
  usageRegistry['log']--;
 }
})();

// Examples:
showConsoleTallies();
console.log('log 1');
console.error('error 1');
console.log('log 2');
console.warn('warn 1');
console.error('error 2');
console.log('log 3');
showConsoleTallies();

PS: That's the ECMA6 version, but feel free to run it through Babel if you'd like it to be compiled for use in older browsers.

Community
  • 1
  • 1
jmealy
  • 583
  • 1
  • 5
  • 14
  • will this count 404s and exceptions like devtool's counter? – dandavis Aug 03 '16 at 20:40
  • Unfortunately, @dandavis, I think it will only work to tally the console methods the developer utilizes. `fetch('http://invalidajaxcall.com/').catch(showConsoleTallies)` (for instance) doesn't appear to use console.error, under the hood. – jmealy Aug 10 '16 at 21:57