简体   繁体   English

检测 console.warn 消息

[英]Detect a console.warn message

I have a need to find out when a warning (not error) message is sent to the browser's console.log .我需要找出警告(不是错误)消息何时发送到浏览器的 console.log 。 From research, I know that I can detect error messages with JavaScript code similar to this:从研究中,我知道我可以使用类似于以下的 JavaScript 代码检测错误消息:

window.onerror = function(errorMsg, url, lineNumber){
    // any action you want goes here
    // errorMsg is the error message itself.
    // url should be the file presenting the error, though i have
    //    found that it only presents to me the address of the site.
    // lineNumber is the line number the error occoured on.
    // here is an example of what you could do with it:
    alert("Error in " + url + " at " + lineNumber + ":\n" + errorMsg);
}

But that only shows error messages.但这仅显示错误消息。 It will not show any warning messages.它不会显示任何警告消息。

Is there a similar way to detect a specific warning message in the console.log?是否有类似的方法来检测 console.log 中的特定警告消息? Or, alternately, is there some JS code that will allow me to search the console.log for a specific string?或者,是否有一些 JS 代码可以让我在 console.log 中搜索特定字符串?

Note that I can see the warning message in the browser's debugging (console logging) window.请注意,我可以在浏览器的调试(控制台日志记录)窗口中看到警告消息。 But I want to 'intercept' the warning message and perform some action if that warning is found.但是我想“拦截”警告消息并在发现该警告时执行一些操作。

Added添加

The intent is to find out when a specific warning message occurs;目的是找出特定警告消息何时发生; the message being similar to "Loading failed for the <script> with source "https://www.example.com/js/somescript.js" .该消息类似于"Loading failed for the <script> with source "https://www.example.com/js/somescript.js" .

But the question of how to intercept a warning message (not error; the code I included does that) that is output by the browser (not my code).但是如何拦截浏览器输出的警告消息(不是错误;我包含的代码就是这样做的)的问题(不是我的代码)。

Added添加

So, here's what I need.所以,这就是我需要的。 Assume this code假设这个代码

console.warn("Here is a warning!");

What code would you write to see if that message was written to the console as a warning?您会编写什么代码来查看该消息是否作为警告写入控制台?

How about overriding the console.log() ?覆盖console.log()怎么样?

 let tmpConsoleLog = console.log; console.log = function(msg){ // Intercept code goes here using msg variable alert(msg); // then perform the normal logging tmpConsoleLog(msg); } console.log("Something");

The warnings you are talking about are generated by the browser, not console.warn .您正在谈论的警告是由浏览器生成的,而不是console.warn This is why you cannot override it.这就是您不能覆盖它的原因。 Most likely, you need to manually add listeners for each event you need.很可能,您需要为您需要的每个事件手动添加侦听器。 For example, if you want to handle a script loading error use the onerror event:例如,如果要处理脚本加载错误,请使用onerror事件:

 <script src="https://www.example.com/js/somescript1.js" onerror="console.log('Cannot load script from ' + this.src)"></script> <script src="https://www.example.com/js/somescript2.js" onerror="console.log('Cannot load script from ' + this.src)"></script>

It's very late, but here's an answer that actually directly answers your question.已经很晚了,但这里有一个答案,实际上直接回答了您的问题。

 var originalConsoleWarn = console.warn; console.warn = function(message) { doSomethingWithWarn(message); originalConsoleWarn(message); }; function doSomethingWithWarn(message){ console.log("DOING SOMETHING WITH: " + message); } console.warn("hi");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM