android logcat日志保存到文件中

本文介绍如何在Android应用中获取权限并保存logcat日志到本地文件,便于日志分析和问题排查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.android.write;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SaveLog extends Thread {

    private String shell = "logcat";
    private String logpath = "/mnt/sdcard2/logcat.txt";
    private BufferedReader mReader;
    private Process process = null;
    private FileOutputStream fos = null;
    private InputStream inputStream= null;
    private boolean mRunning = true;
    private BufferedWriter bw = null;
    private File file = null;
    
    public SaveLog() {
        file = new File(logpath);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file, true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void run() {
        try {
            
            process = Runtime.getRuntime().exec(shell);
            inputStream = process.getInputStream();
            mReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = mReader.readLine()) != null){
                file = new File(logpath);
                if (!file.exists()) {
                    file.createNewFile();
                    fos = new FileOutputStream(file, true);
                }
                if (file.length() > 1 * 1024 * 1024) {
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write("".getBytes());
                    fos.close();
                    clearLogCache();
                }
                if (line.length() == 0) {
                    continue;
                }
                if (fos != null) {
                    fos.write((line + "\n").getBytes());
                }
                
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
    }

    
      private void clearLogCache() {
          Process proc = null;
          List<String> commandList = new ArrayList<String>();
          commandList.add("logcat");
          commandList.add("-c");
          //commandList.add("*:E");// 过滤所有的错误信息
          try {
            proc = Runtime.getRuntime().exec(
                      commandList.toArray(new String[commandList.size()]));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          
      }

}


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值