Reading a file registered through slf4android?

I write magazines using slf4android ( https://github.com/bright/slf4android ), but it is not clear how to read them (ideally, I would just like to download them to my computer). The internal memory of the application is not available for other applications. Can I configure slf4android to enter the shared directory? I tried this, but I get NOENT:

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); File lol = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); fileHandler.setFullFilePathPattern(fileHandler.toString() + "/my_log.%g.%u.log"); LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 
+5
source share
3 answers

Once you have configured logging to a file, follow these steps:

 FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); fileHandler.setFullFilePathPattern("/sdcard/your.package/my_log.log"); LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 

There are two (both simple) ways to get a log file:

  • Using NotifyDeveloperHandler (my favorite)

    slf4android has a very nice feature (for some reason undocumented) that allows you to send email to the specified address with the logs and screenshots included in the application .

     NotifyDeveloperHandler handler = LoggerConfiguration.configuration().notifyDeveloperHandler(this, " example@gmail.com "); handler.notifyWhenDeviceIsShaken(); LoggerConfiguration.configuration().addHandlerToRootLogger(handler); 

it's really convenient to use (literally) because you can initiate a send action by shaking your device.

enter image description here enter image description here

  1. Using adb

    Run adb pull /sdcard/your.package/my_log.log ~/my_log.log in the terminal to copy the log file from the device to the home directory.

+9
source

Official docs say you can do this:

 FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); fileHandler.setFullFilePathPattern(SOMEPATH); LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 

and the log file will be placed in SOMEPATH. I would recommend using a regular environment directory instead of an arbitrary string, e.g.

 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath()+File.pathSeparator+"appLogs" 

Now, if you want to copy some existing logs to the outper destination, you can simply copy the files.

 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(); } } 

Finally, keep in mind that nothing will work if you do not get the correct permissions to access the repository!

Hope this helps. Happy coding!

+1
source

In the code example below, you are not using the “File lol” that you defined. Therefore, it probably fails because you are trying to create another log on top of the first (for example, in "/sdcard/your.package/my_log.%g.%u.log/my_log.%g.%u.log") ;

Try:

 File lol = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); fileHandler.setFullFilePathPattern(lol.getAbsolutePath() + "/my_log.%g.%u.log"); 

But you can also just add a menu item that will look for logs, maybe zip them (along with db or something else) and send them by e-mail , upload them to the server, or simply copy them to another folder.

0
source

Source: https://habr.com/ru/post/1260097/


All Articles