-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* logback integration * make exception not null for protocol * use HubAdapter * fix test * reflect review
- Loading branch information
Showing
20 changed files
with
285 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
rootProject.name = "truffle-kotlin" | ||
|
||
include("truffle-core") | ||
include("truffle-logback") | ||
include("truffle-spring-boot-starter") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.wafflestudio.truffle.sdk.core | ||
|
||
import com.wafflestudio.truffle.sdk.core.protocol.TruffleEvent | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
internal class Hub( | ||
truffleOptions: TruffleOptions, | ||
webClientBuilder: WebClient.Builder? = null, | ||
) : IHub { | ||
private val client: TruffleClient = DefaultTruffleClient( | ||
apiKey = truffleOptions.apiKey, | ||
webClientBuilder = webClientBuilder ?: WebClient.builder(), | ||
) | ||
|
||
override fun captureEvent(truffleEvent: TruffleEvent) { | ||
client.sendEvent(truffleEvent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.wafflestudio.truffle.sdk.core | ||
|
||
import com.wafflestudio.truffle.sdk.core.protocol.TruffleEvent | ||
|
||
interface IHub { | ||
fun captureEvent(truffleEvent: TruffleEvent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.wafflestudio.truffle.sdk.core | ||
|
||
import com.wafflestudio.truffle.sdk.core.protocol.TruffleEvent | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
object Truffle { | ||
private lateinit var hub: IHub | ||
|
||
internal object HubAdapter : IHub { | ||
override fun captureEvent(truffleEvent: TruffleEvent) { | ||
Truffle.captureEvent(truffleEvent) | ||
} | ||
} | ||
|
||
// for modules without access WebClient.Builder | ||
fun init(truffleOptions: TruffleOptions): IHub { | ||
return init(truffleOptions, null) | ||
} | ||
|
||
@Synchronized fun init(truffleOptions: TruffleOptions, webClientBuilder: WebClient.Builder? = null): IHub { | ||
if (::hub.isInitialized) { | ||
return hub | ||
} | ||
|
||
validateConfig(truffleOptions) | ||
|
||
this.hub = Hub(truffleOptions, webClientBuilder) | ||
return HubAdapter | ||
} | ||
|
||
private fun captureEvent(truffleEvent: TruffleEvent) { | ||
hub.captureEvent(truffleEvent) | ||
} | ||
|
||
private fun validateConfig(truffleOptions: TruffleOptions) { | ||
if (truffleOptions.apiKey.isBlank()) { | ||
throw IllegalArgumentException("Truffle API key is blank") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.wafflestudio.truffle.sdk.core | ||
|
||
import ch.qos.logback.classic.Level | ||
|
||
open class TruffleOptions { | ||
/** | ||
* Truffle 서버에서 애플리케이션의 요청이 유효한지 검증하는 데에 사용하는 API key. | ||
* | ||
* 외부에 공개되지 않도록 주의해 관리해야 합니다. | ||
*/ | ||
open lateinit var apiKey: String | ||
|
||
/** | ||
* truffle logback 사용 시 이벤트를 전송할 최소 로그 레벨. | ||
*/ | ||
open var minimumLevel: Level = Level.ERROR | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.wafflestudio.truffle.sdk.core.protocol | ||
|
||
object TruffleAppInfo { | ||
val runtime = TruffleRuntime() | ||
|
||
data class TruffleRuntime( | ||
val name: String = "Java", | ||
val version: String = System.getProperty("java.version") | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.wafflestudio.truffle.sdk.core.protocol | ||
|
||
import ch.qos.logback.classic.Level | ||
|
||
enum class TruffleLevel { | ||
DEBUG, | ||
INFO, | ||
WARNING, | ||
ERROR, | ||
FATAL, | ||
; | ||
|
||
companion object { | ||
fun from(level: Level): TruffleLevel { | ||
return when { | ||
level.isGreaterOrEqual(Level.ERROR) -> ERROR | ||
level.isGreaterOrEqual(Level.WARN) -> WARNING | ||
level.isGreaterOrEqual(Level.INFO) -> INFO | ||
else -> DEBUG | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dependencies { | ||
compileOnly("ch.qos.logback:logback-classic") | ||
|
||
implementation(project(":truffle-core")) | ||
} |
53 changes: 53 additions & 0 deletions
53
truffle-logback/src/main/kotlin/appender/TruffleAppender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.wafflestudio.truffle.sdk.logback | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent | ||
import ch.qos.logback.classic.spi.ThrowableProxy | ||
import ch.qos.logback.core.UnsynchronizedAppenderBase | ||
import com.wafflestudio.truffle.sdk.core.IHub | ||
import com.wafflestudio.truffle.sdk.core.Truffle | ||
import com.wafflestudio.truffle.sdk.core.TruffleOptions | ||
import com.wafflestudio.truffle.sdk.core.protocol.TruffleEvent | ||
import com.wafflestudio.truffle.sdk.core.protocol.TruffleException | ||
import com.wafflestudio.truffle.sdk.core.protocol.TruffleLevel | ||
|
||
class TruffleAppender : UnsynchronizedAppenderBase<ILoggingEvent>() { | ||
lateinit var options: TruffleOptions | ||
private lateinit var hub: IHub | ||
|
||
override fun start() { | ||
hub = Truffle.init(options) | ||
super.start() | ||
} | ||
|
||
override fun append(eventObject: ILoggingEvent) { | ||
if (eventObject.level.isGreaterOrEqual(options.minimumLevel) && | ||
!eventObject.loggerName.startsWith("com.wafflestudio.truffle.sdk") | ||
) { | ||
val truffleEvent = createEvent(eventObject) | ||
hub.captureEvent(truffleEvent) | ||
} | ||
} | ||
|
||
private fun createEvent(eventObject: ILoggingEvent): TruffleEvent { | ||
val exception = eventObject.throwableProxy?.let { | ||
TruffleException((it as ThrowableProxy).throwable) | ||
} ?: TruffleException( | ||
className = eventObject.loggerName, | ||
message = eventObject.formattedMessage, | ||
elements = eventObject.callerData.map { | ||
TruffleException.Element( | ||
className = it.className, | ||
methodName = it.methodName, | ||
fileName = it.fileName ?: "", | ||
lineNumber = it.lineNumber, | ||
isInAppInclude = true, // FIXME | ||
) | ||
}, | ||
) | ||
|
||
return TruffleEvent( | ||
level = TruffleLevel.from(eventObject.level), | ||
exception = exception, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
truffle-spring-boot-starter/src/main/kotlin/TruffleLogbackInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.wafflestudio.truffle.sdk | ||
|
||
import ch.qos.logback.classic.LoggerContext | ||
import com.wafflestudio.truffle.sdk.logback.TruffleAppender | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.context.ApplicationEvent | ||
import org.springframework.context.event.ContextRefreshedEvent | ||
import org.springframework.context.event.GenericApplicationListener | ||
import org.springframework.core.ResolvableType | ||
import ch.qos.logback.classic.Logger as LogbackLogger | ||
|
||
class TruffleLogbackInitializer( | ||
private val truffleProperties: TruffleProperties, | ||
) : GenericApplicationListener { | ||
override fun supportsEventType(eventType: ResolvableType): Boolean { | ||
return eventType.rawClass?.let { ContextRefreshedEvent::class.java.isAssignableFrom(it) } ?: false | ||
} | ||
|
||
override fun onApplicationEvent(event: ApplicationEvent) { | ||
val rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as LogbackLogger | ||
|
||
if (!isTruffleAppenderRegistered(rootLogger)) { | ||
val truffleAppender = TruffleAppender() | ||
truffleAppender.name = "TRUFFLE_APPENDER" | ||
truffleAppender.context = LoggerFactory.getILoggerFactory() as LoggerContext | ||
truffleAppender.options = truffleProperties | ||
|
||
truffleAppender.start() | ||
rootLogger.addAppender(truffleAppender) | ||
} | ||
} | ||
|
||
private fun isTruffleAppenderRegistered(logger: LogbackLogger): Boolean { | ||
val iterator = logger.iteratorForAppenders() | ||
while (iterator.hasNext()) { | ||
if (iterator.next() is TruffleAppender) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} |
Oops, something went wrong.