Repository: logging-log4j2 Updated Branches: refs/heads/master 80354c6d2 -> 986a1ce03
[LOG4J2-1771]: Add Builder to ColumnConfig Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/986a1ce0 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/986a1ce0 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/986a1ce0 Branch: refs/heads/master Commit: 986a1ce03c0736f8eafa1b295cc4b98dfa32eda2 Parents: 80354c6 Author: Matt Sicker <[email protected]> Authored: Sat Jan 7 00:05:29 2017 -0600 Committer: Matt Sicker <[email protected]> Committed: Sat Jan 7 00:05:29 2017 -0600 ---------------------------------------------------------------------- .../core/appender/db/jdbc/ColumnConfig.java | 180 ++++++++++++++----- .../core/appender/db/jdbc/ColumnConfigTest.java | 120 ++++++++----- src/changes/changes.xml | 3 + 3 files changed, 215 insertions(+), 88 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/986a1ce0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfig.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfig.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfig.java index 1828382..2c9a5b9 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfig.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfig.java @@ -20,9 +20,10 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.Core; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; import org.apache.logging.log4j.core.layout.PatternLayout; import org.apache.logging.log4j.core.util.Booleans; import org.apache.logging.log4j.status.StatusLogger; @@ -85,61 +86,156 @@ public final class ColumnConfig { /** * Factory method for creating a column config within the plugin manager. * - * @param config The configuration object - * @param name The name of the database column as it exists within the database table. - * @param pattern The {@link PatternLayout} pattern to insert in this column. Mutually exclusive with - * {@code literalValue!=null} and {@code eventTimestamp=true} - * @param literalValue The literal value to insert into the column as-is without any quoting or escaping. Mutually - * exclusive with {@code pattern!=null} and {@code eventTimestamp=true}. - * @param eventTimestamp If {@code "true"}, indicates that this column is a date-time column in which the event - * timestamp should be inserted. Mutually exclusive with {@code pattern!=null} and - * {@code literalValue!=null}. - * @param unicode If {@code "true"}, indicates that the column is a Unicode String. - * @param clob If {@code "true"}, indicates that the column is a character LOB (CLOB). - * @return the created column config. + * @see Builder + * @deprecated use {@link #newBuilder()} */ - @PluginFactory - public static ColumnConfig createColumnConfig( - @PluginConfiguration final Configuration config, - @PluginAttribute("name") final String name, - @PluginAttribute("pattern") final String pattern, - @PluginAttribute("literal") final String literalValue, - @PluginAttribute("isEventTimestamp") final String eventTimestamp, - @PluginAttribute("isUnicode") final String unicode, - @PluginAttribute("isClob") final String clob) { + @Deprecated + public static ColumnConfig createColumnConfig(final Configuration config, final String name, final String pattern, + final String literalValue, final String eventTimestamp, + final String unicode, final String clob) { if (Strings.isEmpty(name)) { LOGGER.error("The column config is not valid because it does not contain a column name."); return null; } - final boolean isPattern = Strings.isNotEmpty(pattern); - final boolean isLiteralValue = Strings.isNotEmpty(literalValue); final boolean isEventTimestamp = Boolean.parseBoolean(eventTimestamp); final boolean isUnicode = Booleans.parseBoolean(unicode, true); final boolean isClob = Boolean.parseBoolean(clob); - if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) { - LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive."); - return null; + return newBuilder() + .setConfiguration(config) + .setName(name) + .setPattern(pattern) + .setLiteralValue(literalValue) + .setEventTimestamp(isEventTimestamp) + .setUnicode(isUnicode) + .setClob(isClob) + .build(); + } + + @PluginBuilderFactory + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder implements org.apache.logging.log4j.core.util.Builder<ColumnConfig> { + + @PluginConfiguration + private Configuration configuration; + + @PluginBuilderAttribute + @Required(message = "No name provided") + private String name; + + @PluginBuilderAttribute + private String pattern; + + @PluginBuilderAttribute + private String literalValue; + + @PluginBuilderAttribute + private boolean isEventTimestamp; + + @PluginBuilderAttribute + private boolean isUnicode = true; + + @PluginBuilderAttribute + private boolean isClob; + + /** + * The configuration object. + */ + public Builder setConfiguration(final Configuration configuration) { + this.configuration = configuration; + return this; + } + + /** + * The name of the database column as it exists within the database table. + */ + public Builder setName(final String name) { + this.name = name; + return this; } - if (isEventTimestamp) { - return new ColumnConfig(name, null, null, true, false, false); + /** + * The {@link PatternLayout} pattern to insert in this column. Mutually exclusive with + * {@code literalValue!=null} and {@code eventTimestamp=true} + */ + public Builder setPattern(final String pattern) { + this.pattern = pattern; + return this; } - if (isLiteralValue) { - return new ColumnConfig(name, null, literalValue, false, false, false); + + /** + * The literal value to insert into the column as-is without any quoting or escaping. Mutually exclusive with + * {@code pattern!=null} and {@code eventTimestamp=true}. + */ + public Builder setLiteralValue(final String literalValue) { + this.literalValue = literalValue; + return this; } - if (isPattern) { - final PatternLayout layout = - PatternLayout.newBuilder() - .withPattern(pattern) - .withConfiguration(config) - .withAlwaysWriteExceptions(false) - .build(); - return new ColumnConfig(name, layout, null, false, isUnicode, isClob); + + /** + * If {@code "true"}, indicates that this column is a date-time column in which the event timestamp should be + * inserted. Mutually exclusive with {@code pattern!=null} and {@code literalValue!=null}. + */ + public Builder setEventTimestamp(final boolean eventTimestamp) { + isEventTimestamp = eventTimestamp; + return this; } - LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true."); - return null; + /** + * If {@code "true"}, indicates that the column is a Unicode String. + */ + public Builder setUnicode(final boolean unicode) { + isUnicode = unicode; + return this; + } + + /** + * If {@code "true"}, indicates that the column is a character LOB (CLOB). + */ + public Builder setClob(final boolean clob) { + isClob = clob; + return this; + } + + @Override + public ColumnConfig build() { + if (Strings.isEmpty(name)) { + LOGGER.error("The column config is not valid because it does not contain a column name."); + return null; + } + + final boolean isPattern = Strings.isNotEmpty(pattern); + final boolean isLiteralValue = Strings.isNotEmpty(literalValue); + + if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) { + LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive."); + return null; + } + + if (isEventTimestamp) { + return new ColumnConfig(name, null, null, true, false, false); + } + + if (isLiteralValue) { + return new ColumnConfig(name, null, literalValue, false, false, false); + } + + if (isPattern) { + final PatternLayout layout = + PatternLayout.newBuilder() + .withPattern(pattern) + .withConfiguration(configuration) + .withAlwaysWriteExceptions(false) + .build(); + return new ColumnConfig(name, layout, null, false, isUnicode, isClob); + } + + LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true."); + return null; + } } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/986a1ce0/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfigTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfigTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfigTest.java index 301234e..f92378b 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfigTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/ColumnConfigTest.java @@ -17,85 +17,90 @@ package org.apache.logging.log4j.core.appender.db.jdbc; import org.apache.logging.log4j.util.Strings; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; public class ColumnConfigTest { - @Before - public void setUp() { - - } - - @After - public void tearDown() { - - } @Test public void testNullNameNoConfig() { - final ColumnConfig config = ColumnConfig.createColumnConfig(null, null, "%l", null, null, null, null); + final ColumnConfig config = ColumnConfig.newBuilder().setPattern("%l").build(); assertNull("The result should be null.", config); } @Test public void testPatternAndLiteralNoConfig() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", "%l", "literal", null, null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setPattern("%l") + .setLiteralValue("literal") + .build(); assertNull("The result should be null.", config); } @Test public void testPatternAndDateNoConfig() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", "%l", null, "true", null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setPattern("%l") + .setEventTimestamp(true) + .build(); assertNull("The result should be null.", config); } @Test public void testLiteralAndDateNoConfig() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", null, "literal", "true", null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setLiteralValue("literal") + .setEventTimestamp(true) + .build(); assertNull("The result should be null.", config); } @Test public void testNoSettingNoConfig01() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", null, null, null, null, null); + final ColumnConfig config = ColumnConfig.newBuilder().setName("col").build(); assertNull("The result should be null.", config); } @Test public void testNoSettingNoConfig02() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", null, null, "false", null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setEventTimestamp(false) + .build(); assertNull("The result should be null.", config); } @Test public void testNoSettingNoConfig03() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", Strings.EMPTY, Strings.EMPTY, Strings.EMPTY, null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setPattern(Strings.EMPTY) + .setLiteralValue(Strings.EMPTY) + .setEventTimestamp(false) + .build(); assertNull("The result should be null.", config); } @Test public void testDateColumn01() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", null, null, "true", null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setEventTimestamp(true) + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "columnName01", config.getColumnName()); + assertEquals("The column name is not correct.", "col", config.getColumnName()); assertNull("The pattern should be null.", config.getLayout()); assertNull("The literal value should be null.", config.getLiteralValue()); assertTrue("The timestamp flag should be true.", config.isEventTimestamp()); @@ -105,11 +110,15 @@ public class ColumnConfigTest { @Test public void testDateColumn02() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "anotherName02", null, null, "true", "true", "true"); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col2") + .setEventTimestamp(true) + .setUnicode(true) + .setClob(true) + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "anotherName02", config.getColumnName()); + assertEquals("The column name is not correct.", "col2", config.getColumnName()); assertNull("The pattern should be null.", config.getLayout()); assertNull("The literal value should be null.", config.getLiteralValue()); assertTrue("The timestamp flag should be true.", config.isEventTimestamp()); @@ -119,11 +128,13 @@ public class ColumnConfigTest { @Test public void testPatternColumn01() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", "%l", null, null, null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setPattern("%l") + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "columnName01", config.getColumnName()); + assertEquals("The column name is not correct.", "col", config.getColumnName()); assertNotNull("The pattern should not be null.", config.getLayout()); assertEquals("The pattern is not correct.", "%l", config.getLayout().toString()); assertNull("The literal value should be null.", config.getLiteralValue()); @@ -134,11 +145,17 @@ public class ColumnConfigTest { @Test public void testPatternColumn02() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "anotherName02", "%X{id} %level", Strings.EMPTY, "false", "false", "true"); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col2") + .setPattern("%X{id} %level") + .setLiteralValue(Strings.EMPTY) + .setEventTimestamp(false) + .setUnicode(false) + .setClob(true) + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "anotherName02", config.getColumnName()); + assertEquals("The column name is not correct.", "col2", config.getColumnName()); assertNotNull("The pattern should not be null.", config.getLayout()); assertEquals("The pattern is not correct.", "%X{id} %level", config.getLayout().toString()); assertNull("The literal value should be null.", config.getLiteralValue()); @@ -149,11 +166,17 @@ public class ColumnConfigTest { @Test public void testPatternColumn03() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "anotherName02", "%X{id} %level", Strings.EMPTY, "false", "true", "false"); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col3") + .setPattern("%X{id} %level") + .setLiteralValue(Strings.EMPTY) + .setEventTimestamp(false) + .setUnicode(true) + .setClob(false) + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "anotherName02", config.getColumnName()); + assertEquals("The column name is not correct.", "col3", config.getColumnName()); assertNotNull("The pattern should not be null.", config.getLayout()); assertEquals("The pattern is not correct.", "%X{id} %level", config.getLayout().toString()); assertNull("The literal value should be null.", config.getLiteralValue()); @@ -164,11 +187,13 @@ public class ColumnConfigTest { @Test public void testLiteralColumn01() { - final ColumnConfig config = - ColumnConfig.createColumnConfig(null, "columnName01", null, "literalValue01", null, null, null); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col") + .setLiteralValue("literalValue01") + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "columnName01", config.getColumnName()); + assertEquals("The column name is not correct.", "col", config.getColumnName()); assertNull("The pattern should be null.", config.getLayout()); assertNotNull("The literal value should be null.", config.getLiteralValue()); assertEquals("The literal value is not correct.", "literalValue01", config.getLiteralValue()); @@ -179,12 +204,15 @@ public class ColumnConfigTest { @Test public void testLiteralColumn02() { - final ColumnConfig config = ColumnConfig.createColumnConfig( - null, "anotherName02", null, "USER1.MY_SEQUENCE.NEXT", null, "true", "true" - ); + final ColumnConfig config = ColumnConfig.newBuilder() + .setName("col2") + .setLiteralValue("USER1.MY_SEQUENCE.NEXT") + .setUnicode(true) + .setClob(true) + .build(); assertNotNull("The result should not be null.", config); - assertEquals("The column name is not correct.", "anotherName02", config.getColumnName()); + assertEquals("The column name is not correct.", "col2", config.getColumnName()); assertNull("The pattern should be null.", config.getLayout()); assertNotNull("The literal value should be null.", config.getLiteralValue()); assertEquals("The literal value is not correct.", "USER1.MY_SEQUENCE.NEXT", config.getLiteralValue()); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/986a1ce0/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 690a3fd..bfefc24 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -228,6 +228,9 @@ <action issue="LOG4J2-1302" dev="rpopma" type="update"> The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2. </action> + <action issue="LOG4J2-1771" dev="mattsicker" type="add"> + Add a Builder to ColumnConfig and deprecate ColumnConfig.createColumnConfig(). + </action> <action issue="LOG4J2-1770" dev="mattsicker" type="add"> Add a Builder to JdbcAppender and deprecate JdbcAppender.createAppender(). </action>
