From 75b0a6acff5406cc134f5927992c23176d67d707 Mon Sep 17 00:00:00 2001 From: Michal Somora Date: Fri, 13 Oct 2023 13:53:49 +0200 Subject: [PATCH] feat: tkit.log.cdi.mdc.errorKey | java sources --- extensions/log/cdi/README.md | 2 ++ .../log/cdi/test/ErrorDataServiceTest.java | 12 +++++++++++- .../test/app/DummyLogFriendlyException.java | 18 ++++++++++++++++++ .../log/cdi/test/app/ErrorDataService.java | 4 ++++ .../src/test/resources/default.properties | 1 + .../cdi/interceptor/LogServiceInterceptor.java | 4 ++++ .../log/cdi/runtime/LogRuntimeConfig.java | 6 ++++++ extensions/pom.xml | 17 +++++++++++++++++ 8 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/DummyLogFriendlyException.java diff --git a/extensions/log/cdi/README.md b/extensions/log/cdi/README.md index 7a7a87a..7bde928 100644 --- a/extensions/log/cdi/README.md +++ b/extensions/log/cdi/README.md @@ -43,6 +43,8 @@ tkit.log.cdi.failed.enabled=true tkit.log.cdi.failed.template=%1$s(%2$s) throw %3$s [%4$.3fs] # Return void method template tkit.log.cdi.return-void-template=void +# Return void method template +tkit.log.cdi.mdc.errorKey=errorNumber # Enable or disable service log. (optional) tkit.log.cdi.service..log= # Enable or disable service stacktrace. (optional) diff --git a/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/ErrorDataServiceTest.java b/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/ErrorDataServiceTest.java index 44c83e6..a1da0b9 100644 --- a/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/ErrorDataServiceTest.java +++ b/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/ErrorDataServiceTest.java @@ -5,17 +5,20 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.tkit.quarkus.log.cdi.test.app.DummyLogFriendlyException; import org.tkit.quarkus.log.cdi.test.app.ErrorDataService; import org.tkit.quarkus.log.cdi.test.app.ErrorWrapperService; import io.quarkus.test.QuarkusUnitTest; +import static org.tkit.quarkus.log.cdi.test.app.DummyLogFriendlyException.DUMMY_ERROR_NUMBER; + public class ErrorDataServiceTest extends AbstractTest { @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar - .addClasses(ErrorDataService.class, ErrorWrapperService.class) + .addClasses(ErrorDataService.class, ErrorWrapperService.class, DummyLogFriendlyException.class) .addAsResource("default.properties", "application.properties")); @Inject @@ -33,6 +36,13 @@ public void error1Test() { "ERROR [org.tki.qua.log.cdi.tes.app.ErrorDataService] (main) error1(Error) throw java.lang.RuntimeException: Error"); } + @Test + public void error2Test() { + Assertions.assertThrows(RuntimeException.class, () -> service.error2()); + assertLogs().assertContains(0, + DUMMY_ERROR_NUMBER +" ERROR [org.tki.qua.log.cdi.tes.app.ErrorDataService] (main) error2() throw org.tkit.quarkus.log.cdi.test.app.DummyLogFriendlyException"); + } + @Test public void noStacktraceTest() { Assertions.assertThrows(RuntimeException.class, () -> service.noStacktrace()); diff --git a/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/DummyLogFriendlyException.java b/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/DummyLogFriendlyException.java new file mode 100644 index 0000000..78c03e9 --- /dev/null +++ b/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/DummyLogFriendlyException.java @@ -0,0 +1,18 @@ +package org.tkit.quarkus.log.cdi.test.app; + +import org.tkit.quarkus.log.cdi.LogFriendlyException; + +public class DummyLogFriendlyException extends RuntimeException implements LogFriendlyException { + + public static final String DUMMY_ERROR_NUMBER = "DummyErrorNumber"; + + @Override + public String getErrorNumber() { + return DUMMY_ERROR_NUMBER; + } + + @Override + public boolean shouldLogStacktrace() { + return false; + } +} diff --git a/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/ErrorDataService.java b/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/ErrorDataService.java index 0d7fd6c..52952df 100644 --- a/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/ErrorDataService.java +++ b/extensions/log/cdi/deployment/src/test/java/org/tkit/quarkus/log/cdi/test/app/ErrorDataService.java @@ -12,6 +12,10 @@ public void error1(String error) { throw new RuntimeException(error); } + public void error2() { + throw new DummyLogFriendlyException(); + } + @LogService(stacktrace = false) public void noStacktrace() { throw new RuntimeException("Error1"); diff --git a/extensions/log/cdi/deployment/src/test/resources/default.properties b/extensions/log/cdi/deployment/src/test/resources/default.properties index e69de29..a7ee9cf 100644 --- a/extensions/log/cdi/deployment/src/test/resources/default.properties +++ b/extensions/log/cdi/deployment/src/test/resources/default.properties @@ -0,0 +1 @@ +quarkus.log.console.format=%d{yyyy-MM-dd HH:mm:ss,SSS} %X{business_information_errorNumber} %-5p [%c{3.}] (%t) %s%e%n \ No newline at end of file diff --git a/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/interceptor/LogServiceInterceptor.java b/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/interceptor/LogServiceInterceptor.java index 9761c88..02683e0 100644 --- a/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/interceptor/LogServiceInterceptor.java +++ b/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/interceptor/LogServiceInterceptor.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import org.tkit.quarkus.context.ApplicationContext; import org.tkit.quarkus.context.Context; +import org.tkit.quarkus.log.cdi.LogFriendlyException; import org.tkit.quarkus.log.cdi.LogRecorder; import org.tkit.quarkus.log.cdi.LogService; import org.tkit.quarkus.log.cdi.ServiceValue; @@ -122,6 +123,9 @@ public Object methodExecution(final InvocationContext ic) throws Exception { Context.ApplicationError aex = ApplicationContext.get().addError(ex); Throwable error = ex; + if (ex instanceof LogFriendlyException) { + ApplicationContext.addBusinessLogParam(config.errorNumberKey, ((LogFriendlyException) ex).getErrorNumber()); + } if (ex instanceof InvocationTargetException) { error = ex.getCause(); } diff --git a/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/runtime/LogRuntimeConfig.java b/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/runtime/LogRuntimeConfig.java index 37d6877..dfd4a07 100644 --- a/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/runtime/LogRuntimeConfig.java +++ b/extensions/log/cdi/runtime/src/main/java/org/tkit/quarkus/log/cdi/runtime/LogRuntimeConfig.java @@ -54,6 +54,12 @@ public class LogRuntimeConfig { @ConfigItem(name = "service") public Map service = new HashMap<>(); + /** + * Mdc error key for FBN error code + */ + @ConfigItem(name = "mdc.errorKey", defaultValue = "errorNumber") + public String errorNumberKey; + /** * Start message */ diff --git a/extensions/pom.xml b/extensions/pom.xml index aca8aa0..7dfee22 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -23,4 +23,21 @@ test-db-import + + + + maven-source-plugin + 3.0.1 + + + attach-sources + + jar + + + + + + +