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()方法里可以设置是否日志内容加密,默认是不加密的,如果需要加密请自己配置:

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

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/105141054
今日推荐