From ac19fefb6b45b14810f64bc0943d08c0e419b74d Mon Sep 17 00:00:00 2001 From: Maksim Pischulenok Date: Mon, 28 Apr 2025 02:16:27 +0300 Subject: [PATCH] Refactor logging --- .../pischule/memestv/bot/BotConfiguration.kt | 31 +++++++++++-------- .../com/pischule/memestv/bot/BotService.kt | 6 ++-- .../bot/handler/PhotoHandlerService.kt | 17 +++++----- .../bot/handler/ThisCommandHandlerService.kt | 25 ++++++++------- .../pischule/memestv/util/TelegramHelper.kt | 5 +++ 5 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 src/main/kotlin/com/pischule/memestv/util/TelegramHelper.kt diff --git a/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt b/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt index 565e3ce..1ef0db0 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt @@ -5,15 +5,17 @@ import com.github.kotlintelegrambot.bot import com.github.kotlintelegrambot.dispatch import com.github.kotlintelegrambot.dispatcher.message import com.github.kotlintelegrambot.dispatcher.photos +import com.github.kotlintelegrambot.entities.Message import com.pischule.memestv.bot.handler.PhotoHandlerService import com.pischule.memestv.bot.handler.ThisCommandHandlerService +import com.pischule.memestv.util.getMaxResPhotoId import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.withLoggingContext import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration -private val log = KotlinLogging.logger {} +private val logger = KotlinLogging.logger {} @EnableConfigurationProperties(BotProps::class) @Configuration @@ -29,30 +31,33 @@ class BotConfiguration( token = botProps.token dispatch { message { - withLoggingContext( - "messageId" to this.message.messageId.toString(), - "chatId" to this.message.chat.id.toString(), - ) { + withLoggingContext(messageContext(message)) { try { - thisCommandHandlerService.create().invoke(this) + thisCommandHandlerService.create(this) } catch (e: Error) { - log.error(e) { "Error while handling message" } + logger.error(e) { "Error while handling message" } } } } photos { - withLoggingContext( - "messageId" to this.message.messageId.toString(), - "chatId" to this.message.chat.id.toString(), - ) { + withLoggingContext(messageContext(message)) { try { - photoHandlerService.create().invoke(this) + photoHandlerService.create(this) } catch (e: Error) { - log.error(e) { "Error while handling photo" } + logger.error(e) { "Error while handling photo" } } } } } } } + + private fun messageContext(message: Message): Map = + mapOf( + "message_id" to message.messageId.toString(), + "chat_id" to message.chat.id.toString(), + "from_user_id" to message.from?.id.toString(), + "from_user_username" to message.from?.username.toString(), + "file_id" to message.getMaxResPhotoId(), + ) } diff --git a/src/main/kotlin/com/pischule/memestv/bot/BotService.kt b/src/main/kotlin/com/pischule/memestv/bot/BotService.kt index ad1aebf..67047c1 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/BotService.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/BotService.kt @@ -6,7 +6,7 @@ import jakarta.annotation.PostConstruct import jakarta.annotation.PreDestroy import org.springframework.stereotype.Service -private val log = KotlinLogging.logger {} +private val logger = KotlinLogging.logger {} @Service class BotService(private val bot: Bot) { @@ -14,12 +14,12 @@ class BotService(private val bot: Bot) { @PostConstruct fun start() { Thread { bot.startPolling() }.start() - log.info { "Initialized bot" } + logger.info { "Initialized bot" } } @PreDestroy fun stop() { bot.stopPolling() - log.info { "Stopped bot" } + logger.info { "Stopped bot" } } } diff --git a/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt b/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt index cfd73db..0c4ee2e 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt @@ -1,6 +1,5 @@ package com.pischule.memestv.bot.handler -import com.github.kotlintelegrambot.dispatcher.handlers.HandlePhotos import com.github.kotlintelegrambot.dispatcher.handlers.media.MediaHandlerEnvironment import com.github.kotlintelegrambot.entities.ChatId import com.github.kotlintelegrambot.entities.files.PhotoSize @@ -9,17 +8,17 @@ import com.pischule.memestv.bot.BotProps import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Service -private val log = KotlinLogging.logger {} +private val logger = KotlinLogging.logger {} @Service class PhotoHandlerService(private val botProps: BotProps) { - fun create(): HandlePhotos = { - if (shouldForwardMessage(this)) { - forwardPhotoMessage(this) + suspend fun create(env: MediaHandlerEnvironment>) { + if (shouldForwardMessage(env)) { + forwardPhotoMessage(env) } - reactToMessage(this, "👀") + reactToMessage(env, "👀") } private fun shouldForwardMessage(env: MediaHandlerEnvironment>): Boolean { @@ -34,8 +33,8 @@ class PhotoHandlerService(private val botProps: BotProps) { messageId = env.message.messageId, ) .fold( - { log.info { "Forwarded picture message: $it" } }, - { log.error { "Failed to forward message: $it" } }, + { logger.atInfo { "Forwarded picture message: $it" } }, + { logger.error { "Failed to forward message: $it" } }, ) } @@ -49,6 +48,6 @@ class PhotoHandlerService(private val botProps: BotProps) { messageId = env.message.messageId, reaction = listOf(ReactionType.Emoji(emoji)), ) - .onError { error -> log.atWarn { "Failed to react to message: $error" } } + .onError { error -> logger.atWarn { "Failed to react to message: $error" } } } } diff --git a/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt b/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt index 54cc344..8e82e24 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt @@ -1,33 +1,36 @@ package com.pischule.memestv.bot.handler -import com.github.kotlintelegrambot.dispatcher.handlers.HandleMessage import com.github.kotlintelegrambot.dispatcher.handlers.MessageHandlerEnvironment import com.github.kotlintelegrambot.entities.ChatId import com.github.kotlintelegrambot.entities.reaction.ReactionType import com.pischule.memestv.bot.BotProps import com.pischule.memestv.s3.FileUploaderService +import com.pischule.memestv.util.getMaxResPhotoId import io.github.oshai.kotlinlogging.KotlinLogging +import io.github.oshai.kotlinlogging.withLoggingContext import org.springframework.stereotype.Component -private val log = KotlinLogging.logger {} +private val logger = KotlinLogging.logger {} @Component class ThisCommandHandlerService( private val botProps: BotProps, private val fileUploaderService: FileUploaderService, ) { - fun create(): HandleMessage = HandleMessage@{ - if (!shouldHandleMessage(this)) return@HandleMessage + suspend fun create(env: MessageHandlerEnvironment) { + if (!shouldHandleMessage(env)) return - val maxResPhotoId = message.replyToMessage!!.photo!!.last().fileId - val fileBytes = bot.downloadFileBytes(maxResPhotoId) ?: return@HandleMessage + val maxResPhotoId = env.message.replyToMessage?.getMaxResPhotoId() ?: return - log.info { "Downloaded a file $maxResPhotoId from Telegram" } + withLoggingContext("file_id" to maxResPhotoId) { + val fileBytes = env.bot.downloadFileBytes(maxResPhotoId) ?: return + logger.info { "Downloaded a file from Telegram" } - fileUploaderService.uploadFile(fileBytes) - log.info { "Uploaded a file $maxResPhotoId to S3" } + fileUploaderService.uploadFile(fileBytes) + logger.info { "Uploaded a file to S3" } - reactToMessage(this, "👍") + reactToMessage(env, "👍") + } } private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean { @@ -44,6 +47,6 @@ class ThisCommandHandlerService( messageId = env.message.messageId, reaction = listOf(ReactionType.Emoji(emoji)), ) - .onError { error -> log.warn { "Failed to react to message: $error" } } + .onError { error -> logger.warn { "Failed to react to message: $error" } } } } diff --git a/src/main/kotlin/com/pischule/memestv/util/TelegramHelper.kt b/src/main/kotlin/com/pischule/memestv/util/TelegramHelper.kt new file mode 100644 index 0000000..6b7cd2a --- /dev/null +++ b/src/main/kotlin/com/pischule/memestv/util/TelegramHelper.kt @@ -0,0 +1,5 @@ +package com.pischule.memestv.util + +import com.github.kotlintelegrambot.entities.Message + +fun Message.getMaxResPhotoId(): String? = this.replyToMessage?.photo?.lastOrNull()?.fileId