[+] Add error logging

pull/98/head
Azalea 2025-01-03 01:45:42 -05:00
parent a1be699ec5
commit 256aac8faf
2 changed files with 32 additions and 3 deletions

View File

@ -6,6 +6,8 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import jakarta.persistence.Query
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.apache.tika.Tika
@ -44,6 +46,19 @@ typealias JavaSerializable = java.io.Serializable
typealias JDict = Map<String, Any?>
typealias MutJDict = MutableMap<String, Any?>
fun HttpServletRequest.details() = mapOf(
"method" to method,
"uri" to requestURI,
"query" to queryString,
"remote" to remoteAddr,
"headers" to headerNames.asSequence().associateWith { getHeader(it) }
)
fun HttpServletResponse.details() = mapOf(
"status" to status,
"headers" to headerNames.asSequence().associateWith { getHeader(it) },
)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER)
@Retention(AnnotationRetention.RUNTIME)
annotation class Doc(

View File

@ -1,6 +1,8 @@
package icu.samnyan.aqua.sega.general.filter
import ext.details
import ext.logger
import ext.toJson
import icu.samnyan.aqua.sega.util.ZLib
import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpServletRequest
@ -35,9 +37,21 @@ class CompressionFilter : OncePerRequestFilter() {
}
// Handle request
val result = ContentCachingResponseWrapper(resp).run {
chain.doFilter(CompressRequestWrapper(req, reqSrc), this)
ZLib.compress(contentAsByteArray).let { if (isDfi) b64e.encode(it) else it }
val respW = ContentCachingResponseWrapper(resp)
val result = try {
chain.doFilter(CompressRequestWrapper(req, reqSrc), respW)
ZLib.compress(respW.contentAsByteArray).let { if (isDfi) b64e.encode(it) else it }
} finally {
if (respW.status != 200) {
val details = mapOf(
"req" to req.details(),
"resp" to respW.details(),
"body" to reqSrc.toString(Charsets.UTF_8),
"result" to respW.contentAsByteArray.toString(Charsets.UTF_8)
).toJson()
log.error("HTTP ${respW.status}: $details")
}
}
// Write response