Search in sources :

Example 31 with Logger

use of cn.taketoday.logging.Logger in project today-infrastructure by TAKETODAY.

the class CustomizableTraceInterceptorTests method testExceptionPathLogsCorrectly.

@Test
public void testExceptionPathLogsCorrectly() throws Throwable {
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    IllegalArgumentException exception = new IllegalArgumentException();
    given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString"));
    given(methodInvocation.getThis()).willReturn(this);
    given(methodInvocation.proceed()).willThrow(exception);
    Logger log = mock(Logger.class);
    given(log.isTraceEnabled()).willReturn(true);
    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    assertThatIllegalArgumentException().isThrownBy(() -> interceptor.invoke(methodInvocation));
    verify(log).trace(anyString());
    verify(log).trace(anyString(), eq(exception));
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(cn.taketoday.logging.Logger) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 32 with Logger

use of cn.taketoday.logging.Logger in project today-infrastructure by TAKETODAY.

the class ResourceHttpMessageWriter method writeResource.

private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    HttpHeaders headers = message.getHeaders();
    MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);
    headers.setContentType(resourceMediaType);
    if (headers.getContentLength() < 0) {
        long length = lengthOf(resource);
        if (length != -1) {
            headers.setContentLength(length);
        }
    }
    return zeroCopy(resource, null, message, hints).orElseGet(() -> {
        Mono<Resource> input = Mono.just(resource);
        DataBufferFactory factory = message.bufferFactory();
        Flux<DataBuffer> body = this.encoder.encode(input, factory, type, resourceMediaType, hints);
        if (logger.isDebugEnabled()) {
            body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
        }
        return message.writeWith(body);
    });
}
Also used : DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) ResourceDecoder(cn.taketoday.core.codec.ResourceDecoder) ZeroCopyHttpOutputMessage(cn.taketoday.http.ZeroCopyHttpOutputMessage) HttpHeaders(cn.taketoday.http.HttpHeaders) ResourceEncoder(cn.taketoday.core.codec.ResourceEncoder) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) ResourceRegion(cn.taketoday.core.io.ResourceRegion) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) File(java.io.File) HttpRange(cn.taketoday.http.HttpRange) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerHttpRequest(cn.taketoday.http.server.reactive.ServerHttpRequest) Nullable(cn.taketoday.lang.Nullable) HttpLogging(cn.taketoday.http.HttpLogging) MediaTypeFactory(cn.taketoday.http.MediaTypeFactory) Logger(cn.taketoday.logging.Logger) Optional(java.util.Optional) HttpStatus(cn.taketoday.http.HttpStatus) ResourceRegionEncoder(cn.taketoday.core.codec.ResourceRegionEncoder) MediaType(cn.taketoday.http.MediaType) InputStreamResource(cn.taketoday.core.io.InputStreamResource) HttpHeaders(cn.taketoday.http.HttpHeaders) Resource(cn.taketoday.core.io.Resource) InputStreamResource(cn.taketoday.core.io.InputStreamResource) MediaType(cn.taketoday.http.MediaType) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 33 with Logger

use of cn.taketoday.logging.Logger in project today-infrastructure by TAKETODAY.

the class LoggingCacheErrorHandlerTests method handleGetCacheErrorLogsAppropriateMessage.

@Test
void handleGetCacheErrorLogsAppropriateMessage() {
    Logger logger = mock(Logger.class);
    LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(logger, false);
    handler.handleCacheGetError(new RuntimeException(), new NoOpCache("NOOP"), "key");
    verify(logger).warn("Cache 'NOOP' failed to get entry with key 'key'");
}
Also used : NoOpCache(cn.taketoday.cache.support.NoOpCache) Logger(cn.taketoday.logging.Logger) Test(org.junit.jupiter.api.Test)

Example 34 with Logger

use of cn.taketoday.logging.Logger in project today-infrastructure by TAKETODAY.

the class LoggingCacheErrorHandlerTests method handlePutCacheErrorLogsAppropriateMessage.

@Test
void handlePutCacheErrorLogsAppropriateMessage() {
    Logger logger = mock(Logger.class);
    LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(logger, false);
    handler.handleCachePutError(new RuntimeException(), new NoOpCache("NOOP"), "key", new Object());
    verify(logger).warn("Cache 'NOOP' failed to put entry with key 'key'");
}
Also used : NoOpCache(cn.taketoday.cache.support.NoOpCache) Logger(cn.taketoday.logging.Logger) Test(org.junit.jupiter.api.Test)

Aggregations

Logger (cn.taketoday.logging.Logger)34 Test (org.junit.jupiter.api.Test)32 MethodInvocation (org.aopalliance.intercept.MethodInvocation)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)14 NoOpCache (cn.taketoday.cache.support.NoOpCache)10 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)10 List (java.util.List)4 ResolvableType (cn.taketoday.core.ResolvableType)2 Hints (cn.taketoday.core.codec.Hints)2 ResourceDecoder (cn.taketoday.core.codec.ResourceDecoder)2 ResourceEncoder (cn.taketoday.core.codec.ResourceEncoder)2 ResourceRegionEncoder (cn.taketoday.core.codec.ResourceRegionEncoder)2 DescriptiveResource (cn.taketoday.core.io.DescriptiveResource)2 InputStreamResource (cn.taketoday.core.io.InputStreamResource)2 Resource (cn.taketoday.core.io.Resource)2 ResourceRegion (cn.taketoday.core.io.ResourceRegion)2 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)2 DataBufferFactory (cn.taketoday.core.io.buffer.DataBufferFactory)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 HttpLogging (cn.taketoday.http.HttpLogging)2