Skip to content

Commit

Permalink
fix: graal error for private @Property (#8541)
Browse files Browse the repository at this point in the history
* checkstyle: trailing dot for first sentence

* fix: graal error for private @Property

Whiel deploying a GraalVM native executable of a lambda function with 22.3 and MN 3.8.0 I was getting:
```
Caused by: java.lang.NoSuchFieldError: No field 'logbackXmlLocation' found for type: io.micronaut.logging.impl.LogbackLoggingSystem
at io.micronaut.core.reflect.ReflectionUtils.lambda$getRequiredField$2(ReflectionUtils.java:294)
at java.util.Optional.orElseThrow(Optional.java:408)
at io.micronaut.core.reflect.ReflectionUtils.getRequiredField(ReflectionUtils.java:294)
at io.micronaut.context.AbstractInitializableBeanDefinition.setFieldWithReflection(AbstractInitializableBeanDefinition.java:979)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [io.micronaut.logging.impl.LogbackLoggingSystem] could not be loaded: Error instantiating bean of type  [io.micronaut.logging.impl.LogbackLoggingSystem]

Caused by: io.micronaut.context.exceptions.DependencyInjectionException: Error instantiating bean of type  [io.micronaut.logging.impl.LogbackLoggingSystem]
Message: Error setting field value: No field 'logbackXmlLocation' found for type: io.micronaut.logging.impl.LogbackLoggingSystem
Path Taken: new LogbackLoggingSystem()
at io.micronaut.context.AbstractInitializableBeanDefinition.setFieldWithReflection(AbstractInitializableBeanDefinition.java:986)
at io.micronaut.logging.impl.$LogbackLoggingSystem$Definition.injectBean(Unknown Source)
```

This PR changes LogbackLoggingSystem to use constructor injection for the property `logger.config` to avoid such an error.
  • Loading branch information
sdelamo committed Dec 26, 2022
1 parent 8a983fc commit 3815723
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.net.URL;
import java.util.Objects;
import java.util.Optional;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
Expand All @@ -26,6 +25,7 @@
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.logging.LogLevel;
import io.micronaut.logging.LoggingSystem;
import io.micronaut.logging.LoggingSystemException;
Expand All @@ -45,8 +45,11 @@ public final class LogbackLoggingSystem implements LoggingSystem {

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

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

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

@Override
public void setLogLevel(String name, LogLevel level) {
Expand All @@ -57,13 +60,12 @@ public void setLogLevel(String name, LogLevel level) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
Class<? extends RetryPredicate> predicate() default DefaultRetryPredicate.class;

/**
* If {@code true} and the circuit is opened, it throws the original exception wrapped
* If {@code true} and the circuit is opened, it throws the original exception wrapped.
* in a {@link io.micronaut.retry.exception.CircuitOpenException}
* @return Whether to wrap the original exception in a {@link io.micronaut.retry.exception.CircuitOpenException}
*/
Expand Down

0 comments on commit 3815723

Please sign in to comment.