Question: I need to log my messages not only into system logs ( as I know, system log buffer is quite short, but I need to see logs for 3-5 days ), but also in a separate text file. Logging must be asynchronous.
Could you give me an advice about which component should I use in this case?
Thanks.

Answer: 
I hope it will be useful for you.

```
public void appendLog(String text) {
    File logFile = new File("sdcard/log.file");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        //BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.append(text);
        buf.newLine();
        buf.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

```

Don't forget to add permission for `android.permission.WRITE_EXTERNAL_STORAGE` in Manifest!
**Works asynchronously and dose not need to permission !** 

just remember call the init method from your application in onCreateMethod for initializing the Logger

```
class Logger {

    private static File logFileLoc;
    private static ExecutorService logExecutor;

    public static void init(Context applicationContext, String logFileName, boolean reCreate) {

        logFileLoc = new File(applicationContext.getCacheDir(), logFileName);

        if (reCreate && logFileLoc.exists()) logFileLoc.delete();

        logExecutor = Executors.newSingleThreadExecutor();
    }

    public static void log(final String tag, final String msg) {

        if (logFileLoc == null) try {

            throw new Exception("First you should call init method in your application");

        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d(tag, msg);

        logExecutor.execute(new Runnable() {
            @Override
            public void run() {

                try {

                    BufferedWriter writer = new BufferedWriter(new FileWriter(logFileLoc,true));

                    String timeStamp = DateFormat.getDateTimeInstance().format(new Date(System.currentTimeMillis()));

                    writer.append(timeStamp + "  " + tag + "  : " + msg );
                    writer.newLine();

                    writer.flush();

                    writer.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }

}

```

also you can do the same thing with Timber library for more info : 

<https://medium.com/@vicky7230/file-logging-with-timber-4e63a1b86a66>