HTC Android 设备和 adb logcat 上的 Javascript console.log()

发布于 2024-10-30 17:36:46 字数 1092 浏览 1 评论 0原文

我正在开发 HTML 中的应用程序,该应用程序从 Javascript 调用 console.log() 来在开发过程中向我提供有关网页代码中发生的情况的日志。

不幸的是,当我使用 adb logcat 命令检查日志时,我可以看到所有其他应用程序的输出,但看不到 JavaScript 代码的输出。我什至可以从网络浏览器中看到页面已加载的日志,但看不到在网络浏​​览器中执行的 JavaScript 代码的 console.log() 输出。

根据此页面 (http://developer.android.com/guide/webapps/debugging.html)它应该可以工作。

我正在 HTC WildFireHTC Desire HD 上进行测试。


超过 6 个月后编辑

经过一段时间和使用不同设备(手机、电视、机顶盒、WebViews、UIWebViews...)的经验后,我的建议是进行远程日志记录 来自 JavaScript,而不依赖于 console.log() 或其他方法 - 查看图像加载的好技巧 此处

不要错过此处的演示 希望这有帮助! 斯坦恩

I am developing the application in HTML which is calling the console.log() from Javascript to provide me logs during the development about what happens in the web page code.

Unfortunately when I use the adb logcat command to check logs I can see output from all other applications, but not the output from my JavaScript code. I can see even the log from web browser that the page is loaded, but not console.log() output from my JavaScript code executed in the web browser.

According to information on this page (http://developer.android.com/guide/webapps/debugging.html) it should work.

I am testing on HTC WildFire and HTC Desire HD.


Edited after more then 6 months

After some time and experience with different devices (phones, TVs, set top boxes, WebViews, UIWebViews...) my advice is to do the remote logging from JavaScript and not relying on the console.log() or other methods - see the nice trick with the image loading here.

Do not miss the presentation here
Hope this helps!
STeN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回眸一遍 2024-11-06 17:36:47

在 Android 2.3.3(大概从那时起)的默认浏览器中,您可以简单地使用内置的 javascript 控制台:

  • 打开浏览器,
  • 转到您的网页,
  • 在地址栏中输入 about:debug 并点击输入
  • 如果您进入浏览器应用程序的设置,您将看到调试选项部分
  • 如果尚未启用,请勾选“显示 JavaScript 控制台”
  • 刷新您的网页

在顶部,您将看到一个标有“JavaScript 控制台”的栏。

In the default browser on Android 2.3.3 (and presumably from then on) you can simply use the built in javascript console:

  • open the browser
  • go to your webpage
  • type about:debug in the address bar and hit enter
  • you'll see a section for debug options appear if you go into the browser app's settings
  • tick "Show JavaScript Console" if not already enabled
  • refresh your webpage

At the top, you'll see a bar labeled "JavaScript Console".

夜未央樱花落 2024-11-06 17:36:47

我几乎只使用过三款不同的 HTC 手机,从未遇到过此问题。以下是一些需要检查的事项:

  1. 确保 USB 调试已启用。
  2. 确保您的 Webview 设置了 WebChromeClient。 Webview中使用的浏览器没有实现console.log()。
  3. 应该很容易看到 adb logcat 的输出,但为了更容易,请过滤输出。

打开 USB 调试

  1. 断开设备与计算机的连接。
  2. 进入设置->应用->发展->选择
  3. 计算机上的“启用 USB 调试”插件。 (确保您安装了正确的驱动程序以使用 ADB - 更多信息请参见:http:// developer.android.com/guide/developing/device.html)

设置一个覆盖 onConsoleMessage() 的 WebChromeClient:

//Set the output prefix so you can filter adb logcat results later
public static final String TAG = "Your-Application-Name";

myWebView = (WebView) findViewById(R.id.webview);

//Not going to have much luck running JS without this:
myWebView.getSettings().setJavaScriptEnabled(true);

//Override onConsoleMessage() to output to the Log.
myWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        Log.d(TAG, cm.message() + " -- From line "
        + cm.lineNumber() + " of "
        + cm.sourceId() );
        return true;
    }
});

有关 onConsoleMessage() 的更多信息,请参见:http://developer.android.com/reference/android/webkit /WebChromeClient.html#onConsoleMessage(java.lang.String, int, java.lang.String)

有关调试的更多信息:http://developer.android.com/guide/webapps/debugging.html

过滤 adb logcat 的输出:

adb logcat tag-name:log-level *:S

tag-name与 Log.x 中指定的字符串匹配
log-level 与您在调用 Log.x 时指定的日志级别相匹配 <---

与上述代码相关的示例:

adb logcat Your-Application-Name:D *:S

这将显示匹配标签、Your-Application-Name 的所有 d 级别日志,并静默其他所有内容。

有关 adb 过滤的更多信息,请参见此处:
http://developer.android.com/guide/developing/tools/adb .html#logcat

希望有帮助!我知道这是对其他答案的一些总结,但事实是,它需要所有步骤才能使其发挥作用。 :)

I have been using three different HTC phones, almost exclusively, and have never had this issue. Here are a few things to check:

  1. Make sure USB debugging is enabled.
  2. Make sure your Webview has a WebChromeClient set. The browser used in Webview does not implement console.log().
  3. It should be easy to see the output of adb logcat, but to make it easier, filter the output.

