You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2026/10/18 20:38:01 UTC
(logging-log4j2) branch Jackson3 created (now 1a67d98bec)
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a change to branch Jackson3
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
at 1a67d98bec Support Jackson 3
This branch includes the following new commits:
new 1a67d98bec Support Jackson 3
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(logging-log4j2) 01/01: Support Jackson 3
Posted by rg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch Jackson3
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 1a67d98beca2a96582fb0e8e688d02b42dd6fab0
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Oct 17 14:16:26 2026 -0700
Support Jackson 3
---
log4j-core/pom.xml | 20 ++
.../config/yaml/Jackson3YamlConfiguration.java | 263 +++++++++++++++++++++
.../core/config/yaml/YamlConfigurationFactory.java | 50 +++-
log4j-parent/pom.xml | 20 ++
4 files changed, 343 insertions(+), 10 deletions(-)
diff --git a/log4j-core/pom.xml b/log4j-core/pom.xml
index e0c8205f67..bd1764162c 100644
--- a/log4j-core/pom.xml
+++ b/log4j-core/pom.xml
@@ -69,6 +69,7 @@
org.codehaus.stax2.*;resolution:=optional,
org.jctools.*;resolution:=optional,
org.zeromq;resolution:=optional,
+ tools.jackson.*;resolution:=optional,
javax.lang.model.*;resolution:=optional,
javax.tools;resolution:=optional,
<!-- `java.sql`, which depends on `java.logging` is optional -->
@@ -99,6 +100,9 @@
org.apache.commons.csv;transitive=false,
org.jspecify;transitive=false,
org.zeromq.jeromq;transitive=false,
+ tools.jackson.core;transitive=false,
+ tools.jackson.databind;transitive=false,
+ tools.jackson.dataformat.yaml;transitive=false,
<!-- A module descriptor is only available in version 1.2.16+, hence it is not detected -->
com.conversantmedia.disruptor;substitute="disruptor";transitive=false;static=true,
<!-- Filebased module names: MUST be static -->
@@ -193,6 +197,22 @@
<artifactId>jackson-dataformat-yaml</artifactId>
<optional>true</optional>
</dependency>
+ <!-- Alternative YAML support using Jackson 3.x (requires Java 17+) -->
+ <dependency>
+ <groupId>tools.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>tools.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>tools.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-yaml</artifactId>
+ <optional>true</optional>
+ </dependency>
<!-- Alternative implementation of BlockingQueue using JCTools for AsyncAppender -->
<dependency>
<groupId>org.jctools</groupId>
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/Jackson3YamlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/Jackson3YamlConfiguration.java
new file mode 100644
index 0000000000..4d92a2efad
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/Jackson3YamlConfiguration.java
@@ -0,0 +1,263 @@
+/*
+ * 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.yaml;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.AbstractConfiguration;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.Reconfigurable;
+import org.apache.logging.log4j.core.config.plugins.util.PluginType;
+import org.apache.logging.log4j.core.config.status.StatusConfiguration;
+import org.apache.logging.log4j.core.util.Integers;
+import org.apache.logging.log4j.core.util.Patterns;
+import tools.jackson.databind.JsonNode;
+import tools.jackson.databind.ObjectMapper;
+import tools.jackson.dataformat.yaml.YAMLMapper;
+
+/**
+ * Creates a Node hierarchy from a YAML file using Jackson 3.x.
+ * <p>
+ * This is the Jackson 3.x equivalent of {@link YamlConfiguration}, which requires Jackson 2.x.
+ * </p>
+ * @since 2.27.0
+ */
+public class Jackson3YamlConfiguration extends AbstractConfiguration implements Reconfigurable {
+
+ private final List<Status> status = new ArrayList<>();
+ private JsonNode root;
+
+ public Jackson3YamlConfiguration(final LoggerContext loggerContext, final ConfigurationSource configSource) {
+ super(loggerContext, configSource);
+ byte[] buffer;
+ try {
+ try (final InputStream configStream = configSource.getInputStream()) {
+ buffer = toByteArray(configStream);
+ }
+ final InputStream is = new ByteArrayInputStream(buffer);
+ root = getObjectMapper().readTree(is);
+ if (root.size() == 1) {
+ for (final JsonNode node : root) {
+ root = node;
+ }
+ }
+ processAttributes(rootNode, root);
+ final StatusConfiguration statusConfig = new StatusConfiguration().withStatus(getDefaultStatus());
+ int monitorIntervalSeconds = 0;
+ for (final Map.Entry<String, String> entry :
+ rootNode.getAttributes().entrySet()) {
+ final String key = entry.getKey();
+ final String value = getConfigurationStrSubstitutor().replace(entry.getValue());
+ if ("status".equalsIgnoreCase(key)) {
+ statusConfig.withStatus(value);
+ } else if ("dest".equalsIgnoreCase(key)) {
+ statusConfig.withDestination(value);
+ } else if ("shutdownHook".equalsIgnoreCase(key)) {
+ isShutdownHookEnabled = !"disable".equalsIgnoreCase(value);
+ } else if ("shutdownTimeout".equalsIgnoreCase(key)) {
+ shutdownTimeoutMillis = Long.parseLong(value);
+ } else if ("packages".equalsIgnoreCase(key)) {
+ pluginPackages.addAll(Arrays.asList(value.split(Patterns.COMMA_SEPARATOR)));
+ } else if ("name".equalsIgnoreCase(key)) {
+ setName(value);
+ } else if ("monitorInterval".equalsIgnoreCase(key)) {
+ monitorIntervalSeconds = Integers.parseInt(value);
+ } else if ("advertiser".equalsIgnoreCase(key)) {
+ createAdvertiser(value, configSource, buffer, "application/yaml");
+ }
+ }
+ initializeWatchers(this, configSource, monitorIntervalSeconds);
+ statusConfig.initialize();
+ if (getName() == null) {
+ setName(configSource.getLocation());
+ }
+ } catch (final Exception ex) {
+ LOGGER.error("Error parsing " + configSource.getLocation(), ex);
+ }
+ }
+
+ /**
+ * Returns the mapper used to parse the configuration. YAML supports comments natively.
+ */
+ protected ObjectMapper getObjectMapper() {
+ return YAMLMapper.builder().build();
+ }
+
+ @Override
+ public void setup() {
+ final List<Node> children = rootNode.getChildren();
+ for (final Map.Entry<String, JsonNode> entry : root.properties()) {
+ final JsonNode n = entry.getValue();
+ if (n.isObject()) {
+ LOGGER.debug("Processing node for object {}", entry.getKey());
+ children.add(constructNode(entry.getKey(), rootNode, n));
+ } else if (n.isArray()) {
+ LOGGER.error("Arrays are not supported at the root configuration.");
+ }
+ }
+ LOGGER.debug("Completed parsing configuration");
+ if (status.size() > 0) {
+ for (final Status s : status) {
+ LOGGER.error("Error processing element {}: {}", s.name, s.errorType);
+ }
+ }
+ }
+
+ @Override
+ public Configuration reconfigure() {
+ try {
+ final ConfigurationSource source = getConfigurationSource().resetInputStream();
+ if (source == null) {
+ return null;
+ }
+ return new Jackson3YamlConfiguration(getLoggerContext(), source);
+ } catch (final IOException ex) {
+ LOGGER.error("Cannot locate file {}", getConfigurationSource(), ex);
+ }
+ return null;
+ }
+
+ private Node constructNode(final String name, final Node parent, final JsonNode jsonNode) {
+ final PluginType<?> type = pluginManager.getPluginType(name);
+ final Node node = new Node(parent, name, type);
+ processAttributes(node, jsonNode);
+ final List<Node> children = node.getChildren();
+ for (final Map.Entry<String, JsonNode> entry : jsonNode.properties()) {
+ final JsonNode n = entry.getValue();
+ if (n.isArray() || n.isObject()) {
+ if (type == null) {
+ status.add(new Status(name, n, ErrorType.CLASS_NOT_FOUND));
+ }
+ if (n.isArray()) {
+ LOGGER.debug("Processing node for array {}", entry.getKey());
+ for (int i = 0; i < n.size(); ++i) {
+ final String pluginType = getType(n.get(i), entry.getKey());
+ final PluginType<?> entryType = pluginManager.getPluginType(pluginType);
+ final Node item = new Node(node, entry.getKey(), entryType);
+ processAttributes(item, n.get(i));
+ if (pluginType.equals(entry.getKey())) {
+ LOGGER.debug("Processing {}[{}]", entry.getKey(), i);
+ } else {
+ LOGGER.debug("Processing {} {}[{}]", pluginType, entry.getKey(), i);
+ }
+ final List<Node> itemChildren = item.getChildren();
+ for (final Map.Entry<String, JsonNode> itemEntry :
+ n.get(i).properties()) {
+ if (itemEntry.getValue().isObject()) {
+ LOGGER.debug("Processing node for object {}", itemEntry.getKey());
+ itemChildren.add(constructNode(itemEntry.getKey(), item, itemEntry.getValue()));
+ } else if (itemEntry.getValue().isArray()) {
+ final JsonNode array = itemEntry.getValue();
+ final String entryName = itemEntry.getKey();
+ LOGGER.debug("Processing array for object {}", entryName);
+ for (int j = 0; j < array.size(); ++j) {
+ itemChildren.add(constructNode(entryName, item, array.get(j)));
+ }
+ }
+ }
+ children.add(item);
+ }
+ } else {
+ LOGGER.debug("Processing node for object {}", entry.getKey());
+ children.add(constructNode(entry.getKey(), node, n));
+ }
+ } else {
+ LOGGER.debug("Node {} is of type {}", entry.getKey(), n.getNodeType());
+ }
+ }
+
+ String t;
+ if (type == null) {
+ t = "null";
+ } else {
+ t = type.getElementName() + ':' + type.getPluginClass();
+ }
+
+ final String p = node.getParent() == null
+ ? "null"
+ : node.getParent().getName() == null
+ ? LoggerConfig.ROOT
+ : node.getParent().getName();
+ LOGGER.debug("Returning {} with parent {} of type {}", node.getName(), p, t);
+ return node;
+ }
+
+ private String getType(final JsonNode node, final String name) {
+ for (final Map.Entry<String, JsonNode> entry : node.properties()) {
+ if (entry.getKey().equalsIgnoreCase("type")) {
+ final JsonNode n = entry.getValue();
+ if (n.isValueNode()) {
+ return n.asString();
+ }
+ }
+ }
+ return name;
+ }
+
+ private void processAttributes(final Node parent, final JsonNode node) {
+ final Map<String, String> attrs = parent.getAttributes();
+ for (final Map.Entry<String, JsonNode> entry : node.properties()) {
+ if (!entry.getKey().equalsIgnoreCase("type")) {
+ final JsonNode n = entry.getValue();
+ if (n.isValueNode()) {
+ attrs.put(entry.getKey(), n.asString());
+ }
+ }
+ }
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "[location=" + getConfigurationSource() + "]";
+ }
+
+ /**
+ * The error that occurred.
+ */
+ private enum ErrorType {
+ CLASS_NOT_FOUND
+ }
+
+ /**
+ * Status for recording errors.
+ */
+ private static class Status {
+ private final JsonNode node;
+ private final String name;
+ private final ErrorType errorType;
+
+ public Status(final String name, final JsonNode node, final ErrorType errorType) {
+ this.name = name;
+ this.node = node;
+ this.errorType = errorType;
+ }
+
+ @Override
+ public String toString() {
+ return "Status [name=" + name + ", errorType=" + errorType + ", node=" + node + "]";
+ }
+ }
+}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/YamlConfigurationFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/YamlConfigurationFactory.java
index f3f0630a9c..3d667d19f9 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/YamlConfigurationFactory.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/YamlConfigurationFactory.java
@@ -33,7 +33,19 @@ public class YamlConfigurationFactory extends ConfigurationFactory {
*/
private static final String[] SUFFIXES = new String[] {".yml", ".yaml"};
- private static final String[] dependencies = new String[] {
+ /**
+ * Classes required for Jackson 3.x support.
+ */
+ private static final String[] JACKSON3_DEPENDENCIES = new String[] {
+ "tools.jackson.databind.ObjectMapper",
+ "tools.jackson.databind.JsonNode",
+ "tools.jackson.dataformat.yaml.YAMLMapper"
+ };
+
+ /**
+ * Classes required for Jackson 2.x support.
+ */
+ private static final String[] JACKSON2_DEPENDENCIES = new String[] {
"com.fasterxml.jackson.databind.ObjectMapper",
"com.fasterxml.jackson.databind.JsonNode",
"com.fasterxml.jackson.core.JsonParser",
@@ -42,17 +54,33 @@ public class YamlConfigurationFactory extends ConfigurationFactory {
private final boolean isActive;
+ private final boolean useJackson3;
+
public YamlConfigurationFactory() {
- for (final String dependency : dependencies) {
- if (!Loader.isClassAvailable(dependency)) {
- LOGGER.debug(
- "Missing dependencies for Yaml support, ConfigurationFactory {} is inactive",
- getClass().getName());
- isActive = false;
- return;
+ if (areClassesAvailable(JACKSON3_DEPENDENCIES)) {
+ LOGGER.debug("Using Jackson 3.x for Yaml support");
+ useJackson3 = true;
+ isActive = true;
+ } else if (areClassesAvailable(JACKSON2_DEPENDENCIES)) {
+ LOGGER.debug("Using Jackson 2.x for Yaml support");
+ useJackson3 = false;
+ isActive = true;
+ } else {
+ LOGGER.debug(
+ "Missing dependencies for Yaml support, ConfigurationFactory {} is inactive",
+ getClass().getName());
+ useJackson3 = false;
+ isActive = false;
+ }
+ }
+
+ private static boolean areClassesAvailable(final String[] classNames) {
+ for (final String className : classNames) {
+ if (!Loader.isClassAvailable(className)) {
+ return false;
}
}
- isActive = true;
+ return true;
}
@Override
@@ -65,7 +93,9 @@ public class YamlConfigurationFactory extends ConfigurationFactory {
if (!isActive()) {
return null;
}
- return new YamlConfiguration(loggerContext, source);
+ return useJackson3
+ ? new Jackson3YamlConfiguration(loggerContext, source)
+ : new YamlConfiguration(loggerContext, source);
}
@Override
diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml
index 1acb47ea50..33fa6335b2 100644
--- a/log4j-parent/pom.xml
+++ b/log4j-parent/pom.xml
@@ -89,6 +89,7 @@
<HdrHistogram.version>2.2.2</HdrHistogram.version>
<hsqldb.version>2.7.4</hsqldb.version>
<jackson-bom.version>2.22.2</jackson-bom.version>
+ <jackson3.version>3.1.4</jackson3.version>
<!-- Override the version in Jakarta EE 9 BOM: -->
<jakarta-activation.version>2.1.4</jakarta-activation.version>
<jakarta-mail.version>2.1.5</jakarta-mail.version>
@@ -194,6 +195,25 @@
<scope>import</scope>
</dependency>
+ <!-- Jackson 3.x artifacts are listed individually, since the Jackson 3.x BOM also manages Jackson 2.x annotations -->
+ <dependency>
+ <groupId>tools.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <version>${jackson3.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>tools.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>${jackson3.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>tools.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-yaml</artifactId>
+ <version>${jackson3.version}</version>
+ </dependency>
+
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-bom</artifactId>