//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()方法里可以设置是否日志内容加密,默认是不加密的,如果需要加密请自己配置:
//——————————————————————————完—————————————————————————————–
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118289.html