5

I want to intercept console log message from AngularJS and display them in a div on the page. I need this in order to debug ajax traffic in a PhoneGap app.

This is an example of the kind of errors I want to capture:

enter image description here

I tried this Showing console errors and alerts in a div inside the page but that does not intercept Angular error messages.

I also tried the solution gameover suggested in the answers. No luck with that either. Apparently $http is handling error logging differently.

4 Answers 4

1

I guess the answer you tried has the right idea but you're overriding the wrong methods. Reading here I can see angularJs uses $log instead of console.log, so to intercept you can try to override those.

Something like this:

$scope.$log = {
        error: function(msg){document.getElementById("logger").innerHTML(msg)},
        info: function(msg){document.getElementById("logger").innerHTML(msg)},
        log: function(msg){document.getElementById("logger").innerHTML(msg)},
        warn: function(msg){document.getElementById("logger").innerHTML(msg)}
    }

Make sure to run that after importing angular.js.

EDIT

Second guess, override the consoleLog method on the LogProvider inner class on angular.js file:

function consoleLog(type) {
  var output ="";
  //arguments array, you'll need to change this accordingly if you want to
  //log arrays, objects etc                       
  forEach(arguments, function(arg) {
    output+= arg +" ";
  });  
  document.getElementById("logger").innerHTML(output);             
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I read that too. But the trouble is in the "Something like". I tried injecting $log in the .run method of the app. But it is not working.
Spent a little more time on the js file, this should do it!
Looks promising! I managed to make it work by injecting $log and overriding error. However: the errors $http writes to the console (see screenshot) are not intercepted this way.
It's pretty late but i believe this can be solved by using a decorator to extend $log.
0

I've used log4javascript for this purpose. I create the log object via

var log = log4javascript.getLogger('myApp')
log.addAppender(new log4javascript.InPageAppender());

I then use this in a value dependency, and hook into it where needed (e.g. http interceptor).

A more lightweight approach might be to use $rootScope.emit and then have a component on your main page which prepends these log messages to a visible div, but this will require you to change all your calls to console.log (or redefine the function in your js).

1 Comment

This looks like a great solution for your own logging requirements. But that does not answer my question. I want to capture the error message AngularJS itself dumps to console.log.
0

I think that this message is not even displayed from AngularJS. It looks like an exception which has not been caught in any JavaScript (angular.js just appears on top of your stack because that's the actual location where the HTTP request is being sent).

Take a look at ng.$exceptionHandler. That should be the code you seem to be interested in. If not, take a quick web search for „JavaScript onerror“ which should tell you how to watch for these kinds of errors.

Comments

0

I would rather user an $http interceptor. Inside the responseError function, you can set a property on a service that will be exposed to the div's scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.