Refactor logging

This commit is contained in:
2025-04-28 02:16:27 +03:00
parent a1ad81d4e5
commit ac19fefb6b
5 changed files with 48 additions and 36 deletions

View File

@@ -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<String, String?> =
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(),
)
}

View File

@@ -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" }
}
}

View File

@@ -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<List<PhotoSize>>) {
if (shouldForwardMessage(env)) {
forwardPhotoMessage(env)
}
reactToMessage(this, "👀")
reactToMessage(env, "👀")
}
private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): 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" } }
}
}

View File

@@ -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" }
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" } }
}
}

View File

@@ -0,0 +1,5 @@
package com.pischule.memestv.util
import com.github.kotlintelegrambot.entities.Message
fun Message.getMaxResPhotoId(): String? = this.replyToMessage?.photo?.lastOrNull()?.fileId