Turn on USB debugging:

  1. Disconnect your device from your computer.
  2. Go to Settings -> Applications -> Development -> Select "Enable USB Debugging"
  3. Plugin to computer. (Make sure you have the correct drivers installed to use ADB - more info here: http://developer.android.com/guide/developing/device.html)

Set a WebChromeClient that overrides onConsoleMessage():

//Set the output prefix so you can filter adb logcat results later
public static final String TAG = "Your-Application-Name";

myWebView = (WebView) findViewById(R.id.webview);

//Not going to have much luck running JS without this:
myWebView.getSettings().setJavaScriptEnabled(true);

//Override onConsoleMessage() to output to the Log.
myWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        Log.d(TAG, cm.message() + " -- From line "
        + cm.lineNumber() + " of "
        + cm.sourceId() );
        return true;
    }
});

More info on onConsoleMessage() here: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onConsoleMessage(java.lang.String, int, java.lang.String)

More info on debugging in general here: http://developer.android.com/guide/webapps/debugging.html

Filter the output of adb logcat:

adb logcat tag-name:log-level *:S

tag-name matches the string specified in Log.x
log-level matches the log level you indicated when calling Log.x <---

Example relating to code above:

adb logcat Your-Application-Name:D *:S

This will show all d level logs for the matching tag, Your-Application-Name, and silence everything else.

More info on adb filtering here:
http://developer.android.com/guide/developing/tools/adb.html#logcat

Hope it helps! I know it was a bit of summarizing of the other answers, but, the fact is, it takes all of the steps to make it work. :)

同展鸳鸯锦 2024-11-06 17:36:47

我遇到了同样的问题,我通过执行以下操作解决了它:

1/ 当您初始化 webview 时,添加一个 javascript 桥接类:

appView.addJavascriptInterface(new JSBridge(), "JSBridge");

2/ 创建带有日志函数的 JSBridge 类

class JSBridge {
  public void log(String msg){
   Log.d(msg);
  }
}

3/ 在您的 javascript 中您现在可以调用

JSBridge.log("my log message");

可能是对于你的问题来说有点矫枉过正,但你也可以用这个解决方案做更多的事情

I had the same problem, I solve it by doing the following:

1/ when you initialize your webview, add a javascript bridge class:

appView.addJavascriptInterface(new JSBridge(), "JSBridge");

2/ create the class JSBridge with a log function

class JSBridge {
  public void log(String msg){
   Log.d(msg);
  }
}

3/ in your javascript you can now call

JSBridge.log("my log message");

Might be a bit overkill for your problem, but you can also do a LOT more with that solution

难理解 2024-11-06 17:36:47

除了您的设备之外,Logcat 还会在设备上生成很多内容,因此您可能必须对其进行过滤。

如果您已标记日志输出,则可以尝试“grep”它。
例如:根据您的文章,输出应如下所示:

Console: Hello World http://www.example.com/hello.html :82

如果您使用该命令(假设您使用的是 Linux、Mac OS 或任何带有 grep 命令的其他操作系统),

adb logcat | grep -i "console"

它会将输出减少为这些引号内指定的关键字。如果您在输出中添加唯一标签,您还可以对其进行过滤,以便只获得您想要查看的内容。

[ctrl]+c

将停止此 logcat 进程。

Logcat generates a lot of stuff on devices in addition to yours so you may have to filter this.

You can try to "grep" your log output if you've tagged it.
For example: acording to your article the output should look like:

Console: Hello World http://www.example.com/hello.html :82

If you use the command (assuming you are using a Linux, Mac OS or anything else with a grep command)

adb logcat | grep -i "console"

it will reduce the output to the keyword specified within these quotation marks. If you add a unique tag to your output you can also filter this so you get only things you want to see.

[ctrl]+c

will stop this logcat process.

∝单色的世界 2024-11-06 17:36:47

请参阅:如何使用 Eclipse 和 HTC Desire HD 在 PhoneGap 应用程序中显示 console.log() 输出?

console.log 似乎被禁用了
在运行 Android 2.2 的 HTC 设备上。

您可以通过使用 远程调试 来解决这个问题.com" rel="nofollow noreferrer">jsconsole.com。

See this: How to display console.log() output in PhoneGap app using Eclipse and HTC Desire HD?

It seems that console.log is disabled
on HTC devices running Android 2.2.

You can get around it by using remote debugging in jsconsole.com.

怎会甘心 2024-11-06 17:36:47

请尝试以下操作:

1) 打开设备选项卡并确保已选择/突出显示您已连接的设备。

2) 确保您的设备上启用了 USB 调试。为此,您需要执行以下操作:

2a) 在主屏幕上,按 MENU,然后点击设置 >应用领域>>发展。

2b) 确保选中 USB 调试复选框。

Try the following:

1) Open the devices tab and make sure that the device you have connected is selected/highlighted.

2) Make sure USB Debugging is enabled on your device. Here is what you need to do for this:

2a) From the Home screen, press MENU, and then tap Settings > Applications > Development.

2b) Make sure the USB debugging check box is selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文