Skip to content

Commit

Permalink
fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-nhs committed Jan 17, 2025
1 parent a67f4dc commit 1f20008
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
<artifactId>kotlin-logging-jvm</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>8.0</version>
</dependency>

</dependencies>
<pluginRepositories>
<pluginRepository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,28 @@ class ValidateController(
@RequestBody input: String,
@RequestHeader("x-request-id", required = false) requestId: String?
): String {
requestId?.let { logger.info { "started processing message $it" } }
val result = parseAndValidateResource(input)
requestId?.let { logger.info { "started processing message $it"} }
val result = parseAndValidateResource(input, requestId ?: "unknown_request_id")
requestId?.let { logger.info { "finished processing message $it"} }
return fhirContext.newJsonParser().encodeResourceToString(result)
}

fun parseAndValidateResource(input: String): OperationOutcome {
fun parseAndValidateResource(input: String, requestId: String): OperationOutcome {
return try {
val inputResource = fhirContext.newJsonParser().parseResource(input)
val resources = getResourcesToValidate(inputResource)
val operationOutcomeList = resources.map { validateResource(it) }
val operationOutcomeIssues = operationOutcomeList.filterNotNull().flatMap { it.issue }
return createOperationOutcome(operationOutcomeIssues)
} catch (e: DataFormatException) {
logger.error(e) { "Caught parser error" }
logger.atError {
message = "Caught parser error"
cause = e
payload = buildMap(capacity = 2) {
put("requestId", requestId)
put("payload", input)
}
}
createOperationOutcome(e.message ?: "Invalid JSON", null)
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/main/resources/log4j2.xml

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>

<root level="${LOG_LEVEL:-INFO}">
<appender-ref ref="jsonConsoleAppender" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class FhirExamplesTest {
fun testFhirExamples(exampleFile: File) {
val lines = exampleFile.bufferedReader().readLines()
val fileContent = lines.joinToString(" ")
val actualResult = testValidateController.parseAndValidateResource(fileContent)
val actualResult = testValidateController.parseAndValidateResource(fileContent, "dummy_request_id")
for (issue in actualResult.issue) {
if (issue.severity.equals(OperationOutcome.IssueSeverity.ERROR)) {
throw AssertionFailedError("Error found checking file ${exampleFile.absolutePath}. Error: ${issue.diagnostics}")
Expand Down

0 comments on commit 1f20008

Please sign in to comment.