简体   繁体   English

Android日志

[英]Android Logging

I need to log exceptions in my android application. 我需要在Android应用程序中记录异常。 Is there way to log exception so, that i can diagrammatically read this logs and send it to server or somethig like that? 有没有记录异常的方法,这样我就可以以图形方式读取此日志并将其发送到服务器或类似的东西?

I can see a few different situations here: 我可以在这里看到几种不同的情况:

A: If you're talking about the development process, exceptions can be viewed in LogCat (for example, in the Debug perspective) by clicking on the Error filter. 答:如果您在谈论开发过程,则可以通过单击“错误”筛选器在LogCat中查看异常(例如,在“调试”透视图中)。

B: If you're talking about crashes in production apps, stack traces are reported to Google and can be viewed in the Android Market Developer Console. B:如果您谈论的是生产应用程序中的崩溃,则堆栈跟踪会报告给Google,并可以在Android Market开发者控制台中查看。

C: Otherwise, if you want to log and submit an exception that you are catching (and therefore not allowing to crash the activity), then check out the logging class in How do I obtain crash-data from my Android application? C:否则,如果要记录并提交正在捕获的异常(因此不允许使活动崩溃),请查看如何从Android应用程序获取崩溃数据中的日志记录类

Try to use Android Logger - it is the easiest implementation of SLF4J API: 尝试使用Android Logger-这是SLF4J API的最简单实现:

android-logger.properties: android-logger.properties:

root=ERROR:MyApplication
logger.com.example.ui=DEBUG:MyApplication-UI

MainActivity.java: MainActivity.java:

package com.example.ui;

import com.noveogroup.android.log.Logger;
import com.noveogroup.android.log.LoggerManager;

public class MainActivity extends Activity {

  private static final Logger logger = LoggerManager.getLogger();

  private void foo(int value) {
    logger.i("entered MainActivity::foo value=%d", value);

    try {
      // some code
    } catch(IOException e) {
      logger.e("I/O error occurred", e);
    }
  }

}

LogCat Output: LogCat输出:

I/MyApplication-UI: entered MainActivity::foo value=10
E/MyApplication-UI: I/O error occurred

C: Otherwise, if you want to log and submit an exception that you are catching (and therefore not allowing to crash the activity), then check out the logging class in How do I obtain crash-data from my Android application? C:否则,如果要记录并提交正在捕获的异常(因此不允许使活动崩溃),请查看如何从Android应用程序获取崩溃数据中的日志记录类?

Instead of using Android logging class, android-logging-log4j can be used to log exception stack traces. 代替使用Android日志记录类,可以使用android-logging-log4j记录异常堆栈跟踪。

Logging exception stack traces in Android using slf4j api 使用SLF4J API在Android中记录异常堆栈跟踪

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExampleLog4JOverSLF4J {
    private final Logger log = LoggerFactory.getLogger(ExampleLog4JOverSLF4J.class);

    public void myMethod() {
        try {
            // invoke code throwing an exception
        }
        catch(Exception e) {
            log.error("An error occured...", e);
            // recover or what ever
        }
    }
}

Logging exception stack traces in Android using log4j api 使用log4j API在Android中记录异常堆栈跟踪

import org.apache.log4j.Logger;

public class ExampleLog4J {
    private final Logger log = Logger.getLogger(LogConfiguratorTest.class);

    public void myMethod() {
        try {
            // invoke code throwing an exception
        }
        catch(Exception e) {
            log.error("An error occured...", e);
            // recover or what ever
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM