theMackabu
05/03/2023, 3:59 PMval FileLogger = createApplicationPlugin(name = "FileLogger", createConfiguration = ::PluginConfiguration) {
val path = pluginConfig.path
val logFile = File(path)
val gson = Gson()
onCall { call ->
val uri = call.request.uri
val status = call.response.status()
val httpMethod = call.request.httpMethod.value
val userAgent = call.request.headers["User-Agent"]
val timestamp = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val logEntry = gson.toJson(object: LogMessage {
override var timestamp = "$timestamp"
override var status = "$status"
override var method = "$httpMethod"
override var path = "$uri"
override var userAgent = "$userAgent"
})
logFile.appendText("$logEntry\n")
}
}
everything else is populated but status is always null
(side note, is it possible to report error messages here?)Rustam Siniukov
05/03/2023, 4:02 PMonCall { ... }
is called before routing, so you probably haven’t called call.respond(...)
yet. try to change it to onCallRespond { ... }
theMackabu
05/03/2023, 4:02 PMtheMackabu
05/03/2023, 4:06 PMHttpStatusCode.OK
in their call.respond
it is not null now, but routes without a set status are nulltheMackabu
05/03/2023, 4:06 PMRustam Siniukov
05/03/2023, 4:17 PMon(ResponseSent) { ... }
is what you need. But why don’t you just config CallLogging
to write to file in json format?Rustam Siniukov
05/03/2023, 4:23 PMtheMackabu
05/03/2023, 4:40 PM