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 HyperLogis 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 inthe 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:

HyperLog.initialize(this)
	HyperLog.setLogLevel(Log.VERBOSE)

	HyperLog.d("Test logs","Debug Log")
	HyperLog.setURL("https://xxxxxxx0xxxxx.x.pipedream.net/")

	HyperLog.pushLogs(this, false, object: HLCallback() {

	  override fun onSuccess(response: Any) {
	    //Handle success
	  }

	  override fun onError(Error: HLErrorResponse) {
	    //Handle errors
	  }

	})

Sample code snippet for HyperLog implementation using Kotlin

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.ERROR_or _LOG_LEVEL_NONE_ before 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.ASSERTLog.VERBOSELog.DEBUGLog.INFOLog.WARN, and Log.ERRORavdiwe, and exception are used, respectively. Exception uses a throwable too.

#mobile #kotlin #android #logging #programming

How to Implement HyperLog With Kotlin in Android
4.45 GEEK