Search code examples
androidloggingdata-storage

Storing application activity log in Android


I would like to log some data of my application activity so as to read it latter on in the system. The data includes strings, floats etc collected when a button is pressed. What is the best way to do this?


Solution

  • I ended up writing my own function for logging on the device.

    public static BufferedWriter out;
        private void createFileOnDevice(Boolean append) throws IOException {
                /*
                 * Function to initially create the log file and it also writes the time of creation to file.
                 */
                File Root = Environment.getExternalStorageDirectory();
                if(Root.canWrite()){
                     File  LogFile = new File(Root, "Log.txt");
                     FileWriter LogWriter = new FileWriter(LogFile, append);
                     out = new BufferedWriter(LogWriter);
                     Date date = new Date();
                     out.write("Logged at" + String.valueOf(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "\n"));
                }
            }
    
    public void writeToFile(String message){
            try {
                out.write(message+"\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    The related question on SO is here.