Skip to content

Commit

Permalink
Restore RAW key loading to avoid breaking change for camelCase packag…
Browse files Browse the repository at this point in the history
…e names
  • Loading branch information
ttzn committed Dec 29, 2020
1 parent c9f7418 commit 991bb76
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Expand Up @@ -22,13 +22,16 @@
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.naming.conventions.StringConvention;
import io.micronaut.core.util.StringUtils;
import io.micronaut.runtime.context.scope.refresh.RefreshEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Properties logging levels configurer.
Expand Down Expand Up @@ -77,18 +80,24 @@ public void onApplicationEvent(RefreshEvent event) {
}

private void configureLogLevels() {
environment.getProperties(LOGGER_LEVELS_PROPERTY_PREFIX).forEach((loggerPrefix, levelString) -> {
LogLevel newLevel = toLogLevel(levelString.toString());
if (newLevel == null) {
throw new ConfigurationException("Invalid log level: '" + levelString + "' for logger: '" + loggerPrefix + "'");
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Setting log level '{}' for logger: '{}'", newLevel, loggerPrefix);
}
for (LoggingSystem loggingSystem : loggingSystems) {
loggingSystem.setLogLevel(loggerPrefix, newLevel);
}
});
Map<String, Object> properties = new HashMap<>(environment.getProperties(LOGGER_LEVELS_PROPERTY_PREFIX));
// Using raw keys here allows configuring log levels for camelCase package names in application.yml
properties.putAll(environment.getProperties(LOGGER_LEVELS_PROPERTY_PREFIX, StringConvention.RAW));
properties.forEach(this::configureLogLevelForPrefix);
}

private void configureLogLevelForPrefix(String loggerPrefix, Object levelValue) {
LogLevel newLevel = toLogLevel(levelValue.toString());
if (newLevel == null) {
throw new ConfigurationException("Invalid log level: '" + levelValue + "' for logger: '" + loggerPrefix + "'");
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Setting log level '{}' for logger: '{}'", newLevel, loggerPrefix);
}
LOGGER.info("Setting log level '{}' for logger: '{}'", newLevel, loggerPrefix);
for (LoggingSystem loggingSystem : loggingSystems) {
loggingSystem.setLogLevel(loggerPrefix, newLevel);
}
}

private static LogLevel toLogLevel(String logLevel) {
Expand Down
Expand Up @@ -16,13 +16,15 @@ class LogbackLogLevelConfigurerSpec extends Specification {
((Logger) LoggerFactory.getLogger('foo.bar1')).setLevel(Level.DEBUG)
((Logger) LoggerFactory.getLogger('foo.bar2')).setLevel(Level.DEBUG)
((Logger) LoggerFactory.getLogger('foo.bar3')).setLevel(Level.ERROR)
((Logger) LoggerFactory.getLogger('foo.barBaz')).setLevel(Level.WARN)

when:
ApplicationContext context = ApplicationContext.run(
[
'logger.levels.aaa.bbb.ccc': 'ERROR',
'logger.levels.foo.bar2' : 'INFO',
'logger.levels.foo.bar3' : '',
'logger.levels.foo.barBaz' : 'INFO',
]
)

Expand All @@ -38,6 +40,7 @@ class LogbackLogLevelConfigurerSpec extends Specification {
'foo.bar2' | Level.INFO
'foo.bar3' | null
'aaa.bbb.ccc' | Level.ERROR
'foo.barBaz' | Level.WARN

}

Expand Down
2 changes: 2 additions & 0 deletions src/main/docs/guide/config/propertySource.adoc
Expand Up @@ -177,4 +177,6 @@ logger:
foo.bar: ERROR
----

The same configuration can be achieved by setting the environment variable `LOGGER_LEVELS_FOO_BAR`. Note that there is currently no way to set log levels for unconventional prefixes such as `foo.barBaz`.

Note that the ability to control log levels via config is controlled via the api:logging.LoggingSystem[] interface. Currently Micronaut ships with a single implementation that allows setting log levels for the Logback library. If another library is chosen you should provide a bean that implements this interface.

0 comments on commit 991bb76

Please sign in to comment.