Logging to a file on Android

后端 未结 6 551
离开以前
离开以前 2020-12-01 11:09

Is there any way of retrieving log messages from an Android handset.

I\'m building an application which uses the GPS of my HTC Hero. I can run and debug the applicat

相关标签:
6条回答
  • 2020-12-01 11:15

    Use slf4-android lib created by BrightInventions. It's simple implementation of slf4j api using android java.util.logging.*.

    Features:

    • logging to file out of the box
    • logging to any other destination by LoggerConfiguration.configuration().addHandlerToLogger
    • shake your device to send logs with screenshot via email
    • really small, it tooks only ~55kB

    slf4android is maintained mainly by @miensol.

    Read more about slf4android on our blog:

    • Introducing slf4android
    • Integrate slf4android with Crashlytics
    0 讨论(0)
  • 2020-12-01 11:16

    Based on @Greg B solution and @JPM question in the comment, I made this static method in a seperate class and each time you instanciate a new thread, call it.

    package com.stackoverflow.

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import android.os.Environment;
    
    public class UnhandledExceptionHandler {
    
        public static void setUndhandledException(Thread thread) {
            if (thread.getUncaughtExceptionHandler() == null) {
                Thread.currentThread().setUncaughtExceptionHandler(
                        new Thread.UncaughtExceptionHandler() {
                            @Override
                            public void uncaughtException(Thread thread,
                                    Throwable ex) {
    
                                PrintWriter pw;
                                try {
                                    pw = new PrintWriter(new FileWriter(Environment
                                            .getExternalStorageDirectory()
                                            + "/rt-networkandgps"
                                            + "/gpx/"
                                            + thread.getId() + ".log", true));
                                    ex.printStackTrace(pw);
                                    pw.flush();
                                    pw.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
            }
    
        }
    
    }
    

    Call it from anythread like this,

                UnhandledExceptionHandler.setUndhandledException(Thread.currentThread());
    
    0 讨论(0)
  • 2020-12-01 11:21

    I tried both options above (microlog4android & android-logging-log4j) and they were a struggle to try to get working. Once they compiled fine then I would get Runtime java.lang.NoClassDefFoundError.

    So then I stumbled on logback-android ... also found here http://logback.qos.ch/

    It has much better instructions and I got it working in a couple hours.

    This appears to be the best logging solution for Android.

    0 讨论(0)
  • You could use Thread.setUncaughtExceptionHandler() to catch the Exceptions.

    Writing to SD Card is as simple as retrieving the directory for the card using Environment.getExternalStorageDirectory() and creating a file there.

    File f = new File(Environment.getExternalStorageDirectory(),filename);
    

    You will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2020-12-01 11:31

    Log4j or slf4j can also be used as logging frameworks in Android together with logcat. See the project android-logging-log4j. Configuring logging to a (rotating) file(s) is very easy.

     static {
        final LogConfigurator logConfigurator = new LogConfigurator();
    
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "myapp.log");
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();
    }
    
    0 讨论(0)
  • 2020-12-01 11:33

    You could take a look at microlog4android. They have a solution ready to log to a file.

    https://github.com/johanlkarlsson/microlog4android

    0 讨论(0)
提交回复
热议问题