Skip to content

Commit

Permalink
feat: add log.debug to send event and handle response (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyang1520 authored Jan 19, 2023
1 parent 656586a commit 23c485a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import com.amplitude.core.Amplitude
import com.amplitude.core.LoggerProvider

class AndroidLoggerProvider() : LoggerProvider {
private val logger: Logger by lazy {
LogcatLogger()
}

override fun getLogger(amplitude: Amplitude): Logger {
return LogcatLogger()
return logger
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.amplitude.android.utilities

import android.content.Context
import android.content.SharedPreferences
import com.amplitude.common.Logger
import com.amplitude.core.Amplitude
import com.amplitude.core.Configuration
import com.amplitude.core.EventCallBack
Expand All @@ -21,7 +22,8 @@ import java.io.File

class AndroidStorage(
context: Context,
apiKey: String
apiKey: String,
private val logger: Logger
) : Storage, EventsFileStorage {

companion object {
Expand Down Expand Up @@ -79,7 +81,8 @@ class AndroidStorage(
scope,
dispatcher,
events as String,
eventsString
eventsString,
logger
)
}

Expand All @@ -103,6 +106,10 @@ class AndroidStorage(
class AndroidStorageProvider : StorageProvider {
override fun getStorage(amplitude: Amplitude): Storage {
val configuration = amplitude.configuration as com.amplitude.android.Configuration
return AndroidStorage(configuration.context, configuration.apiKey)
return AndroidStorage(
configuration.context,
configuration.apiKey,
configuration.loggerProvider.getLogger(amplitude)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.amplitude.android.utilities

import android.app.Application
import com.amplitude.android.Amplitude
import com.amplitude.android.Configuration
import io.mockk.mockk
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class AndroidLoggerProviderTest {
@Test
fun androidLoggerProvider_getLogger_returnsSingletonInstance() {
val testApiKey = "test-123"
val context = mockk<Application>(relaxed = true)

val amplitude = Amplitude(Configuration(testApiKey, context = context!!))
val loggerProvider = AndroidLoggerProvider()
val logger1 = loggerProvider.getLogger(amplitude)
val logger2 = loggerProvider.getLogger(amplitude)
Assertions.assertEquals(logger1, logger2)
}
}
1 change: 1 addition & 0 deletions core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ open class Amplitude internal constructor(
event.timestamp = System.currentTimeMillis()
}

logger.debug("Logged event with type: ${event.eventType}")
timeline.process(event)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import com.amplitude.core.Amplitude
import com.amplitude.core.LoggerProvider

class ConsoleLoggerProvider() : LoggerProvider {
private val logger: Logger by lazy {
ConsoleLogger()
}

override fun getLogger(amplitude: Amplitude): Logger {
return ConsoleLogger()
return logger
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amplitude.core.utilities

import com.amplitude.common.Logger
import com.amplitude.core.Configuration
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.EventPipeline
Expand All @@ -16,10 +17,12 @@ class FileResponseHandler(
private val scope: CoroutineScope,
private val dispatcher: CoroutineDispatcher,
private val eventFilePath: String,
private val eventsString: String
private val eventsString: String,
private val logger: Logger?
) : ResponseHandler {

override fun handleSuccessResponse(successResponse: SuccessResponse) {
logger?.debug("Handle response, status: ${successResponse.status}")
val events: List<BaseEvent>
try {
events = JSONArray(eventsString).toEvents()
Expand All @@ -35,6 +38,7 @@ class FileResponseHandler(
}

override fun handleBadRequestResponse(badRequestResponse: BadRequestResponse) {
logger?.debug("Handle response, status: ${badRequestResponse.status}, error: ${badRequestResponse.error}")
val events: List<BaseEvent>
try {
events = JSONArray(eventsString).toEvents()
Expand Down Expand Up @@ -68,6 +72,7 @@ class FileResponseHandler(
}

override fun handlePayloadTooLargeResponse(payloadTooLargeResponse: PayloadTooLargeResponse) {
logger?.debug("Handle response, status: ${payloadTooLargeResponse.status}, error: ${payloadTooLargeResponse.error}")
val rawEvents: JSONArray
try {
rawEvents = JSONArray(eventsString)
Expand All @@ -91,14 +96,17 @@ class FileResponseHandler(
}

override fun handleTooManyRequestsResponse(tooManyRequestsResponse: TooManyRequestsResponse) {
logger?.debug("Handle response, status: ${tooManyRequestsResponse.status}, error: ${tooManyRequestsResponse.error}")
// wait for next time to pick it up
}

override fun handleTimeoutResponse(timeoutResponse: TimeoutResponse) {
logger?.debug("Handle response, status: ${timeoutResponse.status}")
// wait for next time to try again
}

override fun handleFailedResponse(failedResponse: FailedResponse) {
logger?.debug("Handle response, status: ${failedResponse.status}, error: ${failedResponse.error}")
// wait for next time to try again
}

Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/com/amplitude/core/utilities/FileStorage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amplitude.core.utilities

import com.amplitude.common.Logger
import com.amplitude.core.Amplitude
import com.amplitude.core.Configuration
import com.amplitude.core.EventCallBack
Expand All @@ -15,7 +16,8 @@ import java.io.BufferedReader
import java.io.File

class FileStorage(
private val apiKey: String
private val apiKey: String,
private val logger: Logger
) : Storage, EventsFileStorage {

companion object {
Expand Down Expand Up @@ -82,7 +84,8 @@ class FileStorage(
scope,
dispatcher,
events as String,
eventsString
eventsString,
logger
)
}

Expand All @@ -105,7 +108,10 @@ class FileStorage(

class FileStorageProvider : StorageProvider {
override fun getStorage(amplitude: Amplitude): Storage {
return FileStorage(amplitude.configuration.apiKey)
return FileStorage(
amplitude.configuration.apiKey,
amplitude.configuration.loggerProvider.getLogger(amplitude)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.amplitude.core.utilities

import com.amplitude.core.Configuration
import com.amplitude.core.utils.testAmplitude
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class ConsoleLoggerProviderTest {
@Test
fun `test singleton instance`() {
val testApiKey = "test-123"
val amplitude = testAmplitude(Configuration(testApiKey))
val loggerProvider = ConsoleLoggerProvider()
val logger1 = loggerProvider.getLogger(amplitude)
val logger2 = loggerProvider.getLogger(amplitude)
Assertions.assertEquals(logger1, logger2)
}
}

0 comments on commit 23c485a

Please sign in to comment.