8

My app is a combination of Android native, html5. Till last week I'm able to see the log messages from native code and javascript code running inside th WebView. But suddenly Logcat is not showing the console messages from javascript, though it is showing Log messages from my native code. Any ideas?

Thanks in advance. Venkat

Venkat Papana
  • 4,757
  • 13
  • 52
  • 74

5 Answers5

22

You can try adding a console message handler to your WebView by creating your custom WebChromeClient:

class MyWebChromeClient extends WebChromeClient
{
    @Override
    public boolean onConsoleMessage(ConsoleMessage cm)
    {
        Log.d("CONTENT", String.format("%s @ %d: %s", 
                    cm.message(), cm.lineNumber(), cm.sourceId()));
        return true;
    }
}

And then attach it:

someWebView.setWebChromeClient(new MyWebChromeClient());
3k-
  • 2,467
  • 2
  • 23
  • 24
18

For just android native & html5 (not phonegap/cordova) this covers how to display console messages in logcat. http://developer.android.com/guide/webapps/debugging.html#WebView

An alternative is to use jsconsole.com. Remote debugging is covered at http://jsconsole.com/remote-debugging.html

Edit: Some have found jsconsole.com doesn't work. Another alternative for remote debugging is https://bugfender.com/remote-logging Disclaimer: non-affiliate link and I have no relationship with bugfender other than a user. There is a free plan and also various paid plans with higher usage levels.

Thaddeus Albers
  • 4,094
  • 5
  • 32
  • 42
  • 1
    Wow! Good find dude! jsconsole.com is AMAZING. – Dave Mackintosh May 01 '13 at 13:34
  • 1
    implementing `onConsoleMessage()` worked out for me, see http://developer.android.com/guide/webapps/debugging.html#Browser – Someone Somewhere Feb 19 '14 at 01:48
  • I agree with Dave that jsconsole.com is freakin cool! not only does it give you a way to see your console.log messages but also shows javascript errors with full file path and line numbers. wow! ThankS – Logic1 Jan 25 '15 at 23:27
  • The listen feature on jsconsole.com no longer works, rendering it useless for solving this problem. – NeonD Aug 29 '18 at 16:32
1

I am running PhoneGap 2.9.1.

After doing some searching I was expecting the Console.log to show in the Eclipse LogCat under the tag 'WebView', so I wasn't sure it was working.

After some playing around I found that console.log() does show in the LogCat under the tag 'CordovaLog'.

ScottyG
  • 3,204
  • 3
  • 32
  • 42
1

This command works too nice

adb shell "logcat | grep 'Web Console'"

I found the answer here

Community
  • 1
  • 1
Arnold Gandarillas
  • 3,896
  • 1
  • 30
  • 36
0

If you want to test your cordova App, within your browser, just like you test any other web app.

You can use: cordova serve . This will run the hybrid app locally. You can open the url http://localhost:8000, and can inspect element for console logs. You can change the DOM elements, CSS.