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

     2023-02-24     43

关键词:

【中文标题】扩展 Android.util.Log 以写入文件【英文标题】:extend Android.util.Log to write to file 【发布时间】:2013-09-10 08:29:09 【问题描述】:

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

任何人有一个例子或任何好的提示来达到这个目标?

提前致谢

【问题讨论】:

Write android logcat data to a file的可能重复 【参考方案1】:

您可以通过编程方式读取 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();
        
    

【讨论】:

【参考方案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();

【讨论】:

执行 logcat 安全吗?我的意思是,你能指望它保证可用吗?您的应用是否需要某些权限?

如何监控 android.util.log 或覆盖它?

】如何监控android.util.log或覆盖它?【英文标题】:HowcanImonitorandroid.util.logoroverrideit?【发布时间】:2019-08-3107:15:20【问题描述】:我在项目的任何地方都使用log.d/v/w/e。现在,我想将所有日志保存到设备本地文件中。然而,android.... 查看详情

将图像数据写入文本文件以创建 BMP 文件

...的数据复制并粘贴到另一个文本文件中,将其保存为.BMP扩展名。但 查看详情

如何查看log日志文件

...Java与C语言中输出日志:1)Java代码在程序中输出日志,使用android.util.Log类的以下5个方法:Log.v()、Log.d()、Log.i()、Log.w()、Log.e()。分对应Verbose、Debug、INFO、Warn、Error的首字母。例如:Log.i("类::函数名","日期_时间_源码文... 查看详情

如何查看log日志文件呢?

...Java与C语言中输出日志:1)Java代码在程序中输出日志,使用android.util.Log类的以下5个方法:Log.v()、Log.d()、Log.i()、Log.w()、Log.e()。分对应Verbose、Debug、INFO、Warn、Error的首字母。例如:Log.i("类::函数名","日期_时间_源码文件名_行号_日... 查看详情

使用扩展音频文件写入和读取音频文件

】使用扩展音频文件写入和读取音频文件【英文标题】:WritingandreadinganaudiofilewithExtendedAudioFile【发布时间】:2015-09-2108:19:22【问题描述】:我在使用设备录制时使用ExtAudioFileWriteAsync写入音频文件,但是一旦我完成录制,我会尝... 查看详情

使用超类和扩展类方法写入随机访问文件

】使用超类和扩展类方法写入随机访问文件【英文标题】:Writetorandomaccessfileusingsuperclassandextendedclassmethods【发布时间】:2011-11-2401:16:27【问题描述】:我有一个Book类和一个扩展Book的LibraryBook类。我将信息存储在随机访问文件中... 查看详情

在所有类型的文件上写入文件扩展属性“修订号”

】在所有类型的文件上写入文件扩展属性“修订号”【英文标题】:Writefileextendedproperty"RevisionNumber"onalltypeoffiles【发布时间】:2011-12-0511:13:06【问题描述】:我想用c#读/写窗口信息文件(扩展文件属性)通过执行以下操... 查看详情

为啥文件扩展名会影响写入速度? (C#,流编写器)

】为啥文件扩展名会影响写入速度?(C#,流编写器)【英文标题】:Whydoesfileextensionaffectwritespeed?(C#,StreamWriter)为什么文件扩展名会影响写入速度?(C#,流编写器)【发布时间】:2011-03-1322:24:03【问题描述】:我目前正在测试... 查看详情

在 OS X 上将扩展属性写入文件

】在OSX上将扩展属性写入文件【英文标题】:WritingextendedattributestofilesonOSX【发布时间】:2016-02-0123:17:33【问题描述】:我试图在命令行中使用setfattr命令,但它告诉我commandnotfound。我假设这意味着该软件包未安装,但是据我了解... 查看详情

以原子方式写入文件与不写入文件之间的区别

】以原子方式写入文件与不写入文件之间的区别【英文标题】:Differencebetweenwritingtofileatomicallyandnot【发布时间】:2010-04-2415:48:02【问题描述】:objective-c在iPhone上以原子方式写入文件有什么区别,两者之间有什么性能差异吗?【... 查看详情

如何从 vscode 扩展写入日志?

】如何从vscode扩展写入日志?【英文标题】:Howtowritetologfromvscodeextension?【发布时间】:2016-03-0904:59:51【问题描述】:我正在尝试为VSCode开发带有语言服务器的扩展。我试图弄清楚如何从扩展的语言服务器部分编写文本以记录日... 查看详情

使用 vscode 扩展 api 读取和写入数据到 .txt 文件

】使用vscode扩展api读取和写入数据到.txt文件【英文标题】:Reading&writingdatatoa.txtfileusingvscodeextensionapi【发布时间】:2021-02-0900:23:14【问题描述】:所以我是这个vscode扩展api的新手。我有这个功能,当用户单击某行时,我需要... 查看详情

android----log

android.util.Log常用的方法有以下5个:Log.v()Log.d()Log.i()Log.w()以及Log.e()。根据首字母对应VERBOSE,DEBUG,INFO,WARN,ERROR。1、Log.v的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("","");2、Log.d的... 查看详情

使用 python 将二进制数据写入 zip 文件

...zip文件。以下内容有效,但如果我尝试添加.zip作为文件扩展名以在变量x中“检查”,则不会将任何内容写入文件。我被卡住手动添加.zipurla="someurl"tok="sometoken"pp="token":tokt=r 查看详情

递归爬取并以异步方式写入文件

】递归爬取并以异步方式写入文件【英文标题】:Crawlrecursivelyandwritetofileinasynchronousmanner【发布时间】:2020-02-2804:47:11【问题描述】:我对异步代码完全陌生,所以我现在有点不知所措。我正在做的是递归地异步抓取存档,以检... 查看详情

我的android进阶之旅------>android关于log的一个简单封装

android.util.Log类,能够方便地用于在编码调试过程中打印日志。可是在公布后的产品中,假设有太多的日志打印。则会严重地影响性能。对android.util.Log类做一个简单的封装。当产品要公布的话,将Debug设为false。代码例如以下。pub... 查看详情

java以缓冲字符流向文件写入内容(如果文件存在则删除,否则先创建后写入)

功能:Java以缓冲字符流向文件写入内容(如果文件存在则删除,否则先创建后写入)publicvoidSave_local(XinJianxinJian,Stringfiles)throwsException//xieruxinjiandexiangxixingxi{Filefile=newFile("D:javaxiangmueiJinXinJianWebContentoutfi 查看详情

5.5-5.7vim的使用和扩展

5.5-5.7vim的使用和扩展vim的设置“:wq”和“:x”的区别“:wq”强制性写入文件并退出(存盘并退出writeandquite)。即使文件没有被修改也强制写入,并更新文件的修改时间。“:x”写入文件并退出。仅当文件被修改时才写入,并... 查看详情