Skip to content

Commit

Permalink
Implement Logger and refine existing logs
Browse files Browse the repository at this point in the history
- Introduces a new Logger class with configurable log levels to provide enhanced logging control.
- Refine existing logs within TestDataUploader for improved clarity.
- Add more meaningful logs around upload error.
  • Loading branch information
thebhumilsoni committed Apr 12, 2024
1 parent 1b67873 commit 3a27e55
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.buildkite.test.collector.android.model.TestUploadResponse
import com.buildkite.test.collector.android.network.TestAnalyticsRetrofit
import com.buildkite.test.collector.android.network.api.TestUploaderApi
import com.buildkite.test.collector.android.util.CollectorUtils.Uploader
import com.buildkite.test.collector.android.util.Logger
import retrofit2.Response

/**
Expand All @@ -20,6 +21,9 @@ class TestDataUploader(
private val testSuiteApiToken: String?,
private val isDebugEnabled: Boolean
) {
private val logger =
Logger(minLevel = if (isDebugEnabled) Logger.LogLevel.DEBUG else Logger.LogLevel.INFO)

/**
* Configures and uploads test data.
* The number of test data uploaded in a single request is constrained by [Uploader.TEST_DATA_UPLOAD_LIMIT].
Expand All @@ -40,33 +44,35 @@ class TestDataUploader(

private fun uploadTestData(testData: TestData) {
if (testSuiteApiToken == null) {
println(
"Buildkite test suite API token is missing. " +
"Please set up your API token environment variable to upload the analytics data. Follow [README] for further information."
)
} else {
logger.info {
"Test Suite API token is missing. Please ensure the 'BUILDKITE_ANALYTICS_TOKEN' environment variable is set correctly to upload test data."
}
return
}

try {
logger.debug { "Uploading test analytics data." }

val testUploaderService =
TestAnalyticsRetrofit.getRetrofitInstance(testSuiteApiToken = testSuiteApiToken)
.create(TestUploaderApi::class.java)
val uploadTestDataApiCall = testUploaderService.uploadTestData(testData = testData)
val testUploadResponse = uploadTestDataApiCall.execute()

val executeApiCall = uploadTestDataApiCall.execute()

if (isDebugEnabled) {
logApiResponse(executeApiCall)
}
logApiResponse(testUploadResponse = testUploadResponse)
} catch (e: Exception) {
logger.error { "Error uploading test analytics data: ${e.message}." }
}
}

private fun logApiResponse(executeApiCall: Response<TestUploadResponse>) {
when (val apiResponseCode = executeApiCall.raw().code) {
202 -> println(
"\nTest analytics data successfully uploaded to the BuildKite Test Suite. - ${executeApiCall.body()?.runUrl}"
)

else -> println(
"\nError uploading test analytics data to the BuildKite Test Suite. Error code: $apiResponseCode! Ensure that the test suite API Token is correct."
)
private fun logApiResponse(testUploadResponse: Response<TestUploadResponse>) {
if (testUploadResponse.isSuccessful) {
logger.debug { "Test analytics data successfully uploaded. URL: ${testUploadResponse.body()?.runUrl}" }
} else {
logger.error {
"Error uploading test analytics data. HTTP error code: ${testUploadResponse.code()}. Ensure the test suite API token is correct and properly configured."
}
logger.debug { "Failed response details: ${testUploadResponse.errorBody()?.string()}" }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.buildkite.test.collector.android.util

/**
* Provides logging functionality with configurable log level sensitivity.
*
* @property minLevel The minimum log level that will be logged.
*/
class Logger(
private val minLevel: LogLevel = LogLevel.INFO
) {
/**
* Logs a message at [LogLevel.DEBUG].
* - Messages are logged only if [LogLevel.DEBUG] is greater than or equal to [minLevel].
*/
fun debug(message: () -> String) = log(LogLevel.DEBUG, message)

/**
* Logs a message at [LogLevel.INFO].
* - Messages are logged only if [LogLevel.INFO] is greater than or equal to [minLevel].
*/
fun info(message: () -> String) = log(LogLevel.INFO, message)

/**
* Logs a message at [LogLevel.ERROR].
* - Messages are logged only if [LogLevel.ERROR] is greater than or equal to [minLevel].
*/
fun error(message: () -> String) = log(LogLevel.ERROR, message)

/**
* Conditionally logs messages based on the [minLevel] set.
* - Logs to standard output for [LogLevel.DEBUG] and [LogLevel.INFO], and to standard error for [LogLevel.ERROR].
*/
private fun log(level: LogLevel, message: () -> String) {
if (level >= minLevel) {
val output = if (level == LogLevel.ERROR) System.err else System.out
output.println("\nBuildkiteTestCollector-${level.name}: ${message()}")
}
}

/**
* Defines the log levels, ordered from least to most severe.
*/
enum class LogLevel {
DEBUG,
INFO,
ERROR
}
}

0 comments on commit 3a27e55

Please sign in to comment.