官方的文档说,你可以这样做:
FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
fileHandler.setFullFilePathPattern(SOMEPATH);
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
和日志文件将位于成SOMEPATH。我会建议你使用一个普通的环境目录而不是任意的字符串,如
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath()+File.pathSeparator+"appLogs"
现在,如果你希望将一些现有日志复制到outher目的地,你可以复制文件。
if(BuildConfig.DEBUG) {
File logs = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath(), "logs");
FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
File currentLogs = fileHandler.getCurrentFileName();
if (currentLogs.exists()) {
FileChannel src = new FileInputStream(currentLogs).getChannel();
FileChannel dst = new FileOutputStream(logs).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
最后,请记住,如果您没有获得正确的存储权限,则什么都不会有效!
希望它有帮助。快乐的编码!
你得到了什么错误? –
我的答案解决了你的问题吗? – mklimek