How to Implement HyperLog With Kotlin in Android
A detailed guide on how to implement HyperTrack/HyperLog and log data to a remote server with Kotlin
Logging is necessary when a developer needs to debug the issue in any part of the code. In Android, we have Log, which is super useful when it comes to looking for any error in the code. If you want to see or save the logs remotely to your server, then HyperLog is one of the popular options, as it’s built on top of Log.
Now, there are numerous blog posts that can help you understand what HyperLog is and how it works (like this one) using Java. Today’s article is a detailed guide for implementing it using Kotlin and an exploration of its features, so if you are struggling to understand it or convert the Java code to Kotlin, then this should clear things up.
Once the library is added into the dependencies in the build.gradle file of the application, you can start recording the log messages and push them to the remote server through the following code snippet:
The code snippet above is from the launcher activity of an application where HyperLog has been initialized. The log level is set to show which type of logging will be done further. After specifying the log type with its tag and message, the URL has been set to validate the pushing of logs to the remote server (in this case, RequestBin). After the logs are pushed, either through a file or in plain text, the callback methods of success and error complete the task.
Below is a detailed guide of the HyperLog library in Kotlin.
Initialization
HyperLog.initialize(context: Context, expiryTimeInSecond: Int, logFormat: LogFormat)This initializes the HyperLog functionality in the application. By default, logs seven days or older get deleted automatically. Hence, push them to the remote server within that time or immediately. Context is a mandatory parameter of this method. Others can be removed while initializing.
Logging Structure
HyperLog.setLogFormat(logFormat: LogFormat)This defines a custom log message format. Custom log messages are useful. The developer can choose what details they need in the logs and in which structure.
HyperLog.setLogLevel(logLevel: Int)According to the documentation:
“Sets the level of logging to display, where each level includes all those below it. The default level is LOG_LEVEL_NONE. Please ensure this is set to
Log.ERRORorLOG_LEVEL_NONEbefore deploying your app to ensure no sensitive information is logged.”
HyperLog.d(tag: String, message: String)Like the other log levels, if debug is used, then d is for that. Similarly, according to the specified log levels among Log.ASSERT, Log.VERBOSE, Log.DEBUG, Log.INFO, Log.WARN, and Log.ERROR, a, v, d, i, w, e, and exception are used, respectively. Exception uses a throwable too.
Setting and Getting the URL
HyperLog.setUrl(url: String)This method sets the valid URL endpoint where the logs need to be pushed. In case it’s empty or null, it throws an Illegal Argument Exception.
HyperLog.getUrl()When the URL is set through HyperLog.setUrl(), it is available for the whole application. If that URL is needed at some other part in the application, then HyperLog.getUrl() gets that URL from inside the saved variable in the library. This method returns as string.
Getting Expiry Time
HyperLog.getExpiryTime()If any expiry time is set for the logs, this method returns the length of time in seconds. Otherwise, it returns the default expiry time (i.e. 604800).
Getting Device Logs
HyperLog.getDeviceLogs(deleteLogs: Boolean, batchNumber: Int)This method gets the stored logs in the device according to the batch number. If provided otherwise, the default batch number is 1. Also, it deletes the logs after getting if deleteLogs is set to true. Because the default is set to true, it’s mandatory to set false in case the deletion of logs is not necessary.
HyperLog.getDeviceLogsAsStringList(deleteLogs: Boolean, batchNumber: Int)This method gets the stored logs as a string list rather than the device log model as described above (in the getDeviceLogs method). The rest of this method’s functionality is the same as for the previous method.
HyperLog.getDeviceLogsInFile(context: Context, fileName: String, deleteLogs: Boolean)This method gets the stored logs as a file object with the provided filename or creates the filename according to the timestamp on its own. The deletion of the logs works in the same manner as for the other two methods above.
Log Availability
HyperLog.hasPendingDeviceLogs()If there are any logs available in the device, this method returns true in case any pending logs are available. Otherwise, it returns false.
HyperLog.getDeviceLogsCount()This method returns the number of stored device logs (if any). Otherwise, it returns 0.
HyperLog.getDeviceLogBatchCount()This method returns the number of stored device log batches (if any). Otherwise, it returns 0. Each batch contains 5,000 device logs.
Pushing Logs to Server
HyperLog.pushLogs(context: Context, filename:String, additionalHeaders: HashMap<String, String>, compress: Boolean, hlCallback: HLCallback) This method pushes the logs from the device to the server as a text file or gzip compressed file if compress is true. Otherwise, a simple text can also be pushed, keeping it false. Once the logs are pushed, they are automatically deleted from the device. A benefit of HyperLog is if the log count is greater than the device’s log query limit, then it gets pushed in batches. Also, there’s no need to have the filename or additional headers in the method if no files are included, but their presence makes the method perfect. This method provides onSuccess and onError callback functions where success and error can be handled as required.
Delete Logs
HyperLog.deleteLogs()This method helps delete all logs from the device.
Conclusion
I hope this gives more insight into the functionalities provided by the library.
Thanks for reading. Happy logging!
