Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extend Android.util.Log to write to file

I would like to extend the android.util.Log class to also write to a log file in internal storage of the device, preferrably also for specific TAGS.

I currently have an implementation:

public class CustomLogger{

private final static Logger fileLog = Logger.getLogger(MainActivity.class);
private Context context;

public CustomLogger(Context c){
    this.context = c;

    final LogConfigurator logConfigurator = new LogConfigurator();
    logConfigurator.setFileName(context.getFilesDir() + File.separator + "myApp.log");
    logConfigurator.setRootLevel(Level.DEBUG);
    logConfigurator.setLevel("org.apache", Level.ERROR);
    logConfigurator.configure();
}

public void i(String TAG, String message){

    // Printing the message to LogCat console
    Log.i(TAG, message);

    // Write the log message to the file
    fileLog.info(TAG+": "+message);
}

public void d(String TAG, String message){
    Log.d(TAG, message);
    fileLog.debug(TAG+": "+message);
  }
 } 

As you can see this custom logger logs both to a log file on the internal storage (using the android-logging-log4j library) and through the android.util.Log class. However i would like the standard log entries from the android.util.Log class in my log file, and if possible only certain (custom) TAGS.

Anybody has an example or any good tips on how to reach this?

Thanks in advance

like image 765
Ricky Avatar asked Sep 10 '13 08:09

Ricky


People also ask

What is verbose in Android Logcat?

Verbose: Show all log messages (the default). Debug: Show debug log messages that are useful during development only, as well as the message levels lower in this list. Info: Show expected log messages for regular usage, as well as the message levels lower in this list.

What is the use of log E in Android?

e: This is for when bad stuff happens. Use this tag in places like inside a catch statement. You know that an error has occurred and therefore you're logging an error.

Where are Android logs stored?

To access the logging output, run the 'adb' executable with following arguments to capture the Android Enterprise related logging: Windows: C:\Users\[username]\AppData\Local\Android\sdk\platform-tools> adb logcat -G 32M; adb shell setprop persist. log.


1 Answers

You can read log cat programmatically and store into text file or you send it wherever you want.

Below is the detailed article I have written for same:

Read & Store Log-cat Programmatically in Android

And for reading the logcat here is sample code:

public class LogTest extends Activity {
 
    private StringBuilder log;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
 
            log=new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
            }
            TextView tv = (TextView)findViewById(R.id.textView1);
            tv.setText(log.toString());
        } catch (IOException e) {
        }       
 
        //convert log to string
        final String logString = new String(log.toString());
 
        //create text file in SDCard
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
        dir.mkdirs();
        File file = new File(dir, "logcat.txt");

        try {  
            //to write logcat in text file
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut); 

            // Write the string to the file
            osw.write(logString);            
            osw.flush();
            osw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
like image 197
swiftBoy Avatar answered Sep 22 '22 17:09

swiftBoy