Skip to content
Atharva Sathe edited this page Mar 14, 2023 · 1 revision

Overview

Now that the web server has logging functionality, the Android client has also added functionality to send logs to the server. The original plan was to allow the user to choose if they wanted logs to be uploaded automatically or manually when they wanted. Due to the extended time needed to research Android cache and local storage, logs are not stored locally on the device in cache. If the user chooses to opt-out of automatic log uploading, logs simply will not be uploaded to the server nor stored locally.

When logs are sent to to the server, they must be sent in this format:

{
    "id": "6B819921-4A5A-4AC5-AEA6-2C97DC7326E3",
    "content": "[Log content goes here]",
    "clientPlatform": "android",
    "date": "2022-11-20T05:17:32Z"
}

The HTTP endpoint on the server is /logs. (For example, if the base URL is https://shuttletracker.app, then the full URL would be https://shuttletracker.app/logs.)

How to use Logs in the Android Client

There are 2 main parts to logs: writing to the buffer and sending to the server.

Writing Logs

You can use writeToLogBuffer(functionName: Any, message: String) to log a general message to the buffer or writeExceptionToLogBuffer(functionName: Any, ex: Exception) to log an exception. The function name should be retrieved by calling object{}.javaClass.enclosingMethod.name.

For example: Logs.writeToLogBuffer(object{}.javaClass.enclosingMethod.name, "this is a new log")

If, for some reason, there is no parent function, you can use writeToLogBuffer(message: String)

Sending Logs to Server

You can use sendLogsToServer(logsURL: URL) to send all of the logs in the buffer to the server.

Each activity in the app has a getLogsURL() function which retrieves the logs url from the shared resources. Why do we not just pass resources through? Passing resources and contexts to different functions can cause memory leaks, so we just retrieve the logs url in the activity and pass it to the logs object.

For example: Logs.sendLogsToServer(getLogsURL())

For now, the log buffer is trimmed so only the last 100 logs are stored and sent to the server. This can be changed in the future by modifying the trimLogsBuffer() function. The logs are also flushed automatically with flushLogsBuffer() everytime they are sent to the server.

Clone this wiki locally