This is an automated email from the ASF dual-hosted git repository.

mridulpathak pushed a commit to branch release24.09
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release24.09 by this push:
     new 401aed5a2b java.util.logging output bypasses Log4j 2
401aed5a2b is described below

commit 401aed5a2be1113508909bbdd0c7c25affad9447
Author: Mridul Pathak <[email protected]>
AuthorDate: Sat Oct 24 14:54:28 2026 +0530

    java.util.logging output bypasses Log4j 2
    
    Backported from trunk (#2414)
---
 .../java/org/apache/ofbiz/base/start/Start.java    | 35 ++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git 
a/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java 
b/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
index 62a6765146..27479ee696 100644
--- a/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
+++ b/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
@@ -22,6 +22,9 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
 import java.util.stream.Collectors;
 
 import org.apache.ofbiz.base.container.ContainerLoader;
@@ -71,6 +74,10 @@ public final class Start {
         }
 
         CommandType commandType = CommandType.valueOf(ofbizCommands);
+        if (commandType.equals(CommandType.START)) {
+            // not for the status and shutdown clients: they would open, and 
roll over, the log files of the running server
+            routeJavaUtilLoggingToLog4j();
+        }
         if (!commandType.equals(CommandType.HELP)) {
             INSTANCE.config = StartupControlPanel.init(ofbizCommands);
         }
@@ -94,6 +101,34 @@ public final class Start {
         }
     }
 
+    /**
+     * Routes java.util.logging (used by Tomcat, Jersey, Geronimo and other 
libraries) to Log4j 2,
+     * so that it reaches the OFBiz log files and follows the levels of 
log4j2.xml.
+     * The bridge handler also works when java.util.logging was started before 
main() (e.g. by a JMX agent),
+     * and it only forwards the records java.util.logging itself lets through 
(INFO and above by default).
+     * Only the default console handler of the JDK is removed, and only when 
java.util.logging has not been
+     * configured on purpose, so that handlers set up by an operator or an 
agent are kept.
+     */
+    private static void routeJavaUtilLoggingToLog4j() {
+        try {
+            Class.forName("org.apache.logging.log4j.jul.Log4jBridgeHandler")
+                    .getMethod("install", boolean.class, String.class, 
boolean.class)
+                    .invoke(null, false, null, false);
+        } catch (ReflectiveOperationException | LinkageError e) {
+            System.err.println("java.util.logging is not routed to Log4j 2 
(log4j-jul missing?): " + e);
+            return;
+        }
+        if (System.getProperty("java.util.logging.config.file") == null
+                && System.getProperty("java.util.logging.config.class") == 
null) {
+            Logger rootLogger = Logger.getLogger("");
+            for (Handler handler : rootLogger.getHandlers()) {
+                if (handler.getClass() == ConsoleHandler.class) {
+                    rootLogger.removeHandler(handler);
+                }
+            }
+        }
+    }
+
     /**
      * Returns the <code>Start</code> instance.
      */

Reply via email to