vy commented on code in PR #2249:
URL: https://github.com/apache/logging-log4j2/pull/2249#discussion_r1513500994


##########
log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java:
##########
@@ -16,311 +16,590 @@
  */
 package org.apache.logging.log4j.status;
 
-import java.io.Closeable;
+import static java.util.Objects.requireNonNull;
+
+import edu.umd.cs.findbugs.annotations.Nullable;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.net.URL;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Properties;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
-import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.message.ParameterizedNoReferenceMessageFactory;
-import org.apache.logging.log4j.simple.SimpleLogger;
-import org.apache.logging.log4j.simple.SimpleLoggerContext;
 import org.apache.logging.log4j.spi.AbstractLogger;
 import org.apache.logging.log4j.util.Constants;
-import org.apache.logging.log4j.util.PropertiesUtil;
 
 /**
- * Records events that occur in the logging system. By default, only error 
messages are logged to {@link System#err}.
- * Normally, the Log4j StatusLogger is configured via the root {@code 
<Configuration status="LEVEL"/>} node in a Log4j
- * configuration file. However, this can be overridden via a system property 
named
- * {@value #DEFAULT_STATUS_LISTENER_LEVEL} and will work with any Log4j 
provider.
- *
- * @see SimpleLogger
- * @see SimpleLoggerContext
+ * Records events that occur in the logging system.
+ * {@link StatusLogger} is expected to be a standalone, self-sufficient 
component that the logging system can rely on for low-level logging purposes.
+ * <h3>Listeners</h3>
+ * <p>
+ * Each recorded event will first get buffered and used to notify the 
registered {@link StatusListener}s.
+ * Listener registry is always initialized with a <em>default listener</em>, 
which is a {@link StatusConsoleListener}.
+ * </p>
+ * <p>
+ * You can programmatically register listeners using {@link 
#registerListener(StatusListener)} method.
+ * </p>
+ * <h3>Configuration</h3>
+ * <p>
+ * The {@code StatusLogger} can be configured in following ways:
+ * </p>
+ * <ol>
+ * <li>Passing system properties to the Java process (e.g., {@code 
-Dlog4j2.StatusLogger.level=INFO})</li>
+ * <li>Providing properties in a {@value StatusLogger#PROPERTIES_FILE_NAME} 
file in the classpath</li>
+ * <li>Using Log4j configuration (i.e., {@code <Configuration status="WARN" 
dest="out">} in a {@code log4j2.xml} in the classpath)</li>
+ * </ol>
+ * <p>
+ * It is crucial to understand that there is a time between the first {@code 
StatusLogger} access and a configuration file (e.g., {@code log4j2.xml}) read.
+ * Consider the following example:
+ * </p>
+ * <ol>
+ * <li>The default level is {@code ERROR}</li>
+ * <li>You have <Configuration status="WARN">} in your {@code log4j2.xml}</li>
+ * <li>Until your {@code log4j2.xml} configuration is read, the effective 
level will be {@code ERROR}</li>
+ * <li>Once your {@code log4j2.xml} configuration is read, the effective level 
will be {@code WARN} as you configured</li>
+ * </ol>
+ * <p>
+ * Hence, unless you use either system properties or {@value 
StatusLogger#PROPERTIES_FILE_NAME} file in the classpath, there is a time 
window that only the defaults will be effective.
+ * </p>
+ * <p>
+ * {@code StatusLogger} is designed as a singleton class accessed statically.
+ * If you are running an application containing multiple Log4j configurations 
(e.g., in a servlet environment with multiple containers) and you happen to 
have differing {@code StatusLogger} configurations (e.g, one {@code log4j2.xml} 
containing {@code <Configuration status="ERROR">} while the other {@code 
<Configuration status="INFO">}), the last loaded configuration will be 
effective one.
+ * </p>
+ * <h3>Debug mode</h3>
+ * <p>
+ * When the {@value Constants#LOG4J2_DEBUG} system property is present, any 
level-related filtering will be skipped and all events will be notified to 
listeners.
+ * </p>
  */
-public final class StatusLogger extends AbstractLogger {
+public class StatusLogger extends AbstractLogger {
+
+    private static final long serialVersionUID = 2L;
+
+    /**
+     * The name of the system property that enables debug mode in its presence.
+     * <p>
+     * This is a local clone of {@link Constants#LOG4J2_DEBUG}.
+     * The cloning is necessary to avoid cyclic initialization.
+     * </p>
+     */
+    public static final String DEBUG_PROPERTY_NAME = "log4j2.debug";
+
+    /**
+     * The name of the system property that can be configured with the maximum 
number of events buffered.
+     * <p>
+     * Once the limit is reached, older entries will be removed as new entries 
are added.
+     * </p>
+     *
+     * @since 2.23.0
+     */
+    public static final String BUFFER_CAPACITY_PROPERTY_NAME = 
"log4j2.status.entries";
+
+    /**
+     * The default value of the {@link #BUFFER_CAPACITY_PROPERTY_NAME} system 
property: {@code 0}.
+     *
+     * @since 2.23.0
+     */
+    public static final int BUFFER_CAPACITY_DEFAULT_VALUE = 0;
 
     /**
-     * System property that can be configured with the number of entries in 
the queue. Once the limit is reached older
-     * entries will be removed as new entries are added.
+     * The name of the system property that can be configured with the maximum 
number of events buffered.
+     * <p>
+     * Once the limit is reached, older entries will be removed as new entries 
are added.
+     * </p>
+     *
+     * @deprecated Use {@link #BUFFER_CAPACITY_PROPERTY_NAME} instead.
+     */
+    @Deprecated
+    public static final String MAX_STATUS_ENTRIES = 
BUFFER_CAPACITY_PROPERTY_NAME;
+
+    /**
+     * The name of the system property that can be configured with the {@link 
Level} name to use as the default listener level.
+     * <p>
+     * The listener registry is initialized with a default listener.
+     * This default listener will accept entries filtered by the level 
provided in this configuration.
+     * </p>
+     *
+     * @since 2.23.0
+     */
+    public static final String DEFAULT_LISTENER_LEVEL_PROPERTY_NAME = 
"log4j2.StatusLogger.level";
+
+    /**
+     * The default value of the {@link #DEFAULT_LISTENER_LEVEL_PROPERTY_NAME} 
system property: {@code ERROR}.
+     *
+     * @since 2.23.0
+     */
+    public static final Level DEFAULT_LISTENER_LEVEL_DEFAULT_VALUE = 
Level.ERROR;
+
+    /**
+     * The name of the system property that can be configured with the {@link 
Level} name to use as the default listener level.
+     * <p>
+     * The listener registry is initialized with a default listener.
+     * This default listener will accept entries filtered by the level 
provided in this configuration.
+     * </p>
+     *
+     * @since 2.8
+     * @deprecated Use {@link #DEFAULT_LISTENER_LEVEL_PROPERTY_NAME} instead.
      */
-    public static final String MAX_STATUS_ENTRIES = "log4j2.status.entries";
+    @Deprecated
+    public static final String DEFAULT_STATUS_LISTENER_LEVEL = 
DEFAULT_LISTENER_LEVEL_PROPERTY_NAME;
 
     /**
-     * System property that can be configured with the {@link Level} name to 
use as the default level for
-     * {@link StatusListener}s.
+     * The name of the system property that can be configured with a {@link 
java.time.format.DateTimeFormatter} pattern that will be passed to the default 
listener.
+     *
+     * @since 2.23.0
      */
-    public static final String DEFAULT_STATUS_LISTENER_LEVEL = 
"log4j2.StatusLogger.level";
+    public static final String INSTANT_FORMAT_PROPERTY_NAME = 
"log4j2.StatusLogger.DateFormat";
 
     /**
-     * System property that can be configured with a date-time format string 
to use as the format for timestamps
-     * in the status logger output. See {@link java.text.SimpleDateFormat} for 
supported formats.
+     * The name of the system property that can be configured with a {@link 
java.time.format.DateTimeFormatter} pattern that will be passed to the default 
listener.
+     *
      * @since 2.11.0
+     * @deprecated Use {@link #INSTANT_FORMAT_PROPERTY_NAME} instead.
      */
-    public static final String STATUS_DATE_FORMAT = 
"log4j2.StatusLogger.DateFormat";
+    @Deprecated
+    public static final String STATUS_DATE_FORMAT = 
INSTANT_FORMAT_PROPERTY_NAME;
 
-    private static final long serialVersionUID = 2L;
+    /**
+     * The name of the file to be searched in the classpath to read properties 
from.
+     *
+     * @since 2.23.0
+     */
+    public static final String PROPERTIES_FILE_NAME = 
"log4j2.StatusLogger.properties";
+
+    /**
+     * Holder for user-provided {@link StatusLogger} configurations.
+     *
+     * @since 2.23.0
+     */
+    public static final class Config {
+
+        private static final Config INSTANCE = new Config();
+
+        private final boolean debugEnabled;
+
+        private final int bufferCapacity;
+
+        private final Level defaultListenerLevel;
+
+        @Nullable
+        private final DateTimeFormatter instantFormatter;
+
+        /**
+         * Constructs an instance using the given properties.
+         * <b>Users should not create new instances, but use {@link 
#getInstance()} instead</b>!
+         *
+         * @param debugEnabled the value of the {@value DEBUG_PROPERTY_NAME} 
property
+         * @param bufferCapacity the value of the {@value 
BUFFER_CAPACITY_PROPERTY_NAME} property
+         * @param defaultListenerLevel the value of the {@value 
DEFAULT_LISTENER_LEVEL_PROPERTY_NAME} property
+         * @param instantFormatter the value of the {@value 
INSTANT_FORMAT_PROPERTY_NAME} property
+         */
+        public Config(
+                boolean debugEnabled,
+                int bufferCapacity,
+                Level defaultListenerLevel,
+                @Nullable DateTimeFormatter instantFormatter) {
+            this.debugEnabled = debugEnabled;
+            if (bufferCapacity < 0) {
+                throw new IllegalArgumentException(
+                        "was expecting a positive `bufferCapacity`, found: " + 
bufferCapacity);
+            }
+            this.bufferCapacity = bufferCapacity;
+            this.defaultListenerLevel = requireNonNull(defaultListenerLevel, 
"defaultListenerLevel");
+            this.instantFormatter = requireNonNull(instantFormatter, 
"instantFormatter");
+        }
+
+        /**
+         * Constructs an instance using either system properties or a property 
file (i.e., {@value Config#PROPERTIES_FILE_NAME}) in the classpath, if 
available.
+         * <b>Users should not create new instances, but use {@link 
#getInstance()} instead</b>!
+         */
+        public Config() {
+            final Properties fileProvidedProperties = readPropertiesFile();
+            this.debugEnabled = readDebugEnabled(fileProvidedProperties);
+            this.bufferCapacity = readBufferCapacity(fileProvidedProperties);
+            this.defaultListenerLevel = 
readDefaultListenerLevel(fileProvidedProperties);
+            this.instantFormatter = 
readInstantFormatter(fileProvidedProperties);
+        }
+
+        /**
+         * Gets the static instance.
+         *
+         * @return a singleton instance
+         */
+        public static Config getInstance() {
+            return INSTANCE;
+        }
+
+        private static boolean readDebugEnabled(final Properties 
fileProvidedProperties) {
+            final String debug = readProperty(fileProvidedProperties, 
DEBUG_PROPERTY_NAME);
+            return debug != null;
+        }
+
+        private static int readBufferCapacity(final Properties 
fileProvidedProperties) {
+            final String capacityString = readProperty(fileProvidedProperties, 
BUFFER_CAPACITY_PROPERTY_NAME);
+            return capacityString != null ? Integer.parseInt(capacityString) : 
BUFFER_CAPACITY_DEFAULT_VALUE;
+        }
 
-    private static final String NOT_AVAIL = "?";
+        private static Level readDefaultListenerLevel(final Properties 
fileProvidedProperties) {
+            final String level = readProperty(fileProvidedProperties, 
DEFAULT_LISTENER_LEVEL_PROPERTY_NAME);
+            return level != null ? Level.valueOf(level) : 
DEFAULT_LISTENER_LEVEL_DEFAULT_VALUE;
+        }
 
-    static final PropertiesUtil PROPS = new 
PropertiesUtil("log4j2.StatusLogger.properties");
+        private static DateTimeFormatter readInstantFormatter(final Properties 
fileProvidedProperties) {
+            final String format = readProperty(fileProvidedProperties, 
INSTANT_FORMAT_PROPERTY_NAME);
+            return format != null ? DateTimeFormatter.ofPattern(format) : null;
+        }
 
-    private static final int MAX_ENTRIES = 
PROPS.getIntegerProperty(MAX_STATUS_ENTRIES, 200);
+        private static String readProperty(final Properties 
fileProvidedProperties, final String propertyName) {
+            final String systemProvidedValue = 
System.getProperty(propertyName);
+            return systemProvidedValue != null
+                    ? systemProvidedValue
+                    : (String) fileProvidedProperties.get(propertyName);
+        }
 
-    private static final String DEFAULT_STATUS_LEVEL = 
PROPS.getStringProperty(DEFAULT_STATUS_LISTENER_LEVEL);
+        // We need to roll out our own `.properties` reader.
+        // We could have used `PropertiesUtil`, `PropertyFilePropertySource`, 
etc.
+        // Consequently, they would delegate to `LoaderUtil`, etc.
+        // All these mechanisms expect a working `StatusLogger`.
+        // Hence, in order to be self-sufficient, we cannot rely on them.
+        private static Properties readPropertiesFile() {
+            final Properties properties = new Properties();
+            final URL url = 
StatusLogger.class.getResource(PROPERTIES_FILE_NAME);

Review Comment:
   @carterkozak, doh! :facepalm: Thanks for the report. Could you review #2354, 
please?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to