From 4f8c0d9ef2b599cd724b6d4c13a08a2c1cbb7481 Mon Sep 17 00:00:00 2001 From: Tommy Schmidt Date: Tue, 28 May 2024 21:38:17 +0200 Subject: [PATCH] feat: improve logs and timestamp format for `/get-server-details` (#120) --- pom.xml | 2 +- .../discord/commands/GetServerDetailsCommand.kt | 2 +- .../migration/DatabaseMigrationService.kt | 17 +++++++++++++++++ .../serverstatus/ServerStatusMonitorService.kt | 8 +++++++- .../vrising/discord/serverstatus/model/Error.kt | 2 +- src/main/resources/application.yml | 2 ++ .../migration/DatabaseMigrationServiceTest.kt | 5 +++-- 7 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 7c9f6a4..4ec3d8d 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ de.darkatra v-rising-discord-bot - 2.9.5 + 2.10.0 jar diff --git a/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt b/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt index cd37ceb..bfc5efb 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt @@ -160,7 +160,7 @@ class GetServerDetailsCommand( value = when (serverStatusMonitor.recentErrors.isEmpty()) { true -> "-" false -> serverStatusMonitor.recentErrors.joinToString("\n") { - "${it.timestamp}```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```" + "```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```" } } } diff --git a/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt b/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt index 4e39757..28336ae 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt @@ -2,6 +2,7 @@ package de.darkatra.vrising.discord.migration import de.darkatra.vrising.discord.serverstatus.model.ServerStatusMonitor import de.darkatra.vrising.discord.serverstatus.model.ServerStatusMonitorStatus +import org.dizitart.no2.Document import org.dizitart.no2.Nitrite import org.dizitart.no2.objects.filters.ObjectFilters import org.dizitart.no2.util.ObjectUtils @@ -78,6 +79,22 @@ class DatabaseMigrationService( documentAction = { document -> document["embedEnabled"] = true } + ), + DatabaseMigration( + description = "Serialize error timestamp as long (epochSecond).", + isApplicable = { currentSchemaVersion -> currentSchemaVersion.major < 2 || (currentSchemaVersion.major == 2 && currentSchemaVersion.minor <= 9) }, + documentAction = { document -> + val recentErrors = document["recentErrors"] + if (recentErrors is List<*>) { + recentErrors.filterIsInstance().forEach { error -> + if (error["timestamp"] is String) { + error["timestamp"] = Instant.parse(error["timestamp"] as String).epochSecond + } + } + } else { + document["recentErrors"] = emptyList() + } + } ) ) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt index 7d9c616..f82d8b4 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt @@ -17,6 +17,7 @@ import dev.kord.core.exception.EntityNotFoundException import dev.kord.rest.builder.message.EmbedBuilder import dev.kord.rest.builder.message.embed import org.slf4j.LoggerFactory +import org.slf4j.MDC import org.springframework.stereotype.Service import java.time.Instant import java.time.temporal.ChronoUnit @@ -32,6 +33,9 @@ class ServerStatusMonitorService( suspend fun updateServerStatusMonitors(kord: Kord) { serverStatusMonitorRepository.getServerStatusMonitors(status = ServerStatusMonitorStatus.ACTIVE).forEach { serverStatusMonitor -> + + MDC.put("server-status-monitor-id", serverStatusMonitor.id) + updateServerStatusMonitor(kord, serverStatusMonitor) updatePlayerActivityFeed(kord, serverStatusMonitor) updatePvpKillFeed(kord, serverStatusMonitor) @@ -40,6 +44,8 @@ class ServerStatusMonitorService( } catch (e: OutdatedServerStatusMonitorException) { logger.debug("Server status monitor was updated or deleted by another thread. Will ignore this exception and proceed as usual.", e) } + + MDC.clear() } } @@ -110,7 +116,7 @@ class ServerStatusMonitorService( add( Error( message = "${e::class.simpleName}: ${e.message}", - timestamp = Instant.now().toString() + timestamp = Instant.now().epochSecond ) ) } diff --git a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/model/Error.kt b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/model/Error.kt index a2a8aae..65679ad 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/model/Error.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/model/Error.kt @@ -2,5 +2,5 @@ package de.darkatra.vrising.discord.serverstatus.model data class Error( val message: String, - val timestamp: String + val timestamp: Long ) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7f88fae..403e5ae 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,5 +7,7 @@ logging: level: root: info com.ibasco.agql: warn + pattern: + console: "%clr(%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}){faint} %clr(%5p) %clr(${PID:-}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m %mdc%n%wEx" version: @project.version@ diff --git a/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt b/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt index 526ac4c..49417d7 100644 --- a/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt +++ b/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt @@ -48,16 +48,17 @@ class DatabaseMigrationServiceTest { repository.insert(Schema(appVersion = "V2.2.0")) repository.insert(Schema(appVersion = "V2.3.0")) repository.insert(Schema(appVersion = "V2.9.0")) + repository.insert(Schema(appVersion = "V2.10.0")) val databaseMigrationService = DatabaseMigrationService( database = database, - appVersionFromPom = "2.9.0" + appVersionFromPom = "2.10.0" ) assertThat(databaseMigrationService.migrateToLatestVersion()).isFalse() val schemas = repository.find().toList() - assertThat(schemas).hasSize(7) + assertThat(schemas).hasSize(8) } @Test