This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 7f7d541a3a Make LowLevelLogUtil use StatusLogger once available
7f7d541a3a is described below
commit 7f7d541a3af492c4c4ccb2e2b46b372920f07902
Author: Matt Sicker <[email protected]>
AuthorDate: Sun Nov 6 15:09:59 2022 -0600
Make LowLevelLogUtil use StatusLogger once available
This makes the logging strategy used here configurable after the fact so
that StatusLogger can update LowLevelLogUtil to use itself after initialization.
Signed-off-by: Matt Sicker <[email protected]>
---
.../apache/logging/log4j/status/StatusLogger.java | 8 +-
.../apache/logging/log4j/util/LowLevelLogUtil.java | 92 +++++++++++++++-------
2 files changed, 72 insertions(+), 28 deletions(-)
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 ee94b85285..1a57b9a391 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
@@ -24,9 +24,10 @@ 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.PropertyEnvironment;
import org.apache.logging.log4j.util.Constants;
+import org.apache.logging.log4j.util.LowLevelLogUtil;
import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.PropertyEnvironment;
import java.io.Closeable;
import java.io.IOException;
@@ -91,6 +92,11 @@ public final class StatusLogger extends AbstractLogger {
ParameterizedNoReferenceMessageFactory.INSTANCE,
SimpleLoggerFactory.getInstance());
+ static {
+ // now safe to use StatusLogger in LowLevelLogUtil
+ LowLevelLogUtil.setLogger(STATUS_LOGGER);
+ }
+
private final SimpleLogger logger;
private final Collection<StatusListener> listeners = new
CopyOnWriteArrayList<>();
diff --git
a/log4j-api/src/main/java/org/apache/logging/log4j/util/LowLevelLogUtil.java
b/log4j-api/src/main/java/org/apache/logging/log4j/util/LowLevelLogUtil.java
index c74544a818..e074d4d3fd 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/LowLevelLogUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LowLevelLogUtil.java
@@ -17,10 +17,9 @@
package org.apache.logging.log4j.util;
-import java.io.OutputStream;
+import org.apache.logging.log4j.Logger;
+
import java.io.PrintWriter;
-import java.io.Writer;
-import java.util.Objects;
/**
* PrintWriter-based logging utility for classes too low level to use {@link
org.apache.logging.log4j.status.StatusLogger}.
@@ -32,47 +31,86 @@ import java.util.Objects;
@InternalApi
public final class LowLevelLogUtil {
- private static PrintWriter writer = new PrintWriter(System.err, true);
+ interface ErrorLogger {
+ void error(final String message);
+
+ void error(final Throwable throwable);
+
+ void error(final String message, final Throwable throwable);
+ }
+
+ private static class StandardErrorLogger implements ErrorLogger {
+ private final PrintWriter stderr = new PrintWriter(System.err, true);
+
+ @Override
+ public void error(final String message) {
+ stderr.println("ERROR: " + message);
+ }
+
+ @Override
+ public void error(final Throwable throwable) {
+ throwable.printStackTrace(stderr);
+ }
+
+ @Override
+ public void error(final String message, final Throwable throwable) {
+ error(message);
+ error(throwable);
+ }
+ }
+
+ private static class DelegateErrorLogger implements ErrorLogger {
+ private final Logger logger;
+
+ private DelegateErrorLogger(final Logger logger) {
+ this.logger = logger;
+ }
+
+ @Override
+ public void error(final String message) {
+ logger.error(message);
+ }
+
+ @Override
+ public void error(final Throwable throwable) {
+ logger.error(throwable);
+ }
+
+ @Override
+ public void error(final String message, final Throwable throwable) {
+ logger.error(message, throwable);
+ }
+ }
+
+ private static ErrorLogger errorLogger = new StandardErrorLogger();
+
+ /**
+ * Sets the low level logging strategy to use a delegate Logger.
+ */
+ public static void setLogger(final Logger logger) {
+ errorLogger = new DelegateErrorLogger(logger);
+ }
/**
* Logs the given message.
- *
+ *
* @param message the message to log
* @since 2.9.2
*/
public static void log(final String message) {
if (message != null) {
- writer.println(message);
+ errorLogger.error(message);
}
}
public static void logException(final Throwable exception) {
if (exception != null) {
- exception.printStackTrace(writer);
+ errorLogger.error(exception);
}
}
public static void logException(final String message, final Throwable
exception) {
- log(message);
- logException(exception);
- }
-
- /**
- * Sets the underlying OutputStream where exceptions are printed to.
- *
- * @param out the OutputStream to log to
- */
- public static void setOutputStream(final OutputStream out) {
- LowLevelLogUtil.writer = new PrintWriter(Objects.requireNonNull(out),
true);
- }
-
- /**
- * Sets the underlying Writer where exceptions are printed to.
- *
- * @param writer the Writer to log to
- */
- public static void setWriter(final Writer writer) {
- LowLevelLogUtil.writer = new
PrintWriter(Objects.requireNonNull(writer), true);
+ errorLogger.error(message, exception);
}
private LowLevelLogUtil() {