Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tkit.log.cdi.mdc.errorKey | java sources #91

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>