Skip to content

Commit a05aed0

Browse files
committed
Add prioritized Logging.load() and tests
Introduce LoggingUtils.load() and load(ClassLoader) to return the highest-priority Logging implementation from SPI (uses Prioritized and ServiceLoaderUtils.loadFirstService). Update Javadoc to clarify priority ordering for loadAll, add unit test (testLoad) asserting the prioritized implementation is returned, and set MAX_PRIORITY in TestingLogging by implementing getPriority(). Minor import adjustments.
1 parent 92bda68 commit a05aed0

5 files changed

Lines changed: 44 additions & 6 deletions

File tree

microsphere-logging-commons/src/main/java/io/microsphere/logging/DefaultLoggingLevelsResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ public Set<String> resolve(Class<?> levelClass) {
5757
.map(field -> field.getName())
5858
.collect(toSet()));
5959
}
60-
}
60+
}

microsphere-logging-commons/src/main/java/io/microsphere/logging/LoggingLevelsResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ public interface LoggingLevelsResolver {
3838
*/
3939
@Nonnull
4040
Set<String> resolve(Class<?> levelClass);
41-
}
41+
}

microsphere-logging-commons/src/main/java/io/microsphere/logging/LoggingUtils.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
import io.microsphere.annotation.Nonnull;
2121
import io.microsphere.annotation.Nullable;
22+
import io.microsphere.lang.Prioritized;
2223

2324
import java.util.List;
2425

2526
import static io.microsphere.util.ClassLoaderUtils.getClassLoader;
27+
import static io.microsphere.util.ServiceLoaderUtils.loadFirstService;
2628
import static io.microsphere.util.ServiceLoaderUtils.loadServicesList;
2729

2830
/**
@@ -35,17 +37,17 @@
3537
public abstract class LoggingUtils {
3638

3739
/**
38-
* Load all {@link Logging} implementations from SPI
40+
* Load all {@link Logging} implementations in priority from SPI
3941
*
40-
* @return
42+
* @return non-null
4143
*/
4244
@Nonnull
4345
public static List<Logging> loadAll() {
4446
return loadAll(getClassLoader(LoggingUtils.class));
4547
}
4648

4749
/**
48-
* Load all {@link Logging} implementations from SPI
50+
* Load all {@link Logging} implementations in priority from SPI
4951
*
5052
* @param classLoader the {@link ClassLoader} which is used to load {@link Logging} implementations
5153
* @return non-null
@@ -56,6 +58,29 @@ public static List<Logging> loadAll(@Nullable ClassLoader classLoader) throws Il
5658
return loadServicesList(Logging.class, classLoader, true);
5759
}
5860

61+
/**
62+
* Load the highest {@link Prioritized priority} {@link Logging} from SPI
63+
*
64+
* @return non-null
65+
* @throws IllegalArgumentException
66+
*/
67+
@Nonnull
68+
public static Logging load() throws IllegalArgumentException {
69+
return load(getClassLoader(LoggingUtils.class));
70+
}
71+
72+
/**
73+
* Load the highest {@link Prioritized priority} {@link Logging} from SPI
74+
*
75+
* @param classLoader the {@link ClassLoader} which is used to load {@link Logging} implementations
76+
* @return non-null
77+
* @throws IllegalArgumentException
78+
*/
79+
@Nonnull
80+
public static Logging load(@Nullable ClassLoader classLoader) throws IllegalArgumentException {
81+
return loadFirstService(Logging.class, classLoader, true);
82+
}
83+
5984
private LoggingUtils() {
6085
}
61-
}
86+
}

microsphere-logging-commons/src/test/java/io/microsphere/logging/LoggingUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
import java.util.List;
2424

25+
import static io.microsphere.logging.LoggingUtils.load;
2526
import static io.microsphere.logging.LoggingUtils.loadAll;
2627
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2729

2830
/**
2931
* {@link LoggingUtils}
@@ -40,4 +42,10 @@ void testLoadAll() {
4042
assertEquals(2, loggings.size());
4143
assertEquals(loadAll(), loggings);
4244
}
45+
46+
@Test
47+
void testLoad() {
48+
Logging logging = load();
49+
assertInstanceOf(TestingLogging.class, logging);
50+
}
4351
}

microsphere-logging-commons/src/test/java/io/microsphere/logging/TestingLogging.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ public void init(String... loggerNamesAndLevels) {
7070
public String getName() {
7171
return "Testing";
7272
}
73+
74+
@Override
75+
public int getPriority() {
76+
return MAX_PRIORITY;
77+
}
7378
}

0 commit comments

Comments
 (0)