2016-06-17 58 views
8

Quindi, se si apre la finestra di ispezione, si ottiene questo (se siete sfortunati):Integrazione della console: ottieni il numero di errori/avvisi lanciati?

enter image description here

Sto costruendo una piccola componente di JS che visualizza le informazioni di debug - C'è un modo per leggere il numero di errori e avvisi riscontrati finora?

Una soluzione hacky che potrei inventare comporta un po 'di trucco sostituendo le funzioni console.(error|log|warn) con le mie, ma devo ancora verificare se funziona per tutti i casi (ad esempio, al di fuori del codice che possiedo).

C'è un modo migliore per farlo?

+0

È necessario un qualche tipo di wrapper attorno al codice per rilevare le eccezioni altrimenti non rilevate o è necessario collegarsi all'API della console in qualche modo. – ssube

+0

Probabilmente si può usare [devtools] (https://developer.chrome.com/extensions/devtools) usando un'estensione chrome. – mash

+1

forse puoi usare window.onerror = function (msg, url, lineNo, columnNo, error) –

risposta

1

Come indicato nella risposta this, non è generalmente una buona idea modificare il comportamento degli oggetti/metodi nativi. Tuttavia, il seguente codice dovrebbe ottenere quello che ti serve in modo abbastanza innocuo:

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

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

 
// 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: Questo è la versione ECMA6, ma sentitevi liberi di run it through Babel se vuoi che sia compilato per l'uso nei browser più vecchi.

+0

conterrà 404s ed eccezioni come il contatore di devtool? – dandavis

+0

Purtroppo, @dandavis, penso che funzionerà solo per calcolare i metodi di console utilizzati dallo sviluppatore. 'fetch ('http://invalidajaxcall.com/') .catch (showConsoleTallies)' (per esempio) non sembra usare console.error, sotto il cofano. – jmealy