Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ We welcome your contributions! Please read [Code of Conduct](./CODE_OF_CONDUCT.m
- [microsphere-logging-test](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-logging-test)
- [microsphere-java-logging](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-java-logging)
- [microsphere-logback](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-logback)
- [microsphere-log4j](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-log4j)
- [microsphere-log4j2](https://javadoc.io/doc/io.github.microsphere-projects/microsphere-log4j2)

## License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
*/
public class JavaLogging implements Logging {

/**
* The root logger name : ""
*/
public static final String ROOT_LOGGER_NAME = "";

/**
* The priority of {@link JavaLogging}
*/
Expand All @@ -50,6 +55,11 @@ public class JavaLogging implements Logging {

static final LoggingMXBean loggingMXBean = getLoggingMXBean();

@Override
public String getRootLoggerName() {
return ROOT_LOGGER_NAME;
}

@Override
public List<String> getLoggerNames() {
return loggingMXBean.getLoggerNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static io.microsphere.logging.LoggingUtils.load;
import static io.microsphere.logging.jdk.JavaLogging.ALL_LEVELS;
import static io.microsphere.logging.jdk.JavaLogging.PRIORITY;
import static io.microsphere.logging.jdk.JavaLogging.ROOT_LOGGER_NAME;
import static io.microsphere.logging.jdk.JavaLogging.loggingMXBean;
import static io.microsphere.util.StringUtils.isBlank;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -46,66 +47,72 @@ class JavaLoggingTest {
*/
public static final Set<String> JAVA_LOGGING_LEVELS = ofSet("OFF", "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST", "ALL");

private JavaLogging JavaLogging;
private JavaLogging logging;

@BeforeEach
void setUp() {
this.JavaLogging = (JavaLogging) load();
this.logging = (JavaLogging) load();
}

@Test
void testConstants() {
assertEquals("", ROOT_LOGGER_NAME);
assertEquals(10, PRIORITY);
assertEquals(JAVA_LOGGING_LEVELS, ALL_LEVELS);
}

@Test
void testGetRootLoggerName() {
assertEquals(ROOT_LOGGER_NAME, this.logging.getRootLoggerName());
}

@Test
void testGetLoggerNames() {
assertEquals(this.JavaLogging.getLoggerNames(), loggingMXBean.getLoggerNames());
assertEquals(this.logging.getLoggerNames(), loggingMXBean.getLoggerNames());
}

@Test
void testGetSupportedLoggingLevels() {
assertEquals(JAVA_LOGGING_LEVELS, this.JavaLogging.getSupportedLoggingLevels());
assertEquals(JAVA_LOGGING_LEVELS, this.logging.getSupportedLoggingLevels());
}

@Test
void testGetLoggerLevel() {
List<String> loggerNames = this.JavaLogging.getLoggerNames();
loggerNames.forEach(loggerName -> assertEquals(this.JavaLogging.getLoggerLevel(loggerName), loggingMXBean.getLoggerLevel(loggerName)));
List<String> loggerNames = this.logging.getLoggerNames();
loggerNames.forEach(loggerName -> assertEquals(this.logging.getLoggerLevel(loggerName), loggingMXBean.getLoggerLevel(loggerName)));
}

@Test
void testSetLoggerLevel() {
List<String> loggerNames = this.JavaLogging.getLoggerNames();
List<String> loggerNames = this.logging.getLoggerNames();
for (String loggerName : loggerNames) {
String level = this.JavaLogging.getLoggerLevel(loggerName);
String level = this.logging.getLoggerLevel(loggerName);
if (isBlank(level)) {
continue;
}
for (String newLevel : ALL_LEVELS) {
this.JavaLogging.setLoggerLevel(loggerName, newLevel);
assertEquals(newLevel, this.JavaLogging.getLoggerLevel(loggerName));
this.logging.setLoggerLevel(loggerName, newLevel);
assertEquals(newLevel, this.logging.getLoggerLevel(loggerName));
}
this.JavaLogging.setLoggerLevel(loggerName, level);
this.logging.setLoggerLevel(loggerName, level);
}
}

@Test
void testGetParentLoggerName() {
List<String> loggerNames = this.JavaLogging.getLoggerNames();
List<String> loggerNames = this.logging.getLoggerNames();
for (String loggerName : loggerNames) {
assertEquals(this.JavaLogging.getParentLoggerName(loggerName), loggingMXBean.getParentLoggerName(loggerName));
assertEquals(this.logging.getParentLoggerName(loggerName), loggingMXBean.getParentLoggerName(loggerName));
}
}

@Test
void testGetName() {
assertEquals("Java Logging", this.JavaLogging.getName());
assertEquals("Java Logging", this.logging.getName());
}

@Test
void testGetPriority() {
assertEquals(PRIORITY, this.JavaLogging.getPriority());
assertEquals(PRIORITY, this.logging.getPriority());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
* @see Logger
* @since 1.0.0
*/
class Log4j2Logger extends AbstractLogger implements DelegatingWrapper {
class Log4jLogger extends AbstractLogger implements DelegatingWrapper {

private final Logger logger;

public Log4j2Logger(String loggerName) {
public Log4jLogger(String loggerName) {
super(loggerName);
this.logger = getLogger(loggerName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected String getDelegateLoggerClassName() {

@Override
public Logger createLogger(String name) {
return new Log4j2Logger(name);
return new Log4jLogger(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Set;

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

@Override
public String getRootLoggerName() {
return ROOT_LOGGER_NAME;
}

@Override
public List<String> getLoggerNames() {
LoggerRepository loggerRepository = getLoggerRepository();
Expand All @@ -82,19 +86,6 @@ public void setLoggerLevel(String loggerName, String levelName) {
LoggerUtils.setLoggerLevel(loggerName, levelName);
}

@Override
public String getParentLoggerName(String loggerName) {
if (ROOT_LOGGER_NAME.equals(loggerName)) {
return null;
}
int lastDotIndex = loggerName.lastIndexOf(DOT);
if (lastDotIndex == -1) {
return ROOT_LOGGER_NAME;
}
String parentLoggerName = loggerName.substring(0, lastDotIndex);
return parentLoggerName;
}

@Override
public String getName() {
return "Log4j";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static org.apache.log4j.LogManager.getRootLogger;

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

static final LoggerRepository loggerRepository = LogManager.getLoggerRepository();

/**
* The name of Root Logger: "root"
*/
public static final String ROOT_LOGGER_NAME = getRootLogger().getName();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ void setUp() {

@Test
void testConstants() {
assertEquals("root", ROOT_LOGGER_NAME);
assertEquals(-3, PRIORITY);
assertEquals(LOG4J_LEVELS, ALL_LEVELS);
}

@Test
void testGetRootLoggerName() {
assertEquals(ROOT_LOGGER_NAME, this.logging.getRootLoggerName());
}

@Test
void testGetLoggerNames() {
assertFalse(this.logging.getLoggerNames().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void testGetDelegateLoggerClassName() {
@Test
void testCreateLogger() {
Logger logger = factory.createLogger("name");
assertInstanceOf(Log4j2Logger.class, logger);
assertInstanceOf(Log4jLogger.class, logger);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link Log4j2Logger} Test
* {@link Log4jLogger} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see Log4j2Logger
* @see Log4jLogger
* @since 1.0.0
*/
class Log4jLoggerTest {

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

protected Log4j2Logger logger;
protected Log4jLogger logger;

@BeforeEach
void setUp() {
this.logger = new Log4j2Logger(LOGGER_NAME);
this.logger = new Log4jLogger(LOGGER_NAME);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import java.util.Set;

import static io.microsphere.constants.SymbolConstants.DOT;
import static io.microsphere.logging.DefaultLoggingLevelsResolver.INSTANCE;
import static io.microsphere.logging.log4j2.util.LoggerUtils.getLevelString;
import static io.microsphere.logging.log4j2.util.LoggerUtils.getLoggerContext;
Expand Down Expand Up @@ -53,6 +52,11 @@ public class Log4j2Logging implements Logging {
*/
public static final Set<String> ALL_LEVELS = INSTANCE.resolve(Level.class);

@Override
public String getRootLoggerName() {
return ROOT_LOGGER_NAME;
}

@Override
public List<String> getLoggerNames() {
return getLoggerContext()
Expand All @@ -76,19 +80,6 @@ public void setLoggerLevel(String loggerName, String levelName) {
LoggerUtils.setLoggerLevel(loggerName, levelName);
}

@Override
public String getParentLoggerName(String loggerName) {
if (ROOT_LOGGER_NAME.equals(loggerName)) {
return null;
}
int lastDotIndex = loggerName.lastIndexOf(DOT);
if (lastDotIndex == -1) {
return ROOT_LOGGER_NAME;
}
String parentLoggerName = loggerName.substring(0, lastDotIndex);
return parentLoggerName;
}

@Override
public String getName() {
return "Log4j2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void testConstants() {
assertEquals(LOG4J2_LEVELS, ALL_LEVELS);
}

@Test
void testGetRootLoggerName() {
assertEquals(ROOT_LOGGER_NAME, this.logging.getRootLoggerName());
}

@Test
void testGetLoggerNames() {
assertFalse(this.logging.getLoggerNames().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import java.util.Set;

import static io.microsphere.constants.SymbolConstants.DOT;
import static io.microsphere.logging.DefaultLoggingLevelsResolver.INSTANCE;
import static io.microsphere.logging.logback.util.LoggerUtils.getLevelString;
import static io.microsphere.logging.logback.util.LoggerUtils.getLoggerContext;
Expand Down Expand Up @@ -53,6 +52,11 @@ public class LogbackLogging implements Logging {
*/
public static final Set<String> ALL_LEVELS = INSTANCE.resolve(Level.class);

@Override
public String getRootLoggerName() {
return ROOT_LOGGER_NAME;
}

@Override
public List<String> getLoggerNames() {
return getLoggerContext()
Expand All @@ -76,19 +80,6 @@ public void setLoggerLevel(String loggerName, String levelName) {
LoggerUtils.setLoggerLevel(loggerName, levelName);
}

@Override
public String getParentLoggerName(String loggerName) {
if (ROOT_LOGGER_NAME.equals(loggerName)) {
return null;
}
int lastDotIndex = loggerName.lastIndexOf(DOT);
if (lastDotIndex == -1) {
return ROOT_LOGGER_NAME;
}
String parentLoggerName = loggerName.substring(0, lastDotIndex);
return parentLoggerName;
}

@Override
public String getName() {
return "Logback";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ void testConstants() {
assertEquals(LOGBACK_LEVELS, ALL_LEVELS);
}

@Test
void testGetRootLoggerName() {
assertEquals(ROOT_LOGGER_NAME, this.logging.getRootLoggerName());
}

@Test
void testGetLoggerNames() {
assertFalse(this.logging.getLoggerNames().isEmpty());
Expand Down
Loading
Loading