开发者

Android Logging

开发者 https://www.devze.com 2023-02-13 09:11 出处:网络
I need to log exceptions in my android application. Is there way to log exception so, that i can di开发者_Python百科agrammatically read this logs and send it to server or somethig like that?I can see

I need to log exceptions in my android application. Is there way to log exception so, that i can di开发者_Python百科agrammatically 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.

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.

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: 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?

Instead of using Android logging class, android-logging-log4j can be used to log exception stack traces.

Logging exception stack traces in Android using slf4j api

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

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
        }
    }
}


Try to use Android Logger - it is the easiest implementation of SLF4J API:

android-logger.properties:

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

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:

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

精彩评论

暂无评论...
验证码 换一张
取 消