Benjamin Jaton created LOG4J2-1083:
--------------------------------------

             Summary: JDBC Appender : support for NULL values when using the 
pattern
                 Key: LOG4J2-1083
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1083
             Project: Log4j 2
          Issue Type: Improvement
          Components: Appenders
            Reporter: Benjamin Jaton


I am using the JDBCAppender:

{code}{
  "type" : "Jdbc",
  "name" : "MyDatabaseAppender",
  "tableName" : "LOGS",
  "ignoreExceptions" : "false",
  "ConnectionFactory" : {
    "class" : "test.ConnectionFactory",
    "method" : "getDatabaseAppenderDataSource"
  },
  "Column" : [
     { "name" : "ID", "pattern" : "%X{ID}", "isUnicode" : "false" },
     { "name" : "NUM", "pattern" : "%X{NUM}", "isUnicode" : "false" }
 ]
}{code}

As you can see, I am using the ThreadContext to pass the variables.

ThreadContext.put("ID", id++);
ThreadContext.put("NUM", "41");

The NUM column is of type BIGINT, and is nullable.

I am trying to set it to NULL with:
  ThreadContext.put("NUM", "NULL");
or
  ThreadContext.put("NUM", null);
or
  ThreadContext.put("NUM", "");
or 
  ThreadContext.remove("NUM");

Every time I get
{code}Caused by: org.apache.derby.client.am.SqlException: Error for batch 
element #0: Invalid character string format for type BIGINT.
    at org.apache.derby.client.am.Statement.completeExecute(Unknown 
Source){code}

I believe this is because, in
{code}org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager.writeInternal(){code}
 we always do
{code}this.statement.setString(i++, column.layout.toSerializable(event));{code}

So we always end up feeding the String "" or "NULL" to the statement.

I think the simplest way to do this would be to accept a new parameter like 
"setToNullIfEmpty".
It's also possible to get the meta data from the DB (type+nullable?) but that's 
much more work.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to