Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2558

Literal values do not work with ColumnMapping for JdbcAppender

Agile BoardAttach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.11.1
    • 2.11.2
    • Appenders, JDBC
    • MacOS High Sierra 10.13.6 Darwin Kernel Version 17.7.0
      java.vm.specification.version: 1.8

      os.arch: x86_64

    Description

      I'm programmatically updating the configuration after it has been created

       

       

      final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
      final Configuration config = ctx.getConfiguration();
      
      ColumnConfig[] columnConfig = new ColumnConfig[0];
      
      ColumnMapping[] columnMappings = new ColumnMapping[1];
      
      columnMappings[0] = ColumnMapping.newBuilder()
              .setLiteral("'TEST_STRING'")
              .build();
      
      FactoryMethodConnectionSource myConnectionSource =
              FactoryMethodConnectionSource.createConnectionSource(
                      "com.ConnectionFactory",
                      "getConnection");
      Appender appender = org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender.newBuilder()
              .setTableName("MSG_LOG")
              .setConfiguration(config)
              .setColumnMappings(columnMappings)
              .setColumnConfigs(columnConfig)
              .setConnectionSource(myConnectionSource)
              .withName("db-appender")
              .build();
      
      appender.start();
      config.addAppender(appender);
      
      Map<String, LoggerConfig> loggers = config.getLoggers();
      loggers.forEach((loggerName, loggerConfig) -> {
          loggerConfig.addAppender(appender, org.apache.logging.log4j.Level.INFO, null);
          config.addLogger("org.apache.logging.log4j", loggerConfig);
      });
      ctx.updateLoggers();
      

      Creates JdbcDatabaseManager with sqlStatement

       

       

      INSERT INTO MSG_LOG (null) VALUES ('TEST_STRING'?)
      

      As you can see, PARAMETER_MARKER "?" gets added.

       

       

      Later in writeInternal method, there is a check for layout without an else condition

      if (layout != null) //line 164 of JdbcDatabaseManager
      

      Layout is null for my column so the parameter never get's set for the PreparedStatement which will throw an error

      Caused by: java.sql.SQLException: Missing IN or OUT parameter at index:: 1
      at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:2086)
      at oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:9713)
      at oracle.jdbc.driver.OraclePreparedStatementWrapper.addBatch(OraclePreparedStatementWrapper.java:1067)
      at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.addBatch(HikariProxyPreparedStatement.java)
      at org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager.writeInternal(JdbcDatabaseManager.java:199)
      ... 65 more
      

      So, we could create the sqlStatement without the ParameterMarker "?" or perhaps just make sure it gets set to null.

       

      I would be open to making a PR if this is an issue and other can reproduce.

       

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            brianyeh Brian Yeh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment