Save Logs In Storage from Android Application

Abhishek Srivastava
2 min readSep 16, 2020

--

I have created one library to save logs in local storage.

If you want to save logs in your local storage and access or check logs when the application is released or in production mode then you can narrow your records from storage.

There are a few steps to implement a logs library in your android code to save logs in local storage.

If you want to check this please go through the below link and check How to implement the save logs library in a few minutes

Step 1. Add the JitPack repository to your build file

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

Step 2. Add the dependency

dependencies {
implementation 'com.github.abhi10jul:SaveLogsInStorage:1.0.1'
}

Step 3. Add these variables in Activity or Fragment globally

private static final int PERMISSION_REQUEST_CODE = 101;
public SaveLogsInStorage saveLoggerInstance;
public static final String directoryName = "CustomLogger";

Optional: You can also declare directoryName in a constant file or static variable because If you want to add like this you can easily use the same parameter in the SaveLogsInStorage constructor

Step 4. Ask to write storage permission to the User

private boolean askWriteStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return (ContextCompat.checkSelfPermission(LauchingActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
}
return true;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}

Step 5. Get onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("value", "Permission Granted, Now you can use save logs in local drive .");
initialisedLogger();
} else {
Log.e("value", "Permission Denied, You cannot use save logs in local drive .");
Toast.makeText(LauchingActivity.this, "Write External Storage permission allows us to do store logs in local storage. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
}
}
}

Step 6: Initialise logger

private void initialisedLogger() {
/*
* First initialised SaveLogsInStorage in variable
* to access all save logs instance method
*/
saveLoggerInstance = SaveLogsInStorage.getSaveLoggerInstance(this, directoryName);
/*
*Explain how to use all save logs instance method
*/
printLogsInStorage();
}
private void printLogsInStorage() {  
Log.i(TAG, "save info logs"); saveLoggerInstance.saveInfoLogs(TAG, "successfully saved info logs");
Log.d(TAG, "save debug logs"); saveLoggerInstance.saveDebugLogs(TAG, "successfully saved debug logs");
Log.w(TAG, "save warning logs"); saveLoggerInstance.saveWarningLogs(TAG, "successfully warnings info logs");
Log.e(TAG, "save error logs"); saveLoggerInstance.saveErrorLogs(TAG, "successfully saved error logs");
try {
Log.e(TAG, "save error logs with exception");
// int dividedZero = 1 / 0;
} catch (Exception e) {
e.printStackTrace(); saveLoggerInstance.saveErrorLogs(TAG, "successfully saved error logs", e);
}
}

Step 7: Read the below lines

Do not repeat Steps 1 to 5 for each Activity or fragment. These implement one time in Launching Activity or First Activity.
This is optional If you want to add all activities You can add these steps. These steps give us the benefit of checking permission all time

Examples 😍

Special Thanks to ALL ❤️

--

--

Abhishek Srivastava
Abhishek Srivastava

Written by Abhishek Srivastava

I am Software Developer having Java, Java Spring boot Rest services, Android, Kotlin, Flutter skills and Android SDK libraries...

No responses yet