Skip to content

Commit

Permalink
+ better worker context switch
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Apr 25, 2024
1 parent 0cc555b commit ec10349
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-engine</artifactId>
<version>15.2.1</version>
<version>15.2.2</version>
<packaging>jar</packaging>
<name>elf4j-engine</name>
<description>A stand-alone Java log engine implementing the ELF4J (Easy Logging Facade for Java) API</description>
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/elf4j/engine/service/writer/GroupWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ private static List<LogWriterType> getLogWriterTypes(@NonNull LogServiceConfigur
.collect(Collectors.toList());
}

private static Runnable withMdcContext(Runnable task) {
Map<String, String> callerContext = MDC.getCopyOfContextMap();
return (callerContext == null)
? task
: () -> {
Map<String, String> workerContext = replaceContextWith(callerContext);
MDC.setContextMap(callerContext);
try {
task.run();
} finally {
MDC.setContextMap(workerContext);
}
};
}

private static Map<String, String> replaceContextWith(Map<String, String> targetContext) {
Map<String, String> replaced = MDC.getCopyOfContextMap();
MDC.setContextMap(targetContext);
return replaced;
}

@Override
public Level getThresholdOutputLevel() {
if (thresholdOutputLevel == null) {
Expand All @@ -137,21 +158,6 @@ public void write(@NonNull LogEvent logEvent) {
logEvent.getCallerThread().getId()));
}

private static Runnable withMdcContext(Runnable task) {
Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
return (copyOfContextMap == null)
? task
: () -> {
MDC.clear();
MDC.setContextMap(copyOfContextMap);
try {
task.run();
} finally {
MDC.clear();
}
};
}

@Override
public boolean includeCallerDetail() {
if (includeCallerDetail == null) {
Expand Down

0 comments on commit ec10349

Please sign in to comment.