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

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 49e1665  [LOG4J2-2657] Improve exception messages in the JDBC appender.
49e1665 is described below

commit 49e1665dd6f74d0214344ac7a7784d6d0e8be1d4
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jul 18 16:30:06 2019 -0400

    [LOG4J2-2657] Improve exception messages in the JDBC appender.
---
 .../core/appender/AppenderLoggingException.java    | 32 ++++++++--
 .../core/appender/db/jdbc/JdbcDatabaseManager.java | 69 +++++++++++++---------
 src/changes/changes.xml                            |  3 +
 3 files changed, 73 insertions(+), 31 deletions(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderLoggingException.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderLoggingException.java
index 4b65a2c..4174e4e 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderLoggingException.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderLoggingException.java
@@ -24,17 +24,18 @@ import org.apache.logging.log4j.LoggingException;
  * using the {@link org.apache.logging.log4j.status.StatusLogger}. Appenders 
should only throw exceptions when an error
  * prevents an event from being written. Appenders <em>must</em> throw an 
exception in this case so that error-handling
  * features like the {@link FailoverAppender} work properly.
- *
+ * <p>
  * Also note that appenders <em>must</em> provide a way to suppress exceptions 
when the user desires and abide by
  * that instruction. See {@link 
org.apache.logging.log4j.core.Appender#ignoreExceptions()}, which is the 
standard
  * way to do this.
+ * </p>
  */
 public class AppenderLoggingException extends LoggingException {
 
     private static final long serialVersionUID = 6545990597472958303L;
 
     /**
-     * Construct an exception with a message.
+     * Constructs an exception with a message.
      *
      * @param message The reason for the exception
      */
@@ -43,7 +44,18 @@ public class AppenderLoggingException extends 
LoggingException {
     }
 
     /**
-     * Construct an exception with a message and underlying cause.
+     * Constructs an exception with a message.
+     *
+     * @param format The reason format for the exception, see {@link 
String#format(String, Object...)}.
+     * @param args The reason arguments for the exception, see {@link 
String#format(String, Object...)}.
+     * @since 2.12.1
+     */
+    public AppenderLoggingException(final String format, Object... args) {
+        super(String.format(format, args));
+    }
+
+    /**
+     * Constructs an exception with a message and underlying cause.
      *
      * @param message The reason for the exception
      * @param cause The underlying cause of the exception
@@ -53,11 +65,23 @@ public class AppenderLoggingException extends 
LoggingException {
     }
 
     /**
-     * Construct an exception with an underlying cause.
+     * Constructs an exception with an underlying cause.
      *
      * @param cause The underlying cause of the exception
      */
     public AppenderLoggingException(final Throwable cause) {
         super(cause);
     }
+
+    /**
+     * Constructs an exception with a message.
+     *
+     * @param cause The underlying cause of the exception
+     * @param format The reason format for the exception, see {@link 
String#format(String, Object...)}.
+     * @param args The reason arguments for the exception, see {@link 
String#format(String, Object...)}.
+     * @since 2.12.1
+     */
+    public AppenderLoggingException(final Throwable cause, final String 
format, Object... args) {
+        super(String.format(format, args), cause);
+    }
 }
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
index da92a10..71b3b5e 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
@@ -202,6 +202,11 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
             shutdown = true;
         }
 
+               @Override
+               public String toString() {
+                       return String.format("Reconnector [latch=%s, 
shutdown=%s]", latch, shutdown);
+               }
+
     }
 
     private static final class ResultSetColumnMetaData {
@@ -497,14 +502,14 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
             // Reconnect
             if (reconnector != null && !factoryData.immediateFail) {
                 reconnector.latch();
-                if (connection == null) {
-                    throw new AppenderLoggingException(
-                            "Error writing to JDBC Manager '" + getName() + 
"': JDBC connection not available.");
-                }
-                if (statement == null) {
-                    throw new AppenderLoggingException(
-                            "Error writing to JDBC Manager '" + getName() + 
"': JDBC statement not available.");
-                }
+                               if (connection == null) {
+                                       throw new AppenderLoggingException(
+                                                       "Error writing to JDBC 
Manager '%s': JDBC connection not available [%s]", getName(), fieldsToString());
+                               }
+                               if (statement == null) {
+                                       throw new AppenderLoggingException(
+                                                       "Error writing to JDBC 
Manager '%s': JDBC statement not available [%s].", getName(), connection, 
fieldsToString());
+                               }
             }
         }
     }
@@ -548,9 +553,10 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
                 logger().debug("Committing Connection {}", this.connection);
                 this.connection.commit();
             }
-        } catch (final SQLException e) {
-            throw new DbAppenderLoggingException("Failed to commit transaction 
logging event or flushing buffer.", e);
-        } finally {
+               } catch (final SQLException e) {
+                       throw new AppenderLoggingException(e, "Failed to commit 
transaction logging event or flushing buffer [%s]",
+                                       fieldsToString());
+               } finally {
             closeResources(true);
         }
         return closed;
@@ -621,6 +627,13 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
         return sb.toString();
     }
 
+       private String fieldsToString() {
+               return String.format(
+                               "columnConfigs=%s, sqlStatement=%s, 
factoryData=%s, connection=%s, statement=%s, reconnector=%s, 
isBatchSupported=%s, columnMetaData=%s",
+                               columnConfigs, sqlStatement, factoryData, 
connection, statement, reconnector, isBatchSupported,
+                               columnMetaData);
+       }
+
     public ConnectionSource getConnectionSource() {
         return factoryData.connectionSource;
     }
@@ -692,10 +705,10 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
                         factoryData, reconnectEx, reconnector.getName(), 
reconnectEx);
                 reconnector.start();
                 reconnector.latch();
-                if (connection == null || statement == null) {
-                    throw new AppenderLoggingException(
-                            String.format("Error sending to %s for %s", 
getName(), factoryData), exception);
-                }
+                               if (connection == null || statement == null) {
+                                       throw new 
AppenderLoggingException(exception, "Error sending to %s for %s [%s]", 
getName(),
+                                                       factoryData, 
fieldsToString());
+                               }
             }
         }
     }
@@ -755,9 +768,10 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
                 if (resultSetColumnMetaData.isStringType()) {
                     value = resultSetColumnMetaData.truncate(value.toString());
                 }
-            } else {
-                logger().error("Missing ResultSetColumnMetaData for {}", 
nameKey);
-            }
+                       } else {
+                               logger().error("Missing ResultSetColumnMetaData 
for {}, connection={}, statement={}", nameKey,
+                                               connection, statement);
+                       }
         }
         return value;
     }
@@ -768,7 +782,8 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
         try {
                        if (!this.isRunning() || isClosed(this.connection) || 
isClosed(this.statement)) {
                                throw new AppenderLoggingException(
-                                               "Cannot write logging event; 
JDBC manager not connected to the database.");
+                                               "Cannot write logging event; 
JDBC manager not connected to the database, running=%s, [%s]).",
+                                               isRunning(), fieldsToString());
                        }
             // Clear in case there are leftovers.
             statement.clearParameters();
@@ -829,14 +844,14 @@ public final class JdbcDatabaseManager extends 
AbstractDatabaseManager {
 
             if (this.isBatchSupported) {
                 this.statement.addBatch();
-            } else if (this.statement.executeUpdate() == 0) {
-                throw new AppenderLoggingException(
-                        "No records inserted in database table for log event 
in JDBC manager.");
-            }
-        } catch (final SQLException e) {
-            throw new DbAppenderLoggingException(
-                    "Failed to insert record for log event in JDBC manager: " 
+ e.getMessage(), e);
-        } finally {
+                       } else if (this.statement.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 AppenderLoggingException(e, "Failed to insert 
record for log event in JDBC manager: %s [%s]", e,
+                                       fieldsToString());
+               } finally {
             // Release ASAP
             try {
                // statement can be null when a AppenderLoggingException is 
thrown at the start of this method
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a5af9e5..24c090f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -36,6 +36,9 @@
       <action issue="LOG4J2-2646" dev="ggregory" type="update">
         Update MongoDB 3 driver from 3.10.1 to 3.10.2.
       </action>
+      <action issue="LOG4J2-2657" dev="ggregory" type="update">
+        Improve exception messages in the JDBC appender.
+      </action>
     </release>
     <release version="2.12.0" date="2019-06-23" description="GA Release 
2.12.0">
       <action issue="LOG4J2-2547" dev="rgoers" type="fix">

Reply via email to