Skip to content

Commit e313a14

Browse files
authored
Merge pull request #13 from microsphere-projects/dev
Sync 'dev' branch
2 parents dbebaf5 + a42c9ea commit e313a14

18 files changed

Lines changed: 207 additions & 79 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ We welcome your contributions! Please read [Code of Conduct](./CODE_OF_CONDUCT.m
218218
- [microsphere-logging-test](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-logging-test)
219219
- [microsphere-java-logging](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-java-logging)
220220
- [microsphere-logback](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-logback)
221+
- [microsphere-log4j](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-log4j)
221222
- [microsphere-log4j2](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-log4j2)
222223

223224
## License

microsphere-java-logging/src/main/java/io/microsphere/logging/jdk/JavaLogging.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
*/
3737
public class JavaLogging implements Logging {
3838

39+
/**
40+
* The root logger name : ""
41+
*/
42+
public static final String ROOT_LOGGER_NAME = "";
43+
3944
/**
4045
* The priority of {@link JavaLogging}
4146
*/
@@ -50,6 +55,11 @@ public class JavaLogging implements Logging {
5055

5156
static final LoggingMXBean loggingMXBean = getLoggingMXBean();
5257

58+
@Override
59+
public String getRootLoggerName() {
60+
return ROOT_LOGGER_NAME;
61+
}
62+
5363
@Override
5464
public List<String> getLoggerNames() {
5565
return loggingMXBean.getLoggerNames();

microsphere-java-logging/src/test/java/io/microsphere/logging/jdk/JavaLoggingTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static io.microsphere.logging.LoggingUtils.load;
2929
import static io.microsphere.logging.jdk.JavaLogging.ALL_LEVELS;
3030
import static io.microsphere.logging.jdk.JavaLogging.PRIORITY;
31+
import static io.microsphere.logging.jdk.JavaLogging.ROOT_LOGGER_NAME;
3132
import static io.microsphere.logging.jdk.JavaLogging.loggingMXBean;
3233
import static io.microsphere.util.StringUtils.isBlank;
3334
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -46,66 +47,72 @@ class JavaLoggingTest {
4647
*/
4748
public static final Set<String> JAVA_LOGGING_LEVELS = ofSet("OFF", "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST", "ALL");
4849

49-
private JavaLogging JavaLogging;
50+
private JavaLogging logging;
5051

5152
@BeforeEach
5253
void setUp() {
53-
this.JavaLogging = (JavaLogging) load();
54+
this.logging = (JavaLogging) load();
5455
}
5556

5657
@Test
5758
void testConstants() {
59+
assertEquals("", ROOT_LOGGER_NAME);
5860
assertEquals(10, PRIORITY);
5961
assertEquals(JAVA_LOGGING_LEVELS, ALL_LEVELS);
6062
}
6163

64+
@Test
65+
void testGetRootLoggerName() {
66+
assertEquals(ROOT_LOGGER_NAME, this.logging.getRootLoggerName());
67+
}
68+
6269
@Test
6370
void testGetLoggerNames() {
64-
assertEquals(this.JavaLogging.getLoggerNames(), loggingMXBean.getLoggerNames());
71+
assertEquals(this.logging.getLoggerNames(), loggingMXBean.getLoggerNames());
6572
}
6673

6774
@Test
6875
void testGetSupportedLoggingLevels() {
69-
assertEquals(JAVA_LOGGING_LEVELS, this.JavaLogging.getSupportedLoggingLevels());
76+
assertEquals(JAVA_LOGGING_LEVELS, this.logging.getSupportedLoggingLevels());
7077
}
7178

7279
@Test
7380
void testGetLoggerLevel() {
74-
List<String> loggerNames = this.JavaLogging.getLoggerNames();
75-
loggerNames.forEach(loggerName -> assertEquals(this.JavaLogging.getLoggerLevel(loggerName), loggingMXBean.getLoggerLevel(loggerName)));
81+
List<String> loggerNames = this.logging.getLoggerNames();
82+
loggerNames.forEach(loggerName -> assertEquals(this.logging.getLoggerLevel(loggerName), loggingMXBean.getLoggerLevel(loggerName)));
7683
}
7784

7885
@Test
7986
void testSetLoggerLevel() {
80-
List<String> loggerNames = this.JavaLogging.getLoggerNames();
87+
List<String> loggerNames = this.logging.getLoggerNames();
8188
for (String loggerName : loggerNames) {
82-
String level = this.JavaLogging.getLoggerLevel(loggerName);
89+
String level = this.logging.getLoggerLevel(loggerName);
8390
if (isBlank(level)) {
8491
continue;
8592
}
8693
for (String newLevel : ALL_LEVELS) {
87-
this.JavaLogging.setLoggerLevel(loggerName, newLevel);
88-
assertEquals(newLevel, this.JavaLogging.getLoggerLevel(loggerName));
94+
this.logging.setLoggerLevel(loggerName, newLevel);
95+
assertEquals(newLevel, this.logging.getLoggerLevel(loggerName));
8996
}
90-
this.JavaLogging.setLoggerLevel(loggerName, level);
97+
this.logging.setLoggerLevel(loggerName, level);
9198
}
9299
}
93100

94101
@Test
95102
void testGetParentLoggerName() {
96-
List<String> loggerNames = this.JavaLogging.getLoggerNames();
103+
List<String> loggerNames = this.logging.getLoggerNames();
97104
for (String loggerName : loggerNames) {
98-
assertEquals(this.JavaLogging.getParentLoggerName(loggerName), loggingMXBean.getParentLoggerName(loggerName));
105+
assertEquals(this.logging.getParentLoggerName(loggerName), loggingMXBean.getParentLoggerName(loggerName));
99106
}
100107
}
101108

102109
@Test
103110
void testGetName() {
104-
assertEquals("Java Logging", this.JavaLogging.getName());
111+
assertEquals("Java Logging", this.logging.getName());
105112
}
106113

107114
@Test
108115
void testGetPriority() {
109-
assertEquals(PRIORITY, this.JavaLogging.getPriority());
116+
assertEquals(PRIORITY, this.logging.getPriority());
110117
}
111118
}

microsphere-log4j/src/main/java/io/microsphere/logging/log4j/Log4j2Logger.java renamed to microsphere-log4j/src/main/java/io/microsphere/logging/log4j/Log4jLogger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
* @see Logger
3434
* @since 1.0.0
3535
*/
36-
class Log4j2Logger extends AbstractLogger implements DelegatingWrapper {
36+
class Log4jLogger extends AbstractLogger implements DelegatingWrapper {
3737

3838
private final Logger logger;
3939

40-
public Log4j2Logger(String loggerName) {
40+
public Log4jLogger(String loggerName) {
4141
super(loggerName);
4242
this.logger = getLogger(loggerName);
4343
}

microsphere-log4j/src/main/java/io/microsphere/logging/log4j/Log4jLoggerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected String getDelegateLoggerClassName() {
4040

4141
@Override
4242
public Logger createLogger(String name) {
43-
return new Log4j2Logger(name);
43+
return new Log4jLogger(name);
4444
}
4545

4646
@Override

microsphere-log4j/src/main/java/io/microsphere/logging/log4j/Log4jLogging.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Set;
2929

3030
import static io.microsphere.collection.CollectionUtils.toIterable;
31-
import static io.microsphere.constants.SymbolConstants.DOT;
3231
import static io.microsphere.logging.DefaultLoggingLevelsResolver.INSTANCE;
3332
import static io.microsphere.logging.log4j.util.LoggerUtils.ROOT_LOGGER_NAME;
3433
import static io.microsphere.logging.log4j.util.LoggerUtils.getLevelString;
@@ -57,6 +56,11 @@ public class Log4jLogging implements Logging {
5756
*/
5857
public static final Set<String> ALL_LEVELS = INSTANCE.resolve(Level.class);
5958

59+
@Override
60+
public String getRootLoggerName() {
61+
return ROOT_LOGGER_NAME;
62+
}
63+
6064
@Override
6165
public List<String> getLoggerNames() {
6266
LoggerRepository loggerRepository = getLoggerRepository();
@@ -82,19 +86,6 @@ public void setLoggerLevel(String loggerName, String levelName) {
8286
LoggerUtils.setLoggerLevel(loggerName, levelName);
8387
}
8488

85-
@Override
86-
public String getParentLoggerName(String loggerName) {
87-
if (ROOT_LOGGER_NAME.equals(loggerName)) {
88-
return null;
89-
}
90-
int lastDotIndex = loggerName.lastIndexOf(DOT);
91-
if (lastDotIndex == -1) {
92-
return ROOT_LOGGER_NAME;
93-
}
94-
String parentLoggerName = loggerName.substring(0, lastDotIndex);
95-
return parentLoggerName;
96-
}
97-
9889
@Override
9990
public String getName() {
10091
return "Log4j";

microsphere-log4j/src/main/java/io/microsphere/logging/log4j/util/LoggerUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static org.apache.log4j.LogManager.getRootLogger;
3030

3131
/**
32-
* The Utilities class of Log4j2 Logger
32+
* The Utilities class of Log4j Logger
3333
*
3434
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
3535
* @see Logger
@@ -39,6 +39,9 @@ public abstract class LoggerUtils {
3939

4040
static final LoggerRepository loggerRepository = LogManager.getLoggerRepository();
4141

42+
/**
43+
* The name of Root Logger: "root"
44+
*/
4245
public static final String ROOT_LOGGER_NAME = getRootLogger().getName();
4346

4447
/**

microsphere-log4j/src/test/java/io/microsphere/logging/log4j/Log4JLoggingTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ void setUp() {
5959

6060
@Test
6161
void testConstants() {
62+
assertEquals("root", ROOT_LOGGER_NAME);
6263
assertEquals(-3, PRIORITY);
6364
assertEquals(LOG4J_LEVELS, ALL_LEVELS);
6465
}
6566

67+
@Test
68+
void testGetRootLoggerName() {
69+
assertEquals(ROOT_LOGGER_NAME, this.logging.getRootLoggerName());
70+
}
71+
6672
@Test
6773
void testGetLoggerNames() {
6874
assertFalse(this.logging.getLoggerNames().isEmpty());

microsphere-log4j/src/test/java/io/microsphere/logging/log4j/Log4jLoggerFactoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void testGetDelegateLoggerClassName() {
5151
@Test
5252
void testCreateLogger() {
5353
Logger logger = factory.createLogger("name");
54-
assertInstanceOf(Log4j2Logger.class, logger);
54+
assertInstanceOf(Log4jLogger.class, logger);
5555
}
5656

5757
@Test

microsphere-log4j/src/test/java/io/microsphere/logging/log4j/Log4jLoggerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@
3030
import static org.junit.jupiter.api.Assertions.assertTrue;
3131

3232
/**
33-
* {@link Log4j2Logger} Test
33+
* {@link Log4jLogger} Test
3434
*
3535
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
36-
* @see Log4j2Logger
36+
* @see Log4jLogger
3737
* @since 1.0.0
3838
*/
3939
class Log4jLoggerTest {
4040

4141
private static final String LOGGER_NAME = Log4jLoggerTest.class.getName();
4242

43-
protected Log4j2Logger logger;
43+
protected Log4jLogger logger;
4444

4545
@BeforeEach
4646
void setUp() {
47-
this.logger = new Log4j2Logger(LOGGER_NAME);
47+
this.logger = new Log4jLogger(LOGGER_NAME);
4848
}
4949

5050
@Test

0 commit comments

Comments
 (0)