10

我想扩展android.util.Log该类以写入设备内部存储中的日志文件,最好也用于特定的TAGS.

我目前有一个实现:

public class CustomLogger{

private final static Logger fileLog = Logger.getLogger(MainActivity.class);
private Context context;

public CustomLogger(Context c){
    this.context = c;

    final LogConfigurator logConfigurator = new LogConfigurator();
    logConfigurator.setFileName(context.getFilesDir() + File.separator + "myApp.log");
    logConfigurator.setRootLevel(Level.DEBUG);
    logConfigurator.setLevel("org.apache", Level.ERROR);
    logConfigurator.configure();
}

public void i(String TAG, String message){

    // Printing the message to LogCat console
    Log.i(TAG, message);

    // Write the log message to the file
    fileLog.info(TAG+": "+message);
}

public void d(String TAG, String message){
    Log.d(TAG, message);
    fileLog.debug(TAG+": "+message);
  }
 } 

正如您所看到的,此自定义记录器将日志记录到内部存储(使用android-logging-log4j库)和通过android.util.Log类的日志文件中。但是,我希望android.util.Log日志文件中的类中的标准日志条目,如果可能的话,只有某些(自定义)TAGS

任何人都有一个例子或任何关于如何实现这一目标的好技巧?

提前致谢

4

2 回答 2

7

您可以以编程方式读取 log cat 并将其存储到文本文件中,或者将其发送到任何您想要的地方。

以下是我为此写的详细文章:

在 Android 中以编程方式读取和存储 Log-cat

为了阅读 logcat,这里是示例代码:

public class LogTest extends Activity {
 
    private StringBuilder log;
 
    @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()));
 
            log=new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
            }
            TextView tv = (TextView)findViewById(R.id.textView1);
            tv.setText(log.toString());
        } catch (IOException e) {
        }       
 
        //convert log to string
        final String logString = new String(log.toString());
 
        //create text file in SDCard
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
        dir.mkdirs();
        File file = new File(dir, "logcat.txt");

        try {  
            //to write logcat in text file
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut); 

            // Write the string to the file
            osw.write(logString);            
            osw.flush();
            osw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2013-09-10T08:38:34.963 回答
2

所以有更短的变体

try {
    final File path = new File(
            Environment.getExternalStorageDirectory(), "DBO_logs5");
    if (!path.exists()) {
        path.mkdir();
    }
    Runtime.getRuntime().exec(
            "logcat  -d -f " + path + File.separator
                    + "dbo_logcat"
                    + ".txt");
} catch (IOException e) {
    e.printStackTrace();
}
于 2015-08-03T15:55:05.440 回答