From fed80f26f25a52946583ee3a0a698099204b8eb2 Mon Sep 17 00:00:00 2001 From: John Niang Date: Tue, 11 Mar 2025 14:35:02 +0800 Subject: [PATCH] Turn off logging of TemplateEngine (#7284) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: This PR turns off the logging of TemplateEngine to prevent too many annoying and useless logs. Please note that the TemplateExceptions won't be eat up because we have a global error handler to log them. #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/4468 #### Special notes for your reviewer: Steps to verify: - Start Halo instance - Execute command `ab -c 100 -n 10000 -H 'Accept: text/html' -H 'Cache-Control: no-cache' http://localhost:8090/` and then press `Ctrl + C` to stop the ab process. - See the logs of Halo instance. #### Does this PR introduce a user-facing change? ```release-note 解决日志中出现大量 InterruptedException 异常的问题 ``` --- .../app/theme/engine/HaloTemplateEngine.java | 26 ++++++++++++------- .../src/main/resources/application-dev.yaml | 1 + .../src/main/resources/application.yaml | 2 ++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/run/halo/app/theme/engine/HaloTemplateEngine.java b/application/src/main/java/run/halo/app/theme/engine/HaloTemplateEngine.java index c890d57df..0cdbc652f 100644 --- a/application/src/main/java/run/halo/app/theme/engine/HaloTemplateEngine.java +++ b/application/src/main/java/run/halo/app/theme/engine/HaloTemplateEngine.java @@ -1,7 +1,9 @@ package run.halo.app.theme.engine; +import java.nio.channels.ClosedByInterruptException; import java.nio.charset.Charset; import java.util.Set; +import lombok.extern.slf4j.Slf4j; import org.reactivestreams.Publisher; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; @@ -9,6 +11,7 @@ import org.springframework.http.MediaType; import org.thymeleaf.context.IContext; import org.thymeleaf.messageresolver.IMessageResolver; import org.thymeleaf.spring6.SpringWebFluxTemplateEngine; +import reactor.core.Exceptions; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; @@ -18,6 +21,7 @@ import reactor.core.scheduler.Schedulers; * * @author johnniang */ +@Slf4j public class HaloTemplateEngine extends SpringWebFluxTemplateEngine { private final IMessageResolver messageResolver; @@ -43,20 +47,22 @@ public class HaloTemplateEngine extends SpringWebFluxTemplateEngine { // while processing. if (publisher instanceof Mono mono) { return mono.subscribeOn(Schedulers.boundedElastic()) - // We should switch back to non-blocking thread. - // See https://github.com/spring-projects/spring-framework/issues/26958 - // for more details. - .publishOn(Schedulers.parallel()); + .doOnError(Exception.class, e -> this.logTemplateError(e, template)); } if (publisher instanceof Flux flux) { - return flux - .subscribeOn(Schedulers.boundedElastic()) - // We should switch back to non-blocking thread. - // See https://github.com/spring-projects/spring-framework/issues/26958 - // for more details. - .publishOn(Schedulers.parallel()); + return flux.subscribeOn(Schedulers.boundedElastic()) + .doOnError(Exception.class, e -> this.logTemplateError(e, template)); } return publisher; } + private void logTemplateError(Exception e, String template) { + if (Exceptions.unwrap(e.getCause()) instanceof InterruptedException) { + log.warn("Interrupted while processing template: {}", template); + } + if (e.getCause() instanceof ClosedByInterruptException) { + log.warn("Interrupted while outputting template: {}", template); + } + // other exceptions will be caught by error handler + } } diff --git a/application/src/main/resources/application-dev.yaml b/application/src/main/resources/application-dev.yaml index 62a31d4a6..77ee009a0 100644 --- a/application/src/main/resources/application-dev.yaml +++ b/application/src/main/resources/application-dev.yaml @@ -32,6 +32,7 @@ halo: logging: level: run.halo.app: DEBUG + web: DEBUG org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler: DEBUG springdoc: cache: diff --git a/application/src/main/resources/application.yaml b/application/src/main/resources/application.yaml index a42536af8..0728a2707 100644 --- a/application/src/main/resources/application.yaml +++ b/application/src/main/resources/application.yaml @@ -57,6 +57,8 @@ springdoc: writer-with-order-by-keys: true logging: + level: + org.thymeleaf.TemplateEngine: OFF file: name: ${halo.work-dir}/logs/halo.log logback: