Android 如何读取我的应用程序日志并写入文件?

Android 如何读取我的应用程序日志并写入文件?,android,logging,logcat,bug-reporting,Android,Logging,Logcat,Bug Reporting,我想在手机上读取我的应用程序日志,然后将其写入文件。 我可以在eclipse中查看和检查我的日志,如下所示: 12-26 11:53:07.456: I/power(188): *** set_screen_state 0 12-26 11:53:07.496: D/Sensors(188): Enable G-Sensor: en = 0 12-26 11:53:07.506: D/Sensors(188): mEnabled = 0x0 12-26 11:53:07.506: D/Senso

我想在手机上读取我的应用程序日志,然后将其写入文件。 我可以在eclipse中查看和检查我的日志,如下所示:

12-26 11:53:07.456: I/power(188): *** set_screen_state 0
12-26 11:53:07.496: D/Sensors(188): Enable G-Sensor: en = 0
12-26 11:53:07.506: D/Sensors(188): mEnabled = 0x0
12-26 11:53:07.506: D/Sensors(188): /data/misc/AccPrmsF.ini does not exist
12-26 11:53:07.596: D/AccelerometerListener(340): enable(false)
12-26 11:53:07.686: V/TransportControlView(188): Create TCV com.android.internal.widget.TransportControlView@412f16a8
12-26 11:53:07.957: D/dalvikvm(188): GC_CONCURRENT freed 1852K, 52% free 11229K/23367K, paused 15ms+23ms
12-26 11:53:07.967: D/SurfaceFlinger(125): About to give-up screen, flinger = 0x15a1918
12-26 11:53:08.187: I/ActivityManager(188): Start proc com.google.android.gsf.login for service com.google.android.gsf.login/com.google.android.gsf.loginservice.GoogleLoginService: pid=25736 uid=10005 gids={3003, 1015, 1007, 2001, 3006}
12-26 11:53:08.207: D/PhoneStatusBar(27013): disable: < expand icons alerts ticker system_info back home RECENT* clock >
12-26 11:53:08.277: W/IInputConnectionWrapper(188): showStatusIcon on inactive InputConnection
12-26 11:53:08.297: D/PhoneStatusBar(27013): disable: < expand icons alerts ticker system_info BACK* HOME* RECENT CLOCK* >
12-26 11:54:16.604: W/ThrottleService(188): unable to find stats for iface rmnet0
12-26 12:00:24.983: D/dalvikvm(188): GC_EXPLICIT freed 1532K, 51% free 11674K/23367K, paused 6ms+22ms
12-26 11:53:07.456:I/电源(188):***设置屏幕状态0
12-26 11:53:07.496:D/传感器(188):启用G传感器:en=0
12-26 11:53:07.506:D/传感器(188):mEnabled=0x0
12-26 11:53:07.506:D/传感器(188):/data/misc/AccPrmsF.ini不存在
12-26 11:53:07.596:D/加速度计指示器(340):启用(错误)
12-26 11:53:07.686:V/TransportControlView(188):创建TCV com.android.internal.widget。TransportControlView@412f16a8
12-26 11:53:07.957:D/dalvikvm(188):GC_并发释放1852K,52%释放11229K/23367K,暂停15毫秒+23毫秒
12-26 11:53:07.967:D/SurfaceFlinger(125):即将放弃屏幕,flinger=0x15a1918
12-26 11:53:08.187:I/ActivityManager(188):启动proc com.google.android.gsf.login for service com.google.android.gsf.login/com.google.android.gsf.login服务:pid=25736 uid=10005 gids={3003、1015、1007、2001、3006}
12-26 11:53:08.207:D/PhoneStatusBar(27013):禁用:
12-26 11:53:08.277:W/IIInputConnectionWrapper(188):在非活动输入连接上显示状态图标
12-26 11:53:08.297:D/PhoneStatusBar(27013):禁用:
12-26 11:54:16.604:W/ThrottleService(188):无法找到iface rmnet0的统计信息
12-26 12:00:24.983:D/dalvikvm(188):GC_显式释放1532K,51%释放11674K/23367K,暂停6ms+22ms
但我想在我的手机上读一下。
我怎么能这么做

从应用程序将日志写入logcat非常简单。 读取日志通常用于错误报告目的

以下代码读取日志并将其写入文件:

public class LogTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
            }

            String logFilePath = Environment.getExternalStorageDirectory() + File.separator + "LogTest.txt";

            File logFile = new File(logFilePath);
            if (!logFile.exists())
                logFile.createNewFile();

            FileOutputStream outStream = new FileOutputStream(logFile, true);
            byte[] buffer = log.toString().getBytes();

            outStream.write(buffer);
            outStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }
}
您还需要两个权限才能读取日志并将文件写入外部存储:

<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

您可以向logcat命令字符串添加更多参数。中的更多信息

在实现bug报告功能时,通过向自己发送日志,不要忘记许多应用程序都会将个人信息输出到logcat中,因此肯定需要一个关于它们的策略


我希望这篇文章有用;-)

从应用程序将日志写入logcat非常简单。 读取日志通常用于错误报告目的

以下代码读取日志并将其写入文件:

public class LogTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
            }

            String logFilePath = Environment.getExternalStorageDirectory() + File.separator + "LogTest.txt";

            File logFile = new File(logFilePath);
            if (!logFile.exists())
                logFile.createNewFile();

            FileOutputStream outStream = new FileOutputStream(logFile, true);
            byte[] buffer = log.toString().getBytes();

            outStream.write(buffer);
            outStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }
}
您还需要两个权限才能读取日志并将文件写入外部存储:

<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

您可以向logcat命令字符串添加更多参数。中的更多信息

在实现bug报告功能时,通过向自己发送日志,不要忘记许多应用程序都会将个人信息输出到logcat中,因此肯定需要一个关于它们的策略

我希望这篇文章有用;-)