Skip to content

Commit

Permalink
feat:introduce shouldWriteAccessLog
Browse files Browse the repository at this point in the history
  • Loading branch information
yzfeng2020 committed Nov 17, 2024
1 parent 4bc956f commit 6905505
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ final void endLogRequestAndResponse(@Nullable Throwable cause) {
}

/**
* Writes an access log if the {@link TransientServiceOption#WITH_ACCESS_LOGGING} option is enabled for
* the {@link #service()}.
* Writes an access log if the {@link ServiceConfig#shouldWriteAccessLog} is true
* for the {@link #service()}.
*/
final void maybeWriteAccessLog() {
final ServiceConfig config = reqCtx.config();
if (config.transientServiceOptions().contains(TransientServiceOption.WITH_ACCESS_LOGGING)) {
if (config.shouldWriteAccessLog()) {
reqCtx.log().whenComplete().thenAccept(log -> {
try (SafeCloseable ignored = reqCtx.push()) {
config.accessLogWriter().log(log);
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/linecorp/armeria/server/ServiceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ List<ShutdownSupport> shutdownSupports() {
return shutdownSupports;
}

/**
* Returns whether the access log should be written for this {@link #service()}.
* Access log is written only when the {@link TransientServiceOption#WITH_ACCESS_LOGGING} is set and the
* access logger is not the the {@link AccessLogWriter#DISABLED}.
*/
public boolean shouldWriteAccessLog() {
return !accessLogWriter.isDisabled() &&
transientServiceOptions.contains(TransientServiceOption.WITH_ACCESS_LOGGING);
}

/**
* Returns the {@link EventLoopGroup} dedicated to the execution of services' methods.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
@FunctionalInterface
public interface AccessLogWriter {

/**
* A disabled access log writer that performs no operation.
*/
AccessLogWriter DISABLED = requestLog -> { /* No operation. */ };

/**
* Returns an access log writer with a common format.
*/
Expand All @@ -50,7 +55,15 @@ static AccessLogWriter combined() {
* Returns disabled access log writer.
*/
static AccessLogWriter disabled() {
return requestLog -> { /* No operation. */ };
return DISABLED;
}

/**
* Determines if this writer is the disabled writer.
*
*/
default boolean isDisabled() {
return this == DISABLED;
}

/**
Expand Down

0 comments on commit 6905505

Please sign in to comment.