Try to simplify the StatusLogger stuff
git-svn-id: https://svn.apache.org/repos/asf/logging/log4j/log4j2/branches/LOG4J2-609@1610811 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
index 0b36f83..c57b8d0 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
@@ -44,7 +44,7 @@
* @param msg The message String.
* @param t The Error or Exception that occurred.
*/
- StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t) {
+ public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t) {
this.timestamp = System.currentTimeMillis();
this.caller = caller;
this.level = level;
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
index 9a248b0..c1abe23 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
@@ -18,6 +18,7 @@
import java.io.Closeable;
import java.io.IOException;
+import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -53,7 +54,7 @@
private static final String NOT_AVAIL = "?";
private static final PropertiesUtil PROPS = new PropertiesUtil("log4j2.StatusLogger.properties");
-
+
private static final int MAX_ENTRIES = PROPS.getIntegerProperty(MAX_STATUS_ENTRIES, 200);
private static final StatusLogger STATUS_LOGGER = new StatusLogger();
@@ -67,8 +68,11 @@
private final Lock msgLock = new ReentrantLock();
private StatusLogger() {
- this.logger = new SimpleLogger("StatusLogger", Level.ERROR, false, true, false, false, Strings.EMPTY, null, PROPS,
- System.err);
+ final Level consoleLevel = Level.toLevel(PROPS.getStringProperty("log4j2.StatusLogger.console.level"), Level.ERROR);
+ final PrintStream consoleDest ="out".equalsIgnoreCase(PROPS.getStringProperty("log4j2.StatusLogger.console.destination")) ? System.out : System.err;
+
+ this.logger = new SimpleLogger("StatusLogger", consoleLevel, false,
+ true, false, false, Strings.EMPTY, null, PROPS, consoleDest);
}
/**
@@ -182,6 +186,7 @@
*/
@Override
public void logMessage(final String fqcn, final Level level, final Marker marker, final Message msg, final Throwable t) {
+ logger.logMessage(fqcn, level, marker, msg, t);
StackTraceElement element = null;
if (fqcn != null) {
element = getStackTraceElement(fqcn, Thread.currentThread().getStackTrace());
@@ -193,14 +198,10 @@
} finally {
msgLock.unlock();
}
- if (listeners.size() > 0) {
- for (final StatusListener listener : listeners) {
- if (data.getLevel().isMoreSpecificThan(listener.getStatusLevel())) {
- listener.log(data);
- }
+ for (final StatusListener listener : listeners) {
+ if (data.getLevel().isMoreSpecificThan(listener.getStatusLevel())) {
+ listener.log(data);
}
- } else {
- logger.logMessage(fqcn, level, marker, msg, t);
}
}
@@ -250,10 +251,15 @@
@Override
public boolean isEnabled(final Level level, final Marker marker) {
- if (listeners.isEmpty()) {
- return false;
+ if (logger.isEnabled(level)) {
+ return true;
}
- return logger.isEnabled(level, marker);
+ for (StatusListener listener : listeners) {
+ if (level.isLessSpecificThan(listener.getStatusLevel())) {
+ return true;
+ }
+ }
+ return false;
}
/**
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java
index c22c50c..9598a1d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java
@@ -34,7 +34,6 @@
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
-import org.apache.logging.log4j.core.config.status.StatusConfiguration;
import org.apache.logging.log4j.core.util.Patterns;
import com.fasterxml.jackson.core.JsonParser;
@@ -69,19 +68,11 @@
}
}
processAttributes(rootNode, root);
- final StatusConfiguration statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES)
- .withStatus(getDefaultStatus());
for (final Map.Entry<String, String> entry : rootNode.getAttributes().entrySet()) {
final String key = entry.getKey();
final String value = getStrSubstitutor().replace(entry.getValue());
- if ("status".equalsIgnoreCase(key)) {
- statusConfig.withStatus(value);
- } else if ("dest".equalsIgnoreCase(key)) {
- statusConfig.withDestination(value);
- } else if ("shutdownHook".equalsIgnoreCase(key)) {
+ if ("shutdownHook".equalsIgnoreCase(key)) {
isShutdownHookEnabled = !"disable".equalsIgnoreCase(value);
- } else if ("verbose".equalsIgnoreCase(entry.getKey())) {
- statusConfig.withVerbosity(value);
} else if ("packages".equalsIgnoreCase(key)) {
final String[] packages = value.split(Patterns.COMMA_SEPARATOR);
for (final String p : packages) {
@@ -98,7 +89,6 @@
createAdvertiser(value, configSource, buffer, "application/json");
}
}
- statusConfig.initialize();
if (getName() == null) {
setName(configSource.getLocation());
}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java
deleted file mode 100644
index 837a85b..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.logging.log4j.core.config.status;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
-
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.status.StatusListener;
-import org.apache.logging.log4j.status.StatusLogger;
-
-/**
- * Configuration for setting up {@link StatusConsoleListener} instances.
- */
-public class StatusConfiguration {
-
- private static final Level DEFAULT_STATUS = Level.ERROR;
- private static final Verbosity DEFAULT_VERBOSITY = Verbosity.QUIET;
-
- private final Collection<String> errorMessages = Collections.synchronizedCollection(new LinkedList<String>());
- private final StatusLogger logger = StatusLogger.getLogger();
-
- private volatile boolean initialized = false;
-
- private Class<? extends StatusConsoleListener> destination = StatusStdOutListener.class;
- private Level status = DEFAULT_STATUS;
- private Verbosity verbosity = DEFAULT_VERBOSITY;
- private String[] verboseClasses;
-
- /**
- * Specifies how verbose the StatusLogger should be.
- */
- public static enum Verbosity {
- QUIET, VERBOSE;
-
- /**
- * Parses the verbosity property into an enum.
- *
- * @param value property value to parse.
- * @return enum corresponding to value, or QUIET by default.
- */
- public static Verbosity toVerbosity(final String value) {
- return Boolean.parseBoolean(value) ? VERBOSE : QUIET;
- }
- }
-
- /**
- * Logs an error message to the StatusLogger. If the StatusLogger hasn't been set up yet, queues the message to be
- * logged after initialization.
- *
- * @param message error message to log.
- */
- public void error(final String message) {
- if (!this.initialized) {
- this.errorMessages.add(message);
- } else {
- this.logger.error(message);
- }
- }
-
- /**
- * Specifies the destination for StatusLogger events. This can be {@code out} (default) for using
- * {@link System#out standard out}, {@code err} for using {@link System#err standard error}, or a file URI to
- * which log events will be written. If the provided URI is invalid, then the default destination of standard
- * out will be used.
- *
- * @param destination where status log messages should be output.
- * @return {@code this}
- */
- public StatusConfiguration withDestination(final String destination) {
- if ("out".equalsIgnoreCase(destination)) {
- this.destination = StatusStdOutListener.class;
- } else if ("err".equalsIgnoreCase(destination)) {
- this.destination = StatusStdErrListener.class;
- } else {
- this.error("Invalid destination [" + destination + "]. Only 'out' or 'err' are supported. Defaulting to 'out'.");
- this.destination = StatusStdOutListener.class;
- }
- return this;
- }
-
- /**
- * Specifies the logging level by name to use for filtering StatusLogger messages.
- *
- * @param status name of logger level to filter below.
- * @return {@code this}
- * @see Level
- */
- public StatusConfiguration withStatus(final String status) {
- this.status = Level.toLevel(status, null);
- if (this.status == null) {
- this.error("Invalid status level specified: " + status + ". Defaulting to ERROR.");
- this.status = Level.ERROR;
- }
- return this;
- }
-
- /**
- * Specifies the logging level to use for filtering StatusLogger messages.
- *
- * @param status logger level to filter below.
- * @return {@code this}
- */
- public StatusConfiguration withStatus(final Level status) {
- this.status = status;
- return this;
- }
-
- /**
- * Specifies the verbosity level to log at. This only applies to classes configured by
- * {@link #withVerboseClasses(String...) verboseClasses}.
- *
- * @param verbosity basic filter for status logger messages.
- * @return {@code this}
- */
- public StatusConfiguration withVerbosity(final String verbosity) {
- this.verbosity = Verbosity.toVerbosity(verbosity);
- return this;
- }
-
- /**
- * Specifies which class names to filter if the configured verbosity level is QUIET.
- *
- * @param verboseClasses names of classes to filter if not using VERBOSE.
- * @return {@code this}
- */
- public StatusConfiguration withVerboseClasses(final String... verboseClasses) {
- this.verboseClasses = verboseClasses;
- return this;
- }
-
- /**
- * Configures and initializes the StatusLogger using the configured options in this instance.
- */
- public void initialize() {
- if (!this.initialized) {
- if (this.status == Level.OFF) {
- this.initialized = true;
- } else {
- final boolean configured = configureExistingStatusConsoleListener();
- if (!configured) {
- registerNewStatusConsoleListener();
- }
- migrateSavedLogMessages();
- }
- }
- }
-
- private boolean configureExistingStatusConsoleListener() {
- boolean configured = false;
- for (final StatusListener statusListener : this.logger.getListeners()) {
- if (this.destination.isInstance(statusListener)) {
- final StatusConsoleListener listener = (StatusConsoleListener) statusListener;
- listener.setLevel(this.status);
- if (this.verbosity == Verbosity.QUIET) {
- listener.setFilters(this.verboseClasses);
- }
- configured = true;
- }
- }
- return configured;
- }
-
-
- private void registerNewStatusConsoleListener() {
- StatusConsoleListener listener;
- try {
- listener = this.destination.newInstance();
- listener.setLevel(this.status);
- if (this.verbosity == Verbosity.QUIET) {
- listener.setFilters(this.verboseClasses);
- }
- this.logger.registerListener(listener);
- } catch (ReflectiveOperationException e) {
- logger.error("Cannot create listener of type " + destination.getClass());
- }
- }
-
- private void migrateSavedLogMessages() {
- for (final String message : this.errorMessages) {
- this.logger.error(message);
- }
- this.initialized = true;
- this.errorMessages.clear();
- }
-}
\ No newline at end of file
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusConsoleListener.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusConsoleListener.java
deleted file mode 100644
index 117ede3..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusConsoleListener.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.config.status;
-
-import java.io.IOException;
-
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.status.StatusData;
-import org.apache.logging.log4j.status.StatusListener;
-
-/**
- * StatusListener that writes to the Console.
- */
-public abstract class StatusConsoleListener implements StatusListener {
-
- protected Level level;
- private String[] filters = null;
-
- /**
- * Creates the StatusConsoleListener using the supplied Level.
- */
- public StatusConsoleListener() {
- this(Level.FATAL);
- }
-
- /**
- * Creates the StatusConsoleListener using the supplied Level.
- * @param level The Level of status messages that should appear on the console.
- */
- public StatusConsoleListener(final Level level) {
- this.level = level;
- }
-
- /**
- * Sets the level to a new value.
- * @param level The new Level.
- */
- public void setLevel(final Level level) {
- this.level = level;
- }
-
- public Object getLevel() {
- return level;
- }
-
- /**
- * Adds package name filters to exclude.
- * @param filters An array of package names to exclude.
- */
- public void setFilters(final String... filters) {
- this.filters = filters;
- }
-
- protected boolean isEnabledFor(final StatusData data) {
- if (filters == null) {
- return true;
- }
- final String caller = data.getStackTraceElement().getClassName();
- for (final String filter : filters) {
- if (caller.startsWith(filter)) {
- return true;
- }
- }
- return false;
- }
-
- @Override
- public void close() throws IOException {
- // don't close system streams
- }
-}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusStdErrListener.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusStdErrListener.java
deleted file mode 100644
index 59bd007..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusStdErrListener.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.apache.logging.log4j.core.config.status;
-
-import org.apache.logging.log4j.status.StatusData;
-
-public class StatusStdErrListener extends StatusConsoleListener {
-
- @Override
- public void log(StatusData data) {
- if (isEnabledFor(data)) {
- System.err.println(data.getFormattedStatus());
- }
- }
-
-}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusStdOutListener.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusStdOutListener.java
deleted file mode 100644
index 83631e1..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/status/StatusStdOutListener.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.apache.logging.log4j.core.config.status;
-
-import org.apache.logging.log4j.status.StatusData;
-
-public class StatusStdOutListener extends StatusConsoleListener {
-
- @Override
- public void log(StatusData data) {
- if (isEnabledFor(data)) {
- System.out.println(data.getFormattedStatus());
- }
- }
-
-}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
index eadcbbc..309bf66 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
@@ -43,7 +43,6 @@
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
-import org.apache.logging.log4j.core.config.status.StatusConfiguration;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.core.util.Patterns;
import org.w3c.dom.Attr;
@@ -134,19 +133,11 @@
final Document document = newDocumentBuilder().parse(source);
rootElement = document.getDocumentElement();
final Map<String, String> attrs = processAttributes(rootNode, rootElement);
- final StatusConfiguration statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES)
- .withStatus(getDefaultStatus());
for (final Map.Entry<String, String> entry : attrs.entrySet()) {
final String key = entry.getKey();
final String value = getStrSubstitutor().replace(entry.getValue());
- if ("status".equalsIgnoreCase(key)) {
- statusConfig.withStatus(value);
- } else if ("dest".equalsIgnoreCase(key)) {
- statusConfig.withDestination(value);
- } else if ("shutdownHook".equalsIgnoreCase(key)) {
+ if ("shutdownHook".equalsIgnoreCase(key)) {
isShutdownHookEnabled = !"disable".equalsIgnoreCase(value);
- } else if ("verbose".equalsIgnoreCase(key)) {
- statusConfig.withVerbosity(value);
} else if ("packages".equalsIgnoreCase(key)) {
final String[] packages = value.split(Patterns.COMMA_SEPARATOR);
for (final String p : packages) {
@@ -167,7 +158,6 @@
createAdvertiser(value, configSource, buffer, "text/xml");
}
}
- statusConfig.initialize();
} catch (final SAXException domEx) {
LOGGER.error("Error parsing " + configSource.getLocation(), domEx);
} catch (final IOException ioe) {