Skip to content

Commit

Permalink
fix: STUD-358 Initial setup for logging instance id
Browse files Browse the repository at this point in the history
  • Loading branch information
saif-software-developer authored and AndrewWestberg committed Nov 29, 2024
1 parent a18eddf commit 97a545f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion newm-server/src/main/kotlin/io/newm/server/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.newm.server.features.user.createUserRoutes
import io.newm.server.features.walletconnection.createWalletConnectionRoutes
import io.newm.server.forwarder.installForwarder
import io.newm.server.health.installHealthCheck
import io.newm.server.logging.initializeLogging
import io.newm.server.logging.initializeSentry
import io.newm.server.logging.installCallLogging
import io.newm.server.staticcontent.createStaticContentRoutes
Expand Down Expand Up @@ -58,11 +59,11 @@ fun main(args: Array<String>) {

@Suppress("unused")
fun Application.module() {
initializeLogging()
initializeSentry()
installDependencyInjection()
initializeDatabase()
installCurator()

installCallLogging()
installContentNegotiation()
installAuthentication()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.newm.server.logging

import io.ktor.server.application.Application
import io.ktor.server.application.log
import org.slf4j.MDC
import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils

fun Application.initializeLogging() {
val instanceId = try {
EC2MetadataUtils.getInstanceId()
} catch (e: Throwable) {
log.error("Failed to get instanceId from EC2MetadataUtils", e)
"instanceId-unknown"
}
MDC.put("instanceId", instanceId)
}
2 changes: 1 addition & 1 deletion newm-server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%X{instanceId}][%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="Sentry" class="io.sentry.logback.SentryAppender">
Expand Down

0 comments on commit 97a545f

Please sign in to comment.