-5

I would like to send back the latest errors/log statements from the console as part of a support-request. I do I retrieve the console through javascript/jquery?

  • Use try.... catch – Kenny May 10 '17 at 06:15
  • 1
    Also, you could override `console` and have it submit calls via AJAX – Phil May 10 '17 at 06:16
  • Try this! => http://stackoverflow.com/questions/20253652/how-do-you-send-console-messages-and-errors-to-alert – David R May 10 '17 at 06:17
  • 1
    Possible duplicate of [Catch all javascript errors and send them to server](http://stackoverflow.com/questions/5328154/catch-all-javascript-errors-and-send-them-to-server) – Rajesh May 10 '17 at 06:18
  • 1
    @Rajesh OP says *"log statements"* too so just catching errors wouldn't be enough – Phil May 10 '17 at 06:19
  • My bad! I'll retract my vote – Rajesh May 10 '17 at 06:20
  • @Rajesh I'd leave the link though, it's a good one. This question is really too broad – Phil May 10 '17 at 06:20
  • 1
    I have actually answered similar question overriding all console functions but its lost. Digging through answers. Will share if found – Rajesh May 10 '17 at 06:22
  • 1
    @Phil This will help. Didn't find related answer but found fiddle link. **[JSFiddle](https://jsfiddle.net/2pwrth00/2/)** – Rajesh May 10 '17 at 06:33

2 Answers2

0

You can also override the console.log

(function(){
    if(window.console && console.log){
        var old = console.log;
        console.log = function(){
            doSomthingElse(arguments); 
            old.apply(this, arguments)
        }
    }  
})();
ToujouAya
  • 593
  • 3
  • 24
  • 2
    What about all the other [`console` methods](https://developer.mozilla.org/en/docs/Web/API/console#Methods)? – Phil May 10 '17 at 06:18
  • You need to do it one by one. Here is fully article about override `console` methods http://ourcodeworld.com/articles/read/104/how-to-override-the-console-methods-in-javascript – ToujouAya May 10 '17 at 06:21
0

You can use try... catch

See this example https://jsfiddle.net/j3tfLukr/

var a = {}

try {
   var b = a.b.c
} catch(e) {
  alert(e);
}
Kenny
  • 5,407
  • 4
  • 19
  • 37
  • 2
    This will cover only that part of code that is written by you – Rajesh May 10 '17 at 06:19
  • To clarify, your code will not cover issues like `$ in undefined` which happens because of delayed loading or accessing `$` before loading jQuery – Rajesh May 10 '17 at 06:30
  • @Rajesh - I would also like to cover issues like $ is undefined as some parts of the application is quite old. Would that be possible? I can see it in the developer console and now I would "just" like to retrieve that :-) – user1697073 May 11 '17 at 06:37
  • @user1697073 for errors, I would suggest using sentry, as mentioned in duplicate I shared under question. If you want to handle manually, I have also shared a JSFiddle link that does something similar, but make sure this script loads before anything else – Rajesh May 11 '17 at 06:40