add logging inside of plagubot

This commit is contained in:
InsanusMokrassar 2022-05-16 14:08:45 -04:00
parent 6b2b0b07f5
commit 8aaaa86dd4

View File

@ -16,6 +16,8 @@ import org.koin.core.context.GlobalContext
import org.koin.core.module.Module import org.koin.core.module.Module
import org.koin.core.scope.Scope import org.koin.core.scope.Scope
import org.koin.dsl.module import org.koin.dsl.module
import java.util.logging.Level
import java.util.logging.Logger
val Scope.plagubot: PlaguBot val Scope.plagubot: PlaguBot
get() = get() get() = get()
@ -25,6 +27,8 @@ data class PlaguBot(
private val json: JsonObject, private val json: JsonObject,
private val config: Config private val config: Config
) : Plugin { ) : Plugin {
@Transient
private val logger = Logger.getLogger("PlaguBot")
@Transient @Transient
private val bot = telegramBot(config.botToken) private val bot = telegramBot(config.botToken)
@ -37,21 +41,29 @@ data class PlaguBot(
single { this@PlaguBot } single { this@PlaguBot }
includes( includes(
config.plugins.map { config.plugins.mapNotNull {
runCatching {
module { module {
with(it) { with(it) {
setupDI(database, params) setupDI(database, params)
} }
} }
}.onFailure { e ->
logger.log(Level.WARNING, "Unable to load DI part of $it", e)
}.getOrNull()
} }
) )
} }
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
config.plugins.forEach { config.plugins.forEach {
runCatching {
with(it) { with(it) {
setupBotPlugin(koin) setupBotPlugin(koin)
} }
}.onFailure { e ->
logger.log(Level.WARNING, "Unable to load bot part of $it", e)
}
} }
} }
@ -61,19 +73,26 @@ data class PlaguBot(
suspend fun start( suspend fun start(
scope: CoroutineScope = CoroutineScope(Dispatchers.IO) scope: CoroutineScope = CoroutineScope(Dispatchers.IO)
): Job { ): Job {
logger.info("Start initialization")
val koinApp = KoinApplication.init() val koinApp = KoinApplication.init()
koinApp.modules( koinApp.modules(
module { module {
setupDI(config.databaseConfig.database, json) setupDI(config.databaseConfig.database, json)
} }
) )
logger.info("Modules loaded")
GlobalContext.startKoin(koinApp) GlobalContext.startKoin(koinApp)
logger.info("Koin started")
lateinit var behaviourContext: BehaviourContext lateinit var behaviourContext: BehaviourContext
bot.buildBehaviour(scope = scope) { bot.buildBehaviour(scope = scope) {
logger.info("Start setup of bot part")
behaviourContext = this behaviourContext = this
setupBotPlugin(koinApp.koin) setupBotPlugin(koinApp.koin)
deleteWebhook() deleteWebhook()
} }
return bot.startGettingOfUpdatesByLongPolling(scope = behaviourContext, updatesFilter = behaviourContext) logger.info("Behaviour builder has been setup")
return bot.startGettingOfUpdatesByLongPolling(scope = behaviourContext, updatesFilter = behaviourContext).also {
logger.info("Long polling has been started")
}
} }
} }