Add Error Logging
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
Chaoscaot 2023-12-23 21:12:13 +01:00
Ursprung 9646ff0955
Commit dd7596e8ee
4 geänderte Dateien mit 21 neuen und 16 gelöschten Zeilen

Datei anzeigen

@ -30,7 +30,6 @@ dependencies {
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("io.ktor:ktor-server-host-common-jvm:$ktor_version")
implementation("io.ktor:ktor-server-call-logging-jvm:$ktor_version")
implementation("io.ktor:ktor-server-request-validation:$ktor_version")
implementation("io.ktor:ktor-server-auth:$ktor_version")
implementation("io.ktor:ktor-server-auth-jvm:$ktor_version")

Datei anzeigen

@ -19,11 +19,13 @@
package de.steamwar.plugins
import de.steamwar.sql.SWException
import de.steamwar.sql.SteamwarUser
import de.steamwar.sql.Token
import de.steamwar.sql.UserPerm
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.application.hooks.*
import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
@ -83,4 +85,21 @@ val SWPermissionCheck = createRouteScopedPlugin("SWAuth", ::SWAuthConfig) {
}
}
}
}
val ErrorLogger = createApplicationPlugin("SWLogger") {
on(CallFailed) { call, cause ->
val msg = """
{
URI: ${call.request.uri}
Method: ${call.request.httpMethod.value}
Headers: ${call.request.headers.entries().joinToString("\n ") { "${it.key}: ${it.value}" }}
Message: ${cause.message}
}
"""
SWException.log(msg, cause.stackTraceToString())
call.response.headers.append("X-Caught", "1")
}
}

Datei anzeigen

@ -24,13 +24,10 @@ import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.ratelimit.*
import io.ktor.server.request.*
import kotlinx.serialization.json.Json
import org.slf4j.event.*
import kotlin.time.Duration.Companion.seconds
fun Application.configurePlugins() {
@ -77,15 +74,5 @@ fun Application.configurePlugins() {
install(ContentNegotiation) {
json(Json)
}
install(CallLogging) {
level = Level.INFO
format {
val verified = it.principal<SWAuthPrincipal>()
if (verified != null) {
"User (${it.request.local.remoteAddress}): ${verified.token.owner.userName}, Token: ${verified.token.name}, ${it.request.httpMethod.value} ${it.request.uri}, Response: ${it.response.status()?.value}, User-Agent: ${it.request.headers["User-Agent"] ?: "Unknown"}"
} else {
"Unauthenticated Request (${it.request.local.remoteAddress}): ${it.request.httpMethod.value} ${it.request.uri}, Response: ${it.response.status()?.value}, User-Agent: ${it.request.headers["User-Agent"] ?: "Unknown"}"
}
}
}
install(ErrorLogger)
}

Datei anzeigen

@ -63,6 +63,6 @@ class SQLWrapperImpl: SQLWrapper {
override fun loadSchemTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: MutableMap<String, SchematicType>?) = loadSchematicTypes(tmpTypes, tmpFromDB)
override fun additionalExceptionMetadata(builder: StringBuilder) {
builder.append("EventAPI")
builder.append("\n\nWebsiteApi")
}
}