Turn off logging of TemplateEngine (#7284)

#### 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 异常的问题
```
pull/7277/merge
John Niang 2025-03-11 14:35:02 +08:00 committed by GitHub
parent 4ad97cd58e
commit fed80f26f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 10 deletions

View File

@ -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<DataBuffer> 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<DataBuffer> 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
}
}

View File

@ -32,6 +32,7 @@ halo:
logging:
level:
run.halo.app: DEBUG
web: DEBUG
org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler: DEBUG
springdoc:
cache:

View File

@ -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: