This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch release-2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit fb7434925cf3af7f447bea654efd0f72eb48d5fd Author: Gary Gregory <[email protected]> AuthorDate: Sun Jan 9 14:57:43 2022 -0500 Add org.apache.logging.log4j.spi.LoggerContext.getLoggerRegistry(). - Add org.apache.logging.log4j.core.LoggerContext.getLoggerRegistry() - Add org.apache.logging.log4j.simple.SimpleLoggerContext.getLoggerRegistry(). - Move and make final SimpleLoggerContext singleton declaration from SimpleLoggerContextFactory to SimpleLoggerContext. - Add and use singleton SimpleLoggerContextFactory (stateless). - Split commit 1/2 for cherry-picking to master. --- .../java/org/apache/logging/log4j/LogManager.java | 26 +++++++++++----------- .../logging/log4j/simple/SimpleLoggerContext.java | 15 ++++++++++++- .../log4j/simple/SimpleLoggerContextFactory.java | 18 ++++++++------- .../apache/logging/log4j/spi/LoggerContext.java | 12 ++++++++++ .../apache/logging/log4j/core/LoggerContext.java | 10 +++++++++ 5 files changed, 59 insertions(+), 22 deletions(-) diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java index 46936c0..88f3168 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java @@ -54,7 +54,7 @@ public class LogManager { public static final String FACTORY_PROPERTY_NAME = "log4j2.loggerContextFactory"; /** - * The name of the root Logger. + * The name of the root Logger is {@value #ROOT_LOGGER_NAME}. */ public static final String ROOT_LOGGER_NAME = Strings.EMPTY; @@ -103,7 +103,7 @@ public class LogManager { if (factories.isEmpty()) { LOGGER.error("Log4j2 could not find a logging implementation. " + "Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."); - factory = new SimpleLoggerContextFactory(); + factory = SimpleLoggerContextFactory.INSTANCE; } else if (factories.size() == 1) { factory = factories.get(factories.lastKey()); } else { @@ -120,7 +120,7 @@ public class LogManager { } else { LOGGER.error("Log4j2 could not find a logging implementation. " + "Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."); - factory = new SimpleLoggerContextFactory(); + factory = SimpleLoggerContextFactory.INSTANCE; } LogManagerStatus.setInitialized(true); } @@ -157,7 +157,7 @@ public class LogManager { return factory.getContext(FQCN, null, null, true); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, null, null, true); + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, null, null, true); } } @@ -176,7 +176,7 @@ public class LogManager { return factory.getContext(FQCN, null, null, currentContext, null, null); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, null, null, currentContext, null, null); + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, null, null, currentContext, null, null); } } @@ -196,7 +196,7 @@ public class LogManager { return factory.getContext(FQCN, loader, null, currentContext); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, loader, null, currentContext); + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, null, currentContext); } } @@ -218,7 +218,7 @@ public class LogManager { return factory.getContext(FQCN, loader, externalContext, currentContext); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, loader, externalContext, currentContext); + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, externalContext, currentContext); } } @@ -240,7 +240,7 @@ public class LogManager { return factory.getContext(FQCN, loader, null, currentContext, configLocation, null); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, loader, null, currentContext, configLocation, + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, null, currentContext, configLocation, null); } } @@ -264,7 +264,7 @@ public class LogManager { return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, null); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, loader, externalContext, currentContext, + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, externalContext, currentContext, configLocation, null); } } @@ -289,7 +289,7 @@ public class LogManager { return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, name); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(FQCN, loader, externalContext, currentContext, + return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, externalContext, currentContext, configLocation, name); } } @@ -309,7 +309,7 @@ public class LogManager { return factory.getContext(fqcn, null, null, currentContext); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(fqcn, null, null, currentContext); + return SimpleLoggerContextFactory.INSTANCE.getContext(fqcn, null, null, currentContext); } } @@ -331,7 +331,7 @@ public class LogManager { return factory.getContext(fqcn, loader, null, currentContext); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(fqcn, loader, null, currentContext); + return SimpleLoggerContextFactory.INSTANCE.getContext(fqcn, loader, null, currentContext); } } @@ -356,7 +356,7 @@ public class LogManager { return factory.getContext(fqcn, loader, null, currentContext, configLocation, name); } catch (final IllegalStateException ex) { LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); - return new SimpleLoggerContextFactory().getContext(fqcn, loader, null, currentContext); + return SimpleLoggerContextFactory.INSTANCE.getContext(fqcn, loader, null, currentContext); } } diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java index 109798c..ac14f37 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java @@ -19,7 +19,6 @@ package org.apache.logging.log4j.simple; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; -import java.nio.file.Files; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.message.MessageFactory; @@ -34,6 +33,9 @@ import org.apache.logging.log4j.util.PropertiesUtil; */ public class SimpleLoggerContext implements LoggerContext { + /** Singleton instance. */ + static final SimpleLoggerContext INSTANCE = new SimpleLoggerContext(); + private static final String SYSTEM_OUT = "system.out"; private static final String SYSTEM_ERR = "system.err"; @@ -126,6 +128,17 @@ public class SimpleLoggerContext implements LoggerContext { return loggerRegistry.getLogger(name, messageFactory); } + /** + * Gets the LoggerRegistry. + * + * @return the LoggerRegistry. + * @since 2.17.2 + */ + @Override + public LoggerRegistry<ExtendedLogger> getLoggerRegistry() { + return loggerRegistry; + } + @Override public boolean hasLogger(final String name) { return false; diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContextFactory.java b/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContextFactory.java index b78a3de..bf9c577 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContextFactory.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContextFactory.java @@ -22,22 +22,24 @@ import org.apache.logging.log4j.spi.LoggerContext; import org.apache.logging.log4j.spi.LoggerContextFactory; /** - * + * Simple and stateless {@link LoggerContextFactory}. */ public class SimpleLoggerContextFactory implements LoggerContextFactory { - private static LoggerContext context = new SimpleLoggerContext(); + /** + * Singleton instance. + */ + public static final SimpleLoggerContextFactory INSTANCE = new SimpleLoggerContextFactory(); @Override - public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, - final boolean currentContext) { - return context; + public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext) { + return SimpleLoggerContext.INSTANCE; } @Override - public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, - final boolean currentContext, final URI configLocation, final String name) { - return context; + public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext, + final URI configLocation, final String name) { + return SimpleLoggerContext.INSTANCE; } @Override diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerContext.java index 61155a1..29be5ac 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerContext.java @@ -16,6 +16,7 @@ */ package org.apache.logging.log4j.spi; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.MessageFactory; /** @@ -40,6 +41,7 @@ public interface LoggerContext { return getLogger(canonicalName != null ? canonicalName : cls.getName()); } + /** * Gets an ExtendedLogger using the fully qualified name of the Class as the Logger name. * @param cls The Class whose name should be used as the Logger name. @@ -70,6 +72,16 @@ public interface LoggerContext { ExtendedLogger getLogger(String name, MessageFactory messageFactory); /** + * Gets the LoggerRegistry. + * + * @return the LoggerRegistry. + * @since 2.17.2 + */ + default LoggerRegistry<? extends Logger> getLoggerRegistry() { + return null; + } + + /** * Gets an object by its name. * @param key The object's key. * @return The Object that is associated with the key, if any. diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java index 6054e34..f169a34 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java @@ -535,6 +535,16 @@ public class LoggerContext extends AbstractLifeCycle } /** + * Gets the LoggerRegistry. + * + * @return the LoggerRegistry. + * @since 2.17.2 + */ + public LoggerRegistry<Logger> getLoggerRegistry() { + return loggerRegistry; + } + + /** * Determines if the specified Logger exists. * * @param name The Logger name to search for.
