Skip to content
Muhammad Umair Adil edited this page Dec 17, 2018 · 5 revisions

Prerequisites

Logging is done on storage directory so it's very important to add these permissions to your project's manifest file first.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Note:

Min API level supported is 16.

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

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

Step 2. Add the dependency

dependencies {
   implementation 'com.github.umair13adil:RxLogs:xxx' //Check latest version
}

Step 3. Initialize the library in your Application Class:

class MainApplication : Application() {
    
        override fun onCreate() {
            super.onCreate()
    
            setUpPLogger() //Initialize PLogger here
        }

private fun setUpPLogger() {
             val logsPath = Environment.getExternalStorageDirectory().absolutePath + File.separator + "PLogs"
             
                     val logsConfig = LogsConfig(
                logLevelsEnabled = arrayListOf(LogLevel.ERROR, LogLevel.SEVERE, LogLevel.INFO, LogLevel.WARNING),
                logTypesEnabled = arrayListOf(LogType.Notification.type, LogType.Location.type, LogType.Navigation.type, LogType.Errors.type, "Deliveries"),
                formatType = FormatType.FORMAT_CURLY,
                logsRetentionPeriodInDays = 7, //Will not work if local XML config file is not present
                zipsRetentionPeriodInDays = 3, //Will not work if local XML config file is not present
                autoDeleteZipOnExport = true,
                autoClearLogs = true,
                enabled = true,
                exportFileNamePreFix = "[",
                exportFileNamePostFix = "]",
                autoExportErrors = true,
                encryptionEnabled = false,
                encryptionKey = "",
                singleLogFileSize = 1, //1Mb
                logFilesLimit = 30,
                directoryStructure = DirectoryStructure.FOR_EVENT,
                nameForEventDirectory = "MyLogs",
                logSystemCrashes = true,
                autoExportLogTypes = arrayListOf(),
                autoExportLogTypesPeriod = 3,
                isDebuggable = true,
                debugFileOperations = false,
                logFileExtension = LogExtension.LOG,
                attachTimeStamp = true,
                attachNoOfFiles = true,
                timeStampFormat = TimeStampFormat.TIME_FORMAT_READABLE,
                zipFilesOnly = false,
                savePath = logsPath,
                zipFileName = "MyLogs",
                exportPath = logsPath + File.separator + "PLogsOutput",
                context = this //Needed to format exceptions in HTML with CSS styling
        ).also { it ->

            //Subscribe to Events listener
            it.getLogEventsListener()
                    .doOnNext {

                        when (it.event) {
                            EventTypes.NEW_ERROR_REPORTED -> {
                                PLog.logThis("PLogger", "event", it.data, LogLevel.INFO)
                            }
                            EventTypes.SEVERE_ERROR_REPORTED -> {
                                PLog.logThis("PLogger", "Severe Error: ", it.data, LogLevel.INFO)
                            }
                            EventTypes.NEW_ERROR_REPORTED_FORMATTED -> {
                                PLog.logThis("PLogger", "formatted", it.data, LogLevel.INFO)
                            }
                            EventTypes.SEVERE_ERROR_REPORTED_FORMATTED -> {
                                PLog.logThis("PLogger", "formatted", it.data, LogLevel.INFO)
                            }
                            EventTypes.PLOGS_EXPORTED -> {
                            }
                            EventTypes.DATA_LOGS_EXPORTED -> {
                            }
                            EventTypes.LOGS_CONFIG_FOUND -> {
                                PLog.getLogsConfigFromXML()?.let { config ->
                                    val gson = GsonBuilder().setPrettyPrinting().create()
                                    PLog.logThis("PLogger", "event", gson.toJson(config).toString(), LogLevel.INFO)
                                }
                            }
                            EventTypes.NEW_EVENT_DIRECTORY_CREATED -> {
                                PLog.logThis("PLogger", "event", "New directory created: " + it.data, LogLevel.INFO)
                            }
                            EventTypes.LOG_TYPE_EXPORTED -> {
                                PLog.logThis("PLogger", "event", "Log Type: " + it.data, LogLevel.INFO)
                            }
                            EventTypes.DELETE_LOGS -> {
                                PLog.logThis("PLogger", "event", "DELETE_LOGS: " + it.data, LogLevel.INFO)
                            }
                            EventTypes.DELETE_EXPORTED_FILES -> {
                                PLog.logThis("PLogger", "event", "DELETE_EXPORTED_FILES: " + it.data, LogLevel.INFO)
                            }
                            else -> {
                            }
                        }
                    }
                    .subscribe()
        }

                     PLog.applyConfigurations(logsConfig, saveToFile = true) //Initialize configurations         
         }
}

Step 4. Register it in the manifest file

<application
    android:name=".MainApplication"
    ...
    >

That's It. You are all setup.