java 创建日志文件并保存应用程序日志

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 创建日志文件并保存应用程序日志相关的知识,希望对你有一定的参考价值。

class log {  
  
    private static final int READ_PERMISSION_CODE = 24;
    private static final int EXTERNAL_PERMISSION_CODE = 25;
  
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /*
        * Code for saving log files into external storage of Android device
        */

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_PERMISSION_CODE);
        }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_PERMISSION_CODE);
        }

        if ( isExternalStorageWritable() ) {

            //File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyPersonalAppFolder" );
            File appDirectory = new File( Environment.getExternalStorageDirectory() + "/CollectionAutomationLog" );
            File logDirectory = new File( appDirectory + "/log" );
            File logFile = new File( logDirectory, "logcat" + System.currentTimeMillis() + ".txt" );

            // create app folder
            if ( !appDirectory.exists() ) {
                appDirectory.mkdir();
            }

            // create log folder
            if ( !logDirectory.exists() ) {
                logDirectory.mkdir();
            }

            // clear the previous logcat and then write the new one to the file
            try {
                Process process = Runtime.getRuntime().exec("logcat -c");
                process = Runtime.getRuntime().exec("logcat -f " + logFile);
            } catch ( IOException e ) {
                e.printStackTrace();
            }

        } else if ( isExternalStorageReadable() ) {
            // only readable
        } else {
            // not accessible
        }


        /*
        * Code completed for saving log files into external storage of Android device
        */

        setContentView(R.layout.activity_splash);
  .
  .
  .
}

/* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
            return true;
        }
        return false;
    }
    
}

以上是关于java 创建日志文件并保存应用程序日志的主要内容,如果未能解决你的问题,请参考以下文章

Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件

Android:写日志文件的库?

如何使用 java 启动 charles 代理并使用特定配置自动保存日志

caffe保存训练log日志文件并利用保存的log文件绘制accuary loss曲线图

Excel,创建自动保存/更改日志,如何?

java 中怎么将程序出现的异常信息保存到日志文件中