Skip to content

Commit

Permalink
Params 추가 (#25)
Browse files Browse the repository at this point in the history
* test: 버전 설정 테스트

* chore: 에러 메시지 수정

* feat: Params 추가

* chore: YLS 오타
  • Loading branch information
cometj03 authored Feb 26, 2024
1 parent 28d4ba7 commit 4b2480e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deployLibraryYLS.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# YLS 라이브러리를 깃헙에 배포하는 워크플로입니다.
# 깃허브에 배포되면 자동으로 jitpack에 등록됩니다. (https://jitpack.io/#yourssu/YLS-Android)
name: Deploy YDS Library
name: Deploy YLS Library

# Github에서 수동으로 실행해야 합니다.
on: workflow_dispatch
Expand Down
10 changes: 10 additions & 0 deletions yls/src/main/kotlin/com/yourssu/logging/system/Params.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yourssu.logging.system

@JvmInline
value class Params(internal val value: Map<String, Any>) {
constructor(vararg params: Pair<String, Any>) : this(params.toMap())
}

fun Map<String, Any>.toParams(): Params = Params(this)

fun Pair<String, Any>.toParams(): Params = Params(this)
42 changes: 31 additions & 11 deletions yls/src/main/kotlin/com/yourssu/logging/system/YLS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ class YLS private constructor() {
}

companion object Facade : Logger() {
private const val DEFAULT_VERSION = 1
internal const val DEFAULT_VERSION = 1
private var version: Int = DEFAULT_VERSION
get() {
return field.also { field = DEFAULT_VERSION }
}
get() = field.also { field = DEFAULT_VERSION }

private lateinit var logger: Logger
private lateinit var defaultEvent: Map<String, Any>
private lateinit var hashedUserId: String
private var defaultEvent: Map<String, Any> = emptyMap()

private var params: Params? = null
get() = field.also { field = null }

/**
* YLS 초기화. 앱의 Application.onCreate()에서 초기화하는 것을 권장합니다.
Expand Down Expand Up @@ -147,12 +148,26 @@ class YLS private constructor() {
return this
}

fun params(params: Params): YLS.Facade {
this.params = params
return this
}

fun createLog(eventMap: Map<String, Any>): YLSEventData {
if (!::hashedUserId.isInitialized) {
throw AssertionError(
"Not initialized! : userId가 초기화되지 않았습니다.\n" +
"YLS.setUserId() 또는 YLS.init()을 먼저 호출해주세요.",
)
}
val maybeParams = this.params.let {
if (it == null) emptyMap() else mapOf("params" to it.value)
}
return YLSEventData(
hashedId = hashedUserId,
timestamp = getTimestamp(),
version = this.version,
event = defaultEvent + eventMap,
event = defaultEvent + eventMap + maybeParams,
)
}

Expand All @@ -165,22 +180,27 @@ class YLS private constructor() {
* @param events 이벤트 key-value 쌍
*/
fun log(vararg events: Pair<String, Any>) {
if (!::logger.isInitialized) {
throw AssertionError("Not initialized! : YLS.init()을 먼저 호출해 주세요.")
}

val eventData = createLog(events.toMap())
log(eventData)
}

fun log(eventData: YLSEventData) {
if (!::logger.isInitialized) {
throw AssertionError(
"Not initialized! : logger가 초기화되지 않았습니다.\n" +
"YLS.setLogger() 또는 YLS.init()을 먼저 호출해 주세요.",
)
}
logger.enqueue(eventData)
}

/** Logger에 남아있는 로그 데이터를 모두 내보낸 후 큐를 비웁니다. */
override fun flush() {
if (!::logger.isInitialized) {
throw AssertionError("Not initialized! : YLS.init()을 먼저 호출해 주세요.")
throw AssertionError(
"Not initialized! : logger가 초기화되지 않았습니다.\n" +
"YLS.setLogger() 또는 YLS.init()을 먼저 호출해 주세요.",
)
}
logger.flush()
}
Expand Down
17 changes: 12 additions & 5 deletions yls/src/test/kotlin/com/yourssu/logging/system/LoggingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,27 @@ class LoggingTest {
@Test
fun ylsDebugClickLogging() {
// 버튼 클릭 이벤트
YLS.log(
"event" to "ButtonClicked",
"screen" to "LoginScreen",
)
YLS.version(123)
.params(Params("likeCount" to 10))
.log("screen" to "LoginScreen")

val e = testLogger.lastEventData
if (e != null) {
assertEquals(YLS.hashString("abc"), e.hashedId)
assertEquals("android", e.event["platform"])
assertEquals("ButtonClicked", e.event["event"])
assertEquals("LoginScreen", e.event["screen"])
assertEquals(123, e.version)
assertEquals(mapOf("likeCount" to 10), e.event["params"])
} else {
assertTrue(false) // always fail
}

// 설정한 버전과 params는 설정할 때 한 번만 들어가고
// 각각 DEFAULT_VERSION과 null로 초기화된다.
YLS.log()
val e2 = testLogger.lastEventData
assertEquals(YLS.DEFAULT_VERSION, e2!!.version)
assertEquals(null, e2.event["params"])
}

@Test
Expand Down

0 comments on commit 4b2480e

Please sign in to comment.