收集所有Log.d从类,并保存到SD卡 [英] Collect all Log.d from class and save it into SDCard

查看:221
本文介绍了收集所有Log.d从类,并保存到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿是否有可能在Android的收集从一个类的所有Log.d,不断追加并将其保存到SD卡?

例如:

 类的Andr​​oid {

    私人最终静态字符串变量=你好;

    公共无效ABCD(){
    Log.d(TAG,它进入ABCD法);
    }

    公共无效EFGH(){
    Log.d(TAG,它走进EFGH法);
    }

       / *这里收集上述所有日志和写入SD卡*提交/

}
 

解决方案

您可以使用的logcat 命令所有日志。您可以尝试这样的:

 公共静态无效PRINTLOG(上下文的背景下){
    字符串文件名= context.getExternalFilesDir(空).getPath()+文件分割符+my_app.log;
    字符串命令=logcat的-d *:V;

    Log.d(TAG,命令:+指令);

    尝试{
        。工艺过程=调用Runtime.getRuntime()EXEC(命令);

        的BufferedReader在=新的BufferedReader(新的InputStreamReader(process.getInputStream()));
        串线= NULL;
        尝试{
            档案文件=新的文件(文件名);
            file.createNewFile();
            FileWriter的作家=新的FileWriter(文件);
            而((行= in.readLine())!= NULL){
                writer.write(行+\ N);
            }
            writer.flush();
            writer.close();
        }
        赶上(IOException异常E){
            e.printStackTrace();
        }
    }
    赶上(IOException异常E){
        e.printStackTrace();
    }
}
 

您可以获取有关的logcat 了解更多信息此处的 http://developer.android.com/tool​​s/help/logcat.html

Hey is it possible in Android to collect all Log.d from one class and keep on appending it and save it into SDCard ?

For example :

Class Android {

    private final static String TAG = "hello";

    public void abcd(){
    Log.d(TAG, "it went into abcd method");
    }

    public void efgh(){
    Log.d(TAG, "it went into efgh method");
    }

       /*Here collect all above LOGS and write to file in SDCard*/

}

解决方案

You can get all logs with the logcat command. You can try something like:

public static void printLog(Context context){
    String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
    String command = "logcat -d *:V";

    Log.d(TAG, "command: " + command);

    try{
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        try{
            File file = new File(filename);
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            while((line = in.readLine()) != null){
                writer.write(line + "\n");
            }
            writer.flush();
            writer.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

You can get more information about logcat here: http://developer.android.com/tools/help/logcat.html

这篇关于收集所有Log.d从类,并保存到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