Skip to content

Commit

Permalink
feat: tkit.log.cdi.mdc.errorKey | java sources
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsomora committed Oct 23, 2023
1 parent c1b9bd0 commit 75b0a6a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions extensions/log/cdi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<fully-qualified-class-name|config-key>.log=
# Enable or disable service stacktrace. (optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class LogRuntimeConfig {
@ConfigItem(name = "service")
public Map<String, ServiceConfig> service = new HashMap<>();

/**
* Mdc error key for FBN error code
*/
@ConfigItem(name = "mdc.errorKey", defaultValue = "errorNumber")
public String errorNumberKey;

/**
* Start message
*/
Expand Down
17 changes: 17 additions & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@
<module>test-db-import</module>
</modules>

<build>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

0 comments on commit 75b0a6a

Please sign in to comment.