This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch feature/2.x/android in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 62b84e68060bb209fd3970ae3ea88ee657f153f4 Author: Piotr P. Karwasz <[email protected]> AuthorDate: Wed Oct 9 09:33:12 2024 +0200 Fix Android-related issues in Log4j Core This fixes three issues encountered in the [`log4j-samples-android`](https://github.com/apache/logging-log4j-samples/tree/main/log4j-samples-android) test project: 1. Disables the `jvmrunargs` lookup on Android and fixes it on the other platforms. Previously, the lookup always returned `null`. 2. Switches the default context selector to `BasicContextSelector` on Android. `StackLocator` is broken on Android: it cannot use our JDK 8 code (missing `sun.reflect` classes), but also it cannot use our JDK 11+ code (missing multi-release JAR support). This causes `ClassLoaderContextSelector` to use two different logger contexts for the same classloader. 3. Fixes a `ParserConfigurationException` caused by the lack of XInclude capabilities in Android's XML parser. The fix to [LOG4J2-3531](https://issues.apache.org/jira/browse/LOG4J2-3531) didn't cover all the cases. Closes #3056. Part of #2832. --- .../log4j/core/config/xml/XmlConfiguration.java | 12 ++++--- .../log4j/core/impl/Log4jContextFactory.java | 9 ++++- .../lookup/JmxRuntimeInputArgumentsLookup.java | 27 +++++++++++---- .../log4j/core/util/internal/SystemUtils.java | 40 ++++++++++++++++++++++ src/changelog/.2.x.x/3056_android_support.xml | 8 +++++ 5 files changed, 83 insertions(+), 13 deletions(-) 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 31e42d843f..6caeec59ae 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 @@ -216,14 +216,16 @@ public class XmlConfiguration extends AbstractConfiguration implements Reconfigu */ private static void enableXInclude(final DocumentBuilderFactory factory) { try { - // Alternative: We set if a system property on the command line is set, for example: - // -DLog4j.XInclude=true factory.setXIncludeAware(true); // LOG4J2-3531: Xerces only checks if the feature is supported when creating a factory. To reproduce: // -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XML11NonValidatingConfiguration - factory.newDocumentBuilder(); - } catch (final UnsupportedOperationException | ParserConfigurationException e) { - factory.setXIncludeAware(false); + try { + factory.newDocumentBuilder(); + } catch (final ParserConfigurationException e) { + factory.setXIncludeAware(false); + LOGGER.warn("The DocumentBuilderFactory [{}] does not support XInclude: {}", factory, e); + } + } catch (final UnsupportedOperationException e) { LOGGER.warn("The DocumentBuilderFactory [{}] does not support XInclude: {}", factory, e); } catch (final AbstractMethodError | NoSuchMethodError err) { LOGGER.warn( diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java index e8fd025175..bc89276774 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java @@ -29,6 +29,7 @@ import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.apache.logging.log4j.core.config.composite.CompositeConfiguration; +import org.apache.logging.log4j.core.selector.BasicContextSelector; import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector; import org.apache.logging.log4j.core.selector.ContextSelector; import org.apache.logging.log4j.core.util.Cancellable; @@ -36,6 +37,7 @@ import org.apache.logging.log4j.core.util.Constants; import org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry; import org.apache.logging.log4j.core.util.Loader; import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry; +import org.apache.logging.log4j.core.util.internal.SystemUtils; import org.apache.logging.log4j.spi.LoggerContextFactory; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.PropertiesUtil; @@ -104,7 +106,12 @@ public class Log4jContextFactory implements LoggerContextFactory, ShutdownCallba } catch (final Exception e) { LOGGER.error("Unable to create custom ContextSelector. Falling back to default.", e); } - return new ClassLoaderContextSelector(); + // StackLocator is broken on Android: + // 1. Android could use the StackLocator implementation for JDK 11, but does not support multi-release JARs. + // 2. Android does not have the `sun.reflect` classes used in the JDK 8 implementation. + // + // Therefore, we use a single logger context. + return SystemUtils.isOsAndroid() ? new BasicContextSelector() : new ClassLoaderContextSelector(); } private static ShutdownCallbackRegistry createShutdownCallbackRegistry() { diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JmxRuntimeInputArgumentsLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JmxRuntimeInputArgumentsLookup.java index 45f216f1b4..6fcf543518 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JmxRuntimeInputArgumentsLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JmxRuntimeInputArgumentsLookup.java @@ -17,10 +17,13 @@ package org.apache.logging.log4j.core.lookup; import java.lang.management.ManagementFactory; -import java.util.List; +import java.util.Collections; import java.util.Map; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.util.internal.SystemUtils; +import org.apache.logging.log4j.status.StatusLogger; /** * Maps JVM input arguments (but not main arguments) using JMX to acquire JVM arguments. @@ -31,17 +34,16 @@ import org.apache.logging.log4j.core.config.plugins.Plugin; @Plugin(name = "jvmrunargs", category = StrLookup.CATEGORY) public class JmxRuntimeInputArgumentsLookup extends MapLookup { - static { - final List<String> argsList = ManagementFactory.getRuntimeMXBean().getInputArguments(); - JMX_SINGLETON = new JmxRuntimeInputArgumentsLookup(MapLookup.toMap(argsList)); - } + private static final Logger LOGGER = StatusLogger.getLogger(); - public static final JmxRuntimeInputArgumentsLookup JMX_SINGLETON; + public static final JmxRuntimeInputArgumentsLookup JMX_SINGLETON = new JmxRuntimeInputArgumentsLookup(); /** * Constructor when used directly as a plugin. */ - public JmxRuntimeInputArgumentsLookup() {} + public JmxRuntimeInputArgumentsLookup() { + this(getMapFromJmx()); + } public JmxRuntimeInputArgumentsLookup(final Map<String, String> map) { super(map); @@ -55,4 +57,15 @@ public class JmxRuntimeInputArgumentsLookup extends MapLookup { final Map<String, String> map = getMap(); return map == null ? null : map.get(key); } + + private static Map<String, String> getMapFromJmx() { + if (!SystemUtils.isOsAndroid()) { + try { + return MapLookup.toMap(ManagementFactory.getRuntimeMXBean().getInputArguments()); + } catch (LinkageError e) { + LOGGER.warn("Failed to get JMX arguments from JVM.", e); + } + } + return Collections.emptyMap(); + } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/SystemUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/SystemUtils.java new file mode 100644 index 0000000000..b7b3d9cbb4 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/SystemUtils.java @@ -0,0 +1,40 @@ +/* + * 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.util.internal; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.status.StatusLogger; + +public final class SystemUtils { + + private static final Logger LOGGER = StatusLogger.getLogger(); + + private static String getJavaVendor() { + try { + return System.getProperty("java.vendor"); + } catch (final SecurityException e) { + LOGGER.warn("Unable to determine Java vendor.", e); + } + return "Unknown"; + } + + public static boolean isOsAndroid() { + return getJavaVendor().contains("Android"); + } + + private SystemUtils() {} +} diff --git a/src/changelog/.2.x.x/3056_android_support.xml b/src/changelog/.2.x.x/3056_android_support.xml new file mode 100644 index 0000000000..764cb7f826 --- /dev/null +++ b/src/changelog/.2.x.x/3056_android_support.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="https://logging.apache.org/xml/ns" + xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" + type="fixed"> + <issue id="3056" link="https://github.com/apache/logging-log4j2/issues/3056"/> + <description format="asciidoc">Fix Android-related issues in Log4j Core.</description> +</entry>
