Skip to content

Commit

Permalink
fix: graal error for private @Property
Browse files Browse the repository at this point in the history
Changes to inject @Property(name = "logger.config") via constructor injection.

Graal 22.3 and Micronaut 3.8.0 was throwing:

```
Caused by: java.lang.NoSuchFieldError: No field 'logbackXmlLocation' found for type: io.micronaut.management.endpoint.loggers.impl.LogbackLoggingSystem
195	at io.micronaut.core.reflect.ReflectionUtils.lambda$getRequiredField$2(ReflectionUtils.java:294)
196	at java.base@17.0.5/java.util.Optional.orElseThrow(Optional.java:403)
197	at io.micronaut.core.reflect.ReflectionUtils.getRequiredField(ReflectionUtils.java:294)
198	at io.micronaut.context.AbstractInitializableBeanDefinition.setFieldWithReflection(AbstractInitializableBeanDefinition.java:979)
199	... 113 common frames omitted
```
  • Loading branch information
sdelamo committed Dec 26, 2022
1 parent 3815723 commit 46a8891
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.context.annotation.Replaces;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.logging.LogLevel;
import io.micronaut.logging.LoggingSystemException;
import io.micronaut.management.endpoint.loggers.LoggerConfiguration;
Expand All @@ -35,7 +36,6 @@
import java.net.URL;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand All @@ -51,8 +51,11 @@
public class LogbackLoggingSystem implements ManagedLoggingSystem, io.micronaut.logging.LoggingSystem {
private static final String DEFAULT_LOGBACK_LOCATION = "logback.xml";

@Property(name = "logger.config")
private Optional<String> logbackXmlLocation;
private final String logbackXmlLocation;

public LogbackLoggingSystem(@Nullable @Property(name = "logger.config") String logbackXmlLocation) {
this.logbackXmlLocation = logbackXmlLocation != null ? logbackXmlLocation : DEFAULT_LOGBACK_LOCATION;
}

@Override
@NonNull
Expand Down Expand Up @@ -117,18 +120,17 @@ private static Level toLevel(LogLevel logLevel) {
return Level.valueOf(logLevel.name());
}
}

@Override
public void refresh() {
LoggerContext context = getLoggerContext();
context.reset();
String logbackXml = logbackXmlLocation.orElse(DEFAULT_LOGBACK_LOCATION);
URL resource = getClass().getClassLoader().getResource(logbackXml);
URL resource = getClass().getClassLoader().getResource(logbackXmlLocation);
if (Objects.isNull(resource)) {
throw new LoggingSystemException("Resource " + logbackXml + " not found");
throw new LoggingSystemException("Resource " + logbackXmlLocation + " not found");
}

try {
try {
new ContextInitializer(context).configureByResource(resource);
} catch (JoranException e) {
throw new LoggingSystemException("Error while refreshing Logback", e);
Expand Down
Expand Up @@ -45,7 +45,7 @@ public final class LogbackLoggingSystem implements LoggingSystem {

private static final String DEFAULT_LOGBACK_LOCATION = "logback.xml";

private String logbackXmlLocation;
private final String logbackXmlLocation;

public LogbackLoggingSystem(@Nullable @Property(name = "logger.config") String logbackXmlLocation) {
this.logbackXmlLocation = logbackXmlLocation != null ? logbackXmlLocation : DEFAULT_LOGBACK_LOCATION;
Expand Down

0 comments on commit 46a8891

Please sign in to comment.