JavaScript 中有没有办法监听控制台事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8000009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way in JavaScript to listen console events?
提问by Milan Jaric
I'm trying to write handler for uncaught exceptions and browser warnings in Javascript. All errors and warnings should be sent to server for later review.
我正在尝试在 Javascript 中为未捕获的异常和浏览器警告编写处理程序。所有错误和警告都应发送到服务器以供以后查看。
Handled exceptions can be caught and easily logged with
可以捕获并轻松记录已处理的异常
console.error("Error: ...");
or
或者
console.warn("Warning: ...");
So they are not problem if they are called from javascript code, even more, unhandled exceptions could be caught with this peace of code:
因此,如果它们是从 javascript 代码调用的,它们就没有问题,甚至可以通过这种和平的代码捕获未处理的异常:
window.onerror = function(){
// add to errors Stack trace etc.
});
}
so exceptions are pretty covered but I've stuck with warnings which browser sends to console. For instance security or html validation warnings. Example below is taken from Google Chrome console
所以异常被涵盖在内,但我一直坚持浏览器发送到控制台的警告。例如安全或 html 验证警告。下面的示例取自 Google Chrome 控制台
The page at https://domainname.com/ran insecure content from http://domainname.com/javascripts/codex/MANIFEST.js.
https://domainname.com/上的页面运行来自http://domainname.com/javascripts/codex/MANIFEST.js 的不安全内容 。
It would be great if there is some event like window.onerror but for warnings. Any thoughts?
如果有一些像 window.onerror 这样的事件但用于警告,那就太好了。有什么想法吗?
回答by Facebook Staff are Complicit
You could just wrap the console
methods yourself. For example, to record each call in an array:
你可以console
自己包装这些方法。例如,要在数组中记录每个调用:
var logOfConsole = [];
var _log = console.log,
_warn = console.warn,
_error = console.error;
console.log = function() {
logOfConsole.push({method: 'log', arguments: arguments});
return _log.apply(console, arguments);
};
console.warn = function() {
logOfConsole.push({method: 'warn', arguments: arguments});
return _warn.apply(console, arguments);
};
console.error = function() {
logOfConsole.push({method: 'error', arguments: arguments});
return _error.apply(console, arguments);
};
回答by Inanc Gumus
More Succint Way:
更简洁的方式:
// this method will proxy your custom method with the original one
function proxy(context, method, message) {
return function() {
method.apply(context, [message].concat(Array.prototype.slice.apply(arguments)))
}
}
// let's do the actual proxying over originals
console.log = proxy(console, console.log, 'Log:')
console.error = proxy(console, console.error, 'Error:')
console.warn = proxy(console, console.warn, 'Warning:')
// let's test
console.log('im from console.log', 1, 2, 3);
console.error('im from console.error', 1, 2, 3);
console.warn('im from console.warn', 1, 2, 3);
回答by Ludovic Feltz
I know it's an old post but it can be useful anyway as others solution are not compatible with older browsers.
我知道这是一篇旧帖子,但无论如何它都会很有用,因为其他解决方案与旧浏览器不兼容。
You can redefine the behavior of each functionof the console (and for all browsers) like this:
您可以像这样重新定义控制台(以及所有浏览器)的每个功能的行为:
// define a new console
var console = (function(oldCons){
return {
log: function(text){
oldCons.log(text);
// Your code
},
info: function (text) {
oldCons.info(text);
// Your code
},
warn: function (text) {
oldCons.warn(text);
// Your code
},
error: function (text) {
oldCons.error(text);
// Your code
}
};
}(window.console));
//Then redefine the old console
window.console = console;
回答by Samson
I needed to debug console output on mobile devices so I built this drop-in library to capture console output and category and dump it to the page. Check the source code, it's quite straightforward.
我需要在移动设备上调试控制台输出,所以我构建了这个嵌入式库来捕获控制台输出和类别并将其转储到页面。查看源代码,很简单。
回答by Joshua
In the same function that you are using to do console.log(), simply post the same message to a web service that you are recording the logs on.
在您用来执行 console.log() 的同一函数中,只需将相同的消息发布到您正在记录日志的 Web 服务。
回答by zzzzBov
You're going about this backwards. Instead of intercepting when an error is logged, trigger an event as part of the error handling mechanism and log it as one of the event listeners:
你这是倒退。与其在记录错误时进行拦截,不如触发事件作为错误处理机制的一部分,并将其记录为事件侦听器之一:
try
{
//might throw an exception
foo();
}
catch (e)
{
$(document).trigger('customerror', e);
}
function customErrorHandler(event, ex)
{
console.error(ex)
}
function customErrorHandler2(event, ex)
{
$.post(url, ex);
}
this code uses jQuery and is oversimplified strictly for use as an example.
这段代码使用了 jQuery,并被严格地过度简化以用作示例。