105.android 簡單的輸出打印日誌到本地文件,xlog日誌工具類

 

 //1.首先下載我上傳的xlog日誌工具類,放到你本地項目裏,工具類裏有些包名可能報錯,換成你自己的包名。地址:https://download.csdn.net/download/weixin_42061754/12489850

 

//2.在Application的onCreate()方法裏初始化initXlog():

 

public class MyApp extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
        initXlog();
    }

    private void initXlog() {
        LogConfiguration config = new LogConfiguration.Builder()
                .logLevel(BuildConfig.DEBUG ? LogLevel.ALL             // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                        : LogLevel.INFO)
                .tag(getString(R.string.app_name))                   // Specify TAG, default: "X-LOG"
                .build();

        FilePrinter filePrinter = new FilePrinter                     // Printer that print the log to the file system
                .Builder(new File(Environment.getExternalStorageDirectory(),
                "caoJiaFeng").getPath(), 7)       // 創建的文件夾名字  Specify the path to save log file and max logs file
                .fileNameGenerator(new DateFileNameGenerator())        // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(new FileSizeBackupStrategy(1024 * 1024 * 3))             // 日誌文件大小設置  Default: FileSizeBackupStrategy(1024 * 1024)
                .logFlattener(new ClassicFlattener())                  // Default: DefaultFlattener
                .build();
        XLog.init(                                                     // Initialize XLog
                config,                                                // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
                filePrinter);
    }

}

//3.在你想要打印的地方,打印log日誌輸出到本地文件(需要讀寫權限,否則失敗):

XLog.d("Log time:",  new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()));
XLog.e("Log time:",  new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()));
XLog.i("Log time:",  new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()));
XLog.w("Log time:",  new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()));

//在DateFileNameGenerator裏的generateFileName()方法可以自定義修改日誌文件名,這裏使用的是當前日期:

//在FilePrinter類的appendLog()方法裏可以設置是否日誌內容加密,默認是不加密的,如果需要加密請自己配置:

//------------------------------------------------------------------------------完----------------------------------------------------------------------------------------- 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章