From f2fb938d543d1024bfe38dfb171a66adbdec4240 Mon Sep 17 00:00:00 2001 From: Grzegorz Grzybek Date: Mon, 4 May 2020 12:18:33 +0200 Subject: [PATCH] [PAXLOGGING-312] Upgrade to log4j2 2.13.2 with necessary changes related to LOG4J2-2779 (cherry picked from commit 5f9d55d7657dcc4dcf45b600a2d16dcae29b1395) --- .../appender/db/jdbc/JdbcDatabaseManager.java | 34 +++++++++++++------ .../config/status/StatusConfiguration.java | 5 ++- .../logging/log4j2/internal/Activator.java | 5 +++ pom.xml | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java b/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java index 9c41710c0..db302595a 100644 --- a/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java +++ b/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java @@ -751,7 +751,20 @@ private void setFields(final MapMessage mapMessage) throws SQLException { * Sets the given Object in the prepared statement. The value is truncated if needed. */ private void setStatementObject(final int j, final String nameKey, final Object value) throws SQLException { - statement.setObject(j, truncate(nameKey, value)); + if (statement == null) { + throw new AppenderLoggingException("Cannot set a value when the PreparedStatement is null."); + } + if (value == null) { + if (columnMetaData == null) { + throw new AppenderLoggingException("Cannot set a value when the column metadata is null."); + } + // [LOG4J2-2762] [JDBC] MS-SQL Server JDBC driver throws SQLServerException when + // inserting a null value for a VARBINARY column. + // Calling setNull() instead of setObject() for null values fixes [LOG4J2-2762]. + this.statement.setNull(j, columnMetaData.get(nameKey).getType()); + } else { + statement.setObject(j, truncate(nameKey, value)); + } } @Override @@ -822,13 +835,7 @@ protected void writeInternal(final LogEvent event, final Serializable serializab } else { final Object value = TypeConverters.convert(layout.toSerializable(event), mapping.getType(), null); - if (value == null) { - // TODO We might need to always initialize the columnMetaData to specify the - // type. - this.statement.setNull(j++, Types.NULL); - } else { - setStatementObject(j++, mapping.getNameKey(), value); - } + setStatementObject(j++, mapping.getNameKey(), value); } } } @@ -854,10 +861,15 @@ protected void writeInternal(final LogEvent event, final Serializable serializab } if (isBuffered() && this.isBatchSupported) { + logger().debug("addBatch for {}", this.statement); this.statement.addBatch(); - } else if (this.statement.executeUpdate() == 0) { - throw new AppenderLoggingException( - "No records inserted in database table for log event in JDBC manager [%s].", fieldsToString()); + } else { + final int executeUpdate = this.statement.executeUpdate(); + logger().debug("executeUpdate = {} for {}", executeUpdate, this.statement); + if (executeUpdate == 0) { + throw new AppenderLoggingException( + "No records inserted in database table for log event in JDBC manager [%s].", fieldsToString()); + } } } catch (final SQLException e) { throw new DbAppenderLoggingException(e, "Failed to insert record for log event in JDBC manager: %s [%s]", e, diff --git a/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java b/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java index 967e51220..78be70d9e 100644 --- a/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java +++ b/pax-logging-log4j2/src/main/java/org/apache/logging/log4j/core/config/status/StatusConfiguration.java @@ -24,8 +24,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; +import java.util.concurrent.LinkedBlockingQueue; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.util.FileUtils; @@ -44,7 +43,7 @@ public class StatusConfiguration { private static final Level DEFAULT_STATUS = Level.ERROR; private static final Verbosity DEFAULT_VERBOSITY = Verbosity.QUIET; - private final Collection errorMessages = Collections.synchronizedCollection(new LinkedList()); + private final Collection errorMessages = new LinkedBlockingQueue(); private final StatusLogger logger = StatusLogger.getLogger(); private volatile boolean initialized = false; diff --git a/pax-logging-log4j2/src/main/java/org/ops4j/pax/logging/log4j2/internal/Activator.java b/pax-logging-log4j2/src/main/java/org/ops4j/pax/logging/log4j2/internal/Activator.java index 7f4f14226..e2c8544e8 100644 --- a/pax-logging-log4j2/src/main/java/org/ops4j/pax/logging/log4j2/internal/Activator.java +++ b/pax-logging-log4j2/src/main/java/org/ops4j/pax/logging/log4j2/internal/Activator.java @@ -26,6 +26,8 @@ import java.util.Map; import java.util.Properties; +import org.apache.logging.log4j.core.impl.ThreadContextDataInjector; +import org.apache.logging.log4j.core.impl.ThreadContextDataProvider; import org.apache.logging.log4j.core.lookup.StrSubstitutor; import org.apache.logging.log4j.status.StatusLogger; import org.ops4j.pax.logging.EventAdminPoster; @@ -82,6 +84,9 @@ public void start(BundleContext bundleContext) throws Exception { boolean cm = BackendSupport.isConfigurationAdminAvailable(); + ThreadContextDataInjector.contextDataProviders.clear(); + ThreadContextDataInjector.contextDataProviders.add(new ThreadContextDataProvider()); + if (!cm) { StatusLogger.getLogger().info("Configuration Admin is not available."); } diff --git a/pom.xml b/pom.xml index bd7cd3621..bb517253d 100644 --- a/pom.xml +++ b/pom.xml @@ -229,7 +229,7 @@ 1.9.16 5.6.12 6.0.3 - 2.13.1 + 2.13.2 1_3 3.4.1.Final 3.1.0