简体   繁体   中英

Android Logging

I need to log exceptions in my android application. 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.

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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