5

I'm writing logs using slf4android (https://github.com/bright/slf4android) but it is not obvious how to read them (ideally I would just like to download them to my computer). The internal storage of the app is not accessible to other apps. Can I configure the slf4android to log to a shared directory? I've 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);
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
Mattias Petter Johansson
  • 1,064
  • 1
  • 15
  • 32

3 Answers3

9

Once you configured logging to a file by:

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:

  1. Using NotifyDeveloperHandler (my favourite)

    slf4android has a very nice feature (for some reason undocumented) which allows sending an email to a given address with logs and screenshot included in an attachment.

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

it's really handy to use (literally) because you can trigger 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 terminal to copy log file from the device to home directory.

klimat
  • 24,711
  • 7
  • 63
  • 70
1

The official docs says you can do this:

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);

fileHandler.setFullFilePathPattern(SOMEPATH);

LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);

and the log file would be located into SOMEPATH. I would recommend you use a regular environment directory instead of an arbitrary string, like

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

Now, if you want to copy some existing logs to an outher destination, you can just 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 nothing will work if you don't get the proper storage permissions!

Hope it helps. Happy coding!

Fco P.
  • 2,486
  • 17
  • 20
0

In code example your provided you don't actually use "File lol" you've defined. So it probably fails because you try to create another log on top of the first one (e.g. 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 option clicking which would find the logs, may be zip them (together with db or whatever) and send by email, upload to server or just copy to another folder.

Community
  • 1
  • 1
shtolik
  • 1,341
  • 22
  • 29