Skip to content

Commit

Permalink
Support async on demand logging
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Jan 20, 2025
1 parent d60652e commit 2d3eda7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import no.entur.logging.cloud.appender.scope.LoggingScopeAsyncAppender;
import no.entur.logging.cloud.appender.scope.predicate.HigherOrEqualToLogLevelPredicate;
import no.entur.logging.cloud.appender.scope.predicate.LoggerNamePrefixHigherOrEqualToLogLevelPredicate;
import no.entur.logging.cloud.spring.ondemand.web.properties.*;
import no.entur.logging.cloud.spring.ondemand.web.scope.*;
import no.entur.logging.cloud.spring.ondemand.web.properties.OndemandFailure;
import no.entur.logging.cloud.spring.ondemand.web.properties.OndemandHttpHeader;
Expand Down Expand Up @@ -54,6 +53,18 @@ public class GcpWebOndemandLoggingAutoConfiguration {
@ConditionalOnProperty(name = {"entur.logging.http.ondemand.enabled"}, havingValue = "true", matchIfMissing = false)
public static class OndemandConfiguration {

private ThreadLocalLoggingScopeFactory factory = new ThreadLocalLoggingScopeFactory();

@Bean
public LoggingScopeControls loggingScopeControls() {
return factory;
}

@Bean
public LoggingScopeThreadUtils loggingScopeThreadUtils() {
return new LoggingScopeThreadUtils(factory);
}

@Bean
public FilterRegistrationBean<OndemandFilter> ondemandFilter(OndemandProperties properties) {
LoggingScopeAsyncAppender appender = getAppender();
Expand All @@ -75,8 +86,6 @@ public FilterRegistrationBean<OndemandFilter> ondemandFilter(OndemandProperties
filters.addFilter(requestMatcher, filter);
}

ThreadLocalLoggingScopeFactory factory = new ThreadLocalLoggingScopeFactory();

OndemandFilter filter = new OndemandFilter(appender, filters, factory);

appender.addScopeProvider(factory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import no.entur.logging.cloud.appender.scope.LoggingScope;
import no.entur.logging.cloud.appender.scope.LoggingScopeFactory;
import no.entur.logging.cloud.appender.scope.LoggingScopeSink;
import no.entur.logging.cloud.spring.ondemand.web.scope.HttpLoggingScopeFilter;
import no.entur.logging.cloud.spring.ondemand.web.scope.HttpLoggingScopeFilters;
Expand All @@ -19,9 +20,9 @@ public class OndemandFilter implements Filter {
private final LoggingScopeSink sink;
private final HttpLoggingScopeFilters filters;

private final ThreadLocalLoggingScopeFactory loggingScopeFactory;
private final LoggingScopeFactory loggingScopeFactory;

public OndemandFilter(LoggingScopeSink sink, HttpLoggingScopeFilters filters, ThreadLocalLoggingScopeFactory loggingScopeFactory) {
public OndemandFilter(LoggingScopeSink sink, HttpLoggingScopeFilters filters, LoggingScopeFactory loggingScopeFactory) {
this.sink = sink;
this.filters = filters;
this.loggingScopeFactory = loggingScopeFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package no.entur.logging.cloud.spring.ondemand.web.scope;

import no.entur.logging.cloud.appender.scope.LoggingScope;
import no.entur.logging.cloud.appender.scope.LoggingScopeProvider;

public interface LoggingScopeControls extends LoggingScopeProvider {

void setCurrentScope(LoggingScope scope);

void clearCurrentScope();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package no.entur.logging.cloud.spring.ondemand.web.scope;

import no.entur.logging.cloud.appender.scope.LoggingScope;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class LoggingScopeThreadUtils {

protected final LoggingScopeControls controls;

public LoggingScopeThreadUtils(LoggingScopeControls controls) {
this.controls = controls;
}

public Runnable withLoggingScope(Runnable runnable) {
LoggingScope currentScope = controls.getCurrentScope();
if(currentScope == null) {
return runnable;
}

return () -> {
controls.setCurrentScope(currentScope);
try {
runnable.run();
} finally {
controls.clearCurrentScope();
}
};
}

public <U> Supplier<U> withLoggingScope(Supplier<U> supplier) {
LoggingScope currentScope = controls.getCurrentScope();
if(currentScope == null) {
return supplier;
}
return (Supplier) () -> {
controls.setCurrentScope(currentScope);
try {
return supplier.get();
} finally {
controls.clearCurrentScope();
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import java.util.function.Predicate;

public class ThreadLocalLoggingScopeFactory implements LoggingScopeFactory, LoggingScopeProvider {
public class ThreadLocalLoggingScopeFactory implements LoggingScopeFactory, LoggingScopeControls {

private final ThreadLocal<LoggingScope> queues = new ThreadLocal<>();

Expand All @@ -27,4 +27,12 @@ public LoggingScope getCurrentScope() {
public void closeScope(LoggingScope scope) {
queues.remove();
}

public void setCurrentScope(LoggingScope scope) {
queues.set(scope);
}

public void clearCurrentScope() {
queues.remove();
}
}

0 comments on commit 2d3eda7

Please sign in to comment.