Skip to content

Commit

Permalink
Register new Logback converter for reflection in a native image
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Mar 1, 2024
1 parent 3ba9478 commit 753bbf4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,7 +58,7 @@ private void registerHintsForBuiltInLogbackConverters(ReflectionHints reflection
}

private void registerHintsForSpringBootConverters(ReflectionHints reflection) {
registerForPublicConstructorInvocation(reflection, ColorConverter.class,
registerForPublicConstructorInvocation(reflection, ApplicationNameConverter.class, ColorConverter.class,
ExtendedWhitespaceThrowableProxyConverter.class, WhitespaceThrowableProxyConverter.class,
CorrelationIdConverter.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,14 @@

package org.springframework.boot.logging.logback;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.pattern.SyslogStartConverter;
import ch.qos.logback.core.pattern.Converter;
import ch.qos.logback.core.rolling.helper.DateTokenConverter;
import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import org.junit.jupiter.api.Test;
Expand All @@ -30,6 +33,8 @@
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -54,11 +59,29 @@ void registersHintsForBuiltInLogbackConverters() {
}

@Test
void registersHintsForSpringBootConverters() throws LinkageError {
void registersHintsForSpringBootConverters() throws IOException {
ReflectionHints reflection = registerHints();
assertThat(List.of(ColorConverter.class, ExtendedWhitespaceThrowableProxyConverter.class,
WhitespaceThrowableProxyConverter.class))
.allSatisfy(registeredForPublicConstructorInvocation(reflection));
assertThat(converterClasses()).allSatisfy(registeredForPublicConstructorInvocation(reflection));
}

@SuppressWarnings("unchecked")
private Stream<Class<Converter<?>>> converterClasses() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
return Stream.of(resolver.getResources("classpath:org/springframework/boot/logging/logback/*.class"))
.filter(Resource::isFile)
.map(this::loadClass)
.filter(Converter.class::isAssignableFrom)
.map((type) -> (Class<Converter<?>>) type);
}

private Class<?> loadClass(Resource resource) {
try {
return getClass().getClassLoader()
.loadClass("org.springframework.boot.logging.logback." + resource.getFilename().replace(".class", ""));
}
catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}

@Test
Expand Down

0 comments on commit 753bbf4

Please sign in to comment.