最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to break on warnings in Google Chrome? - Stack Overflow

programmeradmin4浏览0评论

I want to see the warnings in order to do a better debugging of my application which is made in Asp.Net, so it has a lot of code generated in javascript. The problem is that pages reload fast and I can't take a look at the warning messages. I want to know if there is a way to break on warnings in Google Chrome, as in this question is described how to break on errors. Is there a way to do this, or what alternatives do I have?

I want to see the warnings in order to do a better debugging of my application which is made in Asp.Net, so it has a lot of code generated in javascript. The problem is that pages reload fast and I can't take a look at the warning messages. I want to know if there is a way to break on warnings in Google Chrome, as in this question is described how to break on errors. Is there a way to do this, or what alternatives do I have?

Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Jun 24, 2016 at 14:39 meJustAndrewmeJustAndrew 6,64310 gold badges56 silver badges87 bronze badges 6
  • 1 What kind of warnings? You could hook console.warn and set a breakpoint. – SLaks Commented Jun 24, 2016 at 14:40
  • Well, the warnings could be from anywhere, as it is a code generated from asp, I forgot to mention this is the question, so I don't know where to set a breakpoint, and in the console, because the page is just fast refresh on the actions on buttons, I don't have time to see the warnings messages. – meJustAndrew Commented Jun 24, 2016 at 14:43
  • here is some information on how to debug in chrome developer.chrome./devtools/docs/… – erickeno Commented Jun 24, 2016 at 14:47
  • @erickeno this was also in the first answer of the question I've added linq to. – meJustAndrew Commented Jun 24, 2016 at 14:48
  • okay, I didn't click on the link. There are so many ways to debug you code in Chrome, you can even set manual breakpoints at specific places in your code and see the values. – erickeno Commented Jun 24, 2016 at 14:51
 |  Show 1 more ment

2 Answers 2

Reset to default 8

As @Nitsew has already said, preserving the log is a great solution. But just for fun, and because I thought your question was interesting, I decided to work out a solution to do what you asked; cause a debug to occur when warn is used:

function testBreak() {
    console.warn('This is a warning', 1, 2);
    return 1;
}

var oldWarningFunction = console.warn;
console.warn = function () {
    debugger;
    oldWarningFunction.apply(console, arguments);
};

console.log(testBreak());

Essentially, I have monkey-patched the original console.warn to output a debugger statement. When this causes the debug to occur, you will probably need to view the stack trace to see where you were in the code:

Once you click back one on that stack trace, you can see the function that made the call.

This all seems rather silly since you could just write your own debugger statement right in your code where you want them. And one other thing, to whoever may read this and tries it: Do not leave this in your production code

If you are just trying to look at the console messages, turning on "Preserve log" might be an easier solution.

发布评论

评论列表(0)

  1. 暂无评论