android - 扩展 Android.util.Log 以写入文件

标签 android logging android-logcat

我想扩展 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

有人有关于如何实现这一目标的示例或任何好的提示吗?

提前致谢

最佳答案

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

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

Read & Store Log-cat Programmatically in Android

这里是阅读 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();
        }
    }
}

关于android - 扩展 Android.util.Log 以写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18714060/

相关文章:

android - 如何在 Android Studio 的 LogCat 中过滤多个标签?

java - 如何向创建的 GSON 添加属性到 JSON?

android - 自定义 Android 操作系统构建

android - Android会杀死每个服务还是整个进程?

Android 不使用/找不到我的希伯来语本地化版本。

logging - 每个 Web 服务/页面使用单独的 log4net

从 loop.create_task() 引发的 python asyncio 异常

json - 读取日志时,JSON 从双引号变为单引号

android - 如何在 ddms logcat 中禁用不需要的设备日志

android - Android开发中使用logcat的基础知识