Skip to content

Commit

Permalink
bugfix: parsing min level of caller classes that have no package
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Mar 17, 2023
1 parent 1be9151 commit 0f20696
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-impl-core</artifactId>
<version>0.5.5</version>
<version>0.5.6</version>
<packaging>jar</packaging>
<name>elf4j-impl-core</name>
<description>Native logging service provider implementation of ELF4J (Easy Logging Facade For Java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,12 @@ public class DefaultServiceConfiguration implements ServiceConfiguration {
*
*/
public DefaultServiceConfiguration() {
this(new PropertiesLoader());
}

DefaultServiceConfiguration(PropertiesLoader propertiesLoader) {
this.propertiesLoader = propertiesLoader;
setRepositories(propertiesLoader.load());
this.propertiesLoader = new PropertiesLoader();
setRepositories(this.propertiesLoader.load());
}

DefaultServiceConfiguration(LevelRepository levelRepository, WriterRepository writerRepository) {
this(new PropertiesLoader());
this.propertiesLoader = new PropertiesLoader();
this.levelRepository = levelRepository;
this.writerRepository = writerRepository;
}
Expand Down Expand Up @@ -87,11 +83,10 @@ public void refresh(@Nullable Properties properties) {
}

private boolean loadLoggerConfigurationCache(NativeLogger nativeLogger) {
Level loggerMinimumLevel = levelRepository.getLoggerMinimumLevel(nativeLogger);
Level loggerConfigurationMinimumLevel = levelRepository.getLoggerMinimumLevel(nativeLogger);
Level logServiceWriterMinimumLevel = writerRepository.getLogServiceWriter().getMinimumLevel();
int effectiveMinimumLevelOrdinal =
Math.max(loggerMinimumLevel.ordinal(), logServiceWriterMinimumLevel.ordinal());
return nativeLogger.getLevel().ordinal() >= effectiveMinimumLevelOrdinal;
return nativeLogger.getLevel().ordinal() >= Math.max(loggerConfigurationMinimumLevel.ordinal(),
logServiceWriterMinimumLevel.ordinal());
}

private void setRepositories(@NonNull Properties properties) {
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/elf4j/impl/core/configuration/LevelRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
public class LevelRepository {
private static final Level DEFAULT_LOGGER_MINIMUM_LEVEL = Level.TRACE;
final Map<String, Level> loggerNameValueMap = new HashMap<>();
final Map<String, Level> loggerMinimumLevels = new HashMap<>();

/**
* @param properties configuration source of all minimum output levels for loggers
Expand All @@ -48,10 +48,11 @@ public LevelRepository(Properties properties) {
String[] nameSegments = name.split("@");
switch (nameSegments.length) {
case 1:
loggerNameValueMap.put("", Level.valueOf(properties.getProperty("level").trim().toUpperCase()));
loggerMinimumLevels.put("",
Level.valueOf(properties.getProperty("level").trim().toUpperCase()));
break;
case 2:
loggerNameValueMap.put(nameSegments[1].trim(),
loggerMinimumLevels.put(nameSegments[1].trim(),
Level.valueOf(properties.getProperty(name).trim().toUpperCase()));
break;
default:
Expand All @@ -68,9 +69,12 @@ public LevelRepository(Properties properties) {
public Level getLoggerMinimumLevel(NativeLogger nativeLogger) {
String callerClassName = nativeLogger.getName();
int rootPackageLength = callerClassName.indexOf('.');
if (rootPackageLength == -1) {
rootPackageLength = callerClassName.length();
}
while (callerClassName.length() >= rootPackageLength) {
if (loggerNameValueMap.containsKey(callerClassName)) {
return loggerNameValueMap.get(callerClassName);
if (loggerMinimumLevels.containsKey(callerClassName)) {
return loggerMinimumLevels.get(callerClassName);
}
if (callerClassName.length() == rootPackageLength) {
break;
Expand All @@ -81,7 +85,7 @@ public Level getLoggerMinimumLevel(NativeLogger nativeLogger) {
}
callerClassName = callerClassName.substring(0, end);
}
Level configuredRootLevel = loggerNameValueMap.get("");
Level configuredRootLevel = loggerMinimumLevels.get("");
return configuredRootLevel == null ? DEFAULT_LOGGER_MINIMUM_LEVEL : configuredRootLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DefaultServiceConfigurationTest {
@Nested
class isEnabled {
@Test
void loadFromReposOnlyOnce() {
void cacheLoadFromReposOnlyOnce() {
DefaultServiceConfiguration defaultLoggingConfiguration =
new DefaultServiceConfiguration(mockLevelRepository, mockWriterRepository);
NativeLogger nativeLogger = new NativeLogger("test.owner.class.Name", Level.OFF, mockLogService);
Expand Down

0 comments on commit 0f20696

Please sign in to comment.