Repository: logging-log4j2 Updated Branches: refs/heads/master 09a211757 -> 6e6f999c2
Add a test that shows string substitution works in the JDBC Appender. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/6e6f999c Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/6e6f999c Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/6e6f999c Branch: refs/heads/master Commit: 6e6f999c22b4b717316c9da6a7ceb186f1a7f97b Parents: 09a2117 Author: Gary Gregory <[email protected]> Authored: Mon Oct 22 19:49:39 2018 -0600 Committer: Gary Gregory <[email protected]> Committed: Mon Oct 22 19:49:39 2018 -0600 ---------------------------------------------------------------------- .../jdbc/appender/JdbcDatabaseManager.java | 4 ++ .../JdbcAppenderStringSubstitutionTest.java | 55 ++++++++++++++++++++ .../log4j2-jdbc-string-substitution.xml | 43 +++++++++++++++ 3 files changed, 102 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6e6f999c/log4j-jdbc/src/main/java/org/apache/logging/log4j/jdbc/appender/JdbcDatabaseManager.java ---------------------------------------------------------------------- diff --git a/log4j-jdbc/src/main/java/org/apache/logging/log4j/jdbc/appender/JdbcDatabaseManager.java b/log4j-jdbc/src/main/java/org/apache/logging/log4j/jdbc/appender/JdbcDatabaseManager.java index f145e2c..01eaa13 100644 --- a/log4j-jdbc/src/main/java/org/apache/logging/log4j/jdbc/appender/JdbcDatabaseManager.java +++ b/log4j-jdbc/src/main/java/org/apache/logging/log4j/jdbc/appender/JdbcDatabaseManager.java @@ -117,6 +117,10 @@ public final class JdbcDatabaseManager extends AbstractDatabaseManager { } } + public String getSqlStatement() { + return sqlStatement; + } + @Deprecated @Override protected void writeInternal(final LogEvent event) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6e6f999c/log4j-jdbc/src/test/java/org/apache/logging/log4j/jdbc/appender/JdbcAppenderStringSubstitutionTest.java ---------------------------------------------------------------------- diff --git a/log4j-jdbc/src/test/java/org/apache/logging/log4j/jdbc/appender/JdbcAppenderStringSubstitutionTest.java b/log4j-jdbc/src/test/java/org/apache/logging/log4j/jdbc/appender/JdbcAppenderStringSubstitutionTest.java new file mode 100644 index 0000000..3b137cb --- /dev/null +++ b/log4j-jdbc/src/test/java/org/apache/logging/log4j/jdbc/appender/JdbcAppenderStringSubstitutionTest.java @@ -0,0 +1,55 @@ +/* + * 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.jdbc.appender; + +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +public class JdbcAppenderStringSubstitutionTest { + + private static final String VALUE = "MyTableName"; + private static final String KEY = "Test.TableName"; + + public JdbcAppenderStringSubstitutionTest() { + super(); + System.setProperty(KEY, VALUE); + } + + @AfterClass + public static void afterClass() { + System.getProperties().remove(KEY); + } + + @Rule + public final LoggerContextRule rule = new LoggerContextRule("org/apache/logging/log4j/jdbc/appender/log4j2-jdbc-string-substitution.xml"); + + @Test + public void test() throws Exception { + JdbcAppender appender = rule.getAppender("databaseAppender", JdbcAppender.class); + Assert.assertNotNull(appender); + @SuppressWarnings("resource") + JdbcDatabaseManager manager = appender.getManager(); + Assert.assertNotNull(manager); + String sqlStatement = manager.getSqlStatement(); + Assert.assertFalse(sqlStatement, sqlStatement.contains(KEY)); + Assert.assertTrue(sqlStatement, sqlStatement.contains(VALUE)); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6e6f999c/log4j-jdbc/src/test/resources/org/apache/logging/log4j/jdbc/appender/log4j2-jdbc-string-substitution.xml ---------------------------------------------------------------------- diff --git a/log4j-jdbc/src/test/resources/org/apache/logging/log4j/jdbc/appender/log4j2-jdbc-string-substitution.xml b/log4j-jdbc/src/test/resources/org/apache/logging/log4j/jdbc/appender/log4j2-jdbc-string-substitution.xml new file mode 100644 index 0000000..836c4b4 --- /dev/null +++ b/log4j-jdbc/src/test/resources/org/apache/logging/log4j/jdbc/appender/log4j2-jdbc-string-substitution.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<Configuration status="ERROR"> + + <Appenders> + <Console name="STDOUT"> + <PatternLayout pattern="%C{1.} %m %level MDC%X%n"/> + </Console> + <Jdbc name="databaseAppender" tableName="${sys:Test.TableName}" ignoreExceptions="false"> + <DriverManager connectionString="jdbc:h2:${sys:java.io.tmpdir}/h2/test_log4j;TRACE_LEVEL_SYSTEM_OUT=0" userName="sa" password="" /> + <ColumnMapping name="level" pattern="%level" /> + <ColumnMapping name="logger" pattern="%logger" /> + <ColumnMapping name="message" literal="'Hello World!'" /> + <ColumnMapping name="exception" pattern="%ex{full}" /> + </Jdbc> + </Appenders> + + <Loggers> + <Logger name="org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppenderColumnMappingLiteralTest" level="DEBUG" additivity="false"> + <AppenderRef ref="databaseAppender" /> + </Logger> + + <Root level="FATAL"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> + +</Configuration>
