LOG4J2-3044 - Add RepeatPatternConverter
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/RepeatPatternConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/RepeatPatternConverter.java
new file mode 100644
index 0000000..0db0a3d
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/RepeatPatternConverter.java
@@ -0,0 +1,103 @@
+/*
+ * 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.core.pattern;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.plugins.Plugin;
+import org.apache.logging.log4j.util.PerformanceSensitive;
+import org.apache.logging.log4j.util.Strings;
+
+/**
+ * Equals pattern converter.
+ */
+@Plugin(name = "repeat", category = PatternConverter.CATEGORY)
+@ConverterKeys({"R","repeat" })
+@PerformanceSensitive("allocation")
+public final class RepeatPatternConverter extends LogEventPatternConverter {
+
+ private final String result;
+
+ /**
+ * Gets an instance of the class.
+ *
+ * @param config The current Configuration.
+ * @param options pattern options, an array of two elements: repeatString and count.
+ * @return instance of class.
+ */
+ public static RepeatPatternConverter newInstance(final Configuration config, final String[] options) {
+ if (options.length != 2) {
+ LOGGER.error("Incorrect number of options on repeat. Expected 2 received " + options.length);
+ return null;
+ }
+ if (options[0] == null) {
+ LOGGER.error("No string supplied on repeat");
+ return null;
+ }
+ if (options[1] == null) {
+ LOGGER.error("No repeat count supplied on repeat");
+ return null;
+ }
+ int count = 0;
+ String result = options[0];
+ try {
+ count = Integer.parseInt(options[1].trim());
+ result = Strings.repeat(options[0], count);
+ } catch (Exception ex) {
+ LOGGER.error("The repeat count is not an integer: {}", options[1].trim());
+ }
+
+ return new RepeatPatternConverter(result);
+ }
+
+ /**
+ * Construct the converter.
+ *
+ * @param result The repeated String
+
+ */
+ private RepeatPatternConverter(final String result) {
+ super("repeat", "repeat");
+ this.result = result;
+ }
+
+ /**
+ * Adds the repeated String to the buffer.
+ *
+ * @param obj event to format, may not be null.
+ * @param toAppendTo string buffer to which the formatted event will be appended. May not be null.
+ */
+ public void format(final Object obj, final StringBuilder toAppendTo) {
+ format(toAppendTo);
+ }
+
+ /**
+ * Adds the repeated String to the buffer.
+ *
+ * @param event event to format, may not be null.
+ * @param toAppendTo string buffer to which the formatted event will be appended. May not be null.
+ */
+ public void format(final LogEvent event, final StringBuilder toAppendTo) {
+ format(toAppendTo);
+ }
+
+ private void format(final StringBuilder toAppendTo) {
+ if (result != null) {
+ toAppendTo.append(result);
+ }
+ }
+}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutRepeatTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutRepeatTest.java
new file mode 100644
index 0000000..5b233e8
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutRepeatTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.core.layout;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.apache.logging.log4j.junit.Named;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * See (LOG4J2-905) Ability to disable (date) lookup completely, compatibility issues with other libraries like camel.
+ */
+@LoggerContextSource("PatternLayoutRepeat.xml")
+public class PatternLayoutRepeatTest {
+
+ @Test
+ public void testRepeatSymbol(final LoggerContext context, @Named("List") final ListAppender listAppender) {
+ listAppender.clear();
+ context.getLogger(PatternLayoutRepeatTest.class).info("Hello world");
+ final String string = listAppender.getMessages().get(0);
+ Assertions.assertTrue(string.contains("##########"), "Incorrect result: " + string);
+ }
+
+}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/RepeatPatternConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/RepeatPatternConverterTest.java
new file mode 100644
index 0000000..eac4c1a
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/RepeatPatternConverterTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.core.pattern;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * Tests that process ID succeeds.
+ */
+public class RepeatPatternConverterTest {
+ @Test
+ public void repeat() {
+ final String[] args = {"*", "10"};
+ final String expected = "**********";
+ PatternConverter converter = RepeatPatternConverter.newInstance(null, args);
+ assertNotNull(converter, "No RepeatPatternConverter returned");
+ StringBuilder sb = new StringBuilder();
+ converter.format(null, sb);
+ assertEquals(expected, sb.toString());
+ sb.setLength(0);
+ LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName("MyLogger") //
+ .setLevel(Level.DEBUG) //
+ .setMessage(new SimpleMessage("Hello")).build();
+ converter.format(event, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+
+}
\ No newline at end of file
diff --git a/log4j-core/src/test/resources/PatternLayoutRepeat.xml b/log4j-core/src/test/resources/PatternLayoutRepeat.xml
new file mode 100644
index 0000000..a2ab741
--- /dev/null
+++ b/log4j-core/src/test/resources/PatternLayoutRepeat.xml
@@ -0,0 +1,29 @@
+<?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="WARN">
+ <Appenders>
+ <List name="List">
+ <PatternLayout pattern="[%-5level] %c{1.} %R{#}{10} %msg %R{#}{10}%n" />
+ </List>
+ </Appenders>
+ <Loggers>
+ <Root level="debug">
+ <AppenderRef ref="List" />
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3a01186..631e34e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -169,6 +169,9 @@
</action>
</release>
<release version="2.15.0" date="2021-MM-DD" description="GA Release 2.15.0">
+ <action issue="LOG4J2-3044" dev="rgoers" type="add">
+ Add RepeatPatternConverter.
+ </action>
<action issue="LOG4J2-3041" dev="rgoers" type="update">
Allow a PatternSelector to be specified on GelfLayout.
</action>
diff --git a/src/site/asciidoc/manual/layouts.adoc b/src/site/asciidoc/manual/layouts.adoc
index 0ac0d78..efeacd6 100644
--- a/src/site/asciidoc/manual/layouts.adoc
+++ b/src/site/asciidoc/manual/layouts.adoc
@@ -1362,6 +1362,11 @@
|Outputs the number of milliseconds elapsed since the JVM was
started until the creation of the logging event.
+|[[PatternRepeat]] *R*{string}{count} +
+*repeat*{string}{count}
+|Produces a string containing the requested number of instances of the specified string.
+For example, "%repeat{\*}{2}" will result in the string "**".
+
|[[PatternReplace]] *replace*{pattern}{regex}{substitution}
|Replaces occurrences of 'regex', a regular expression, with its
replacement 'substitution' in the string resulting from evaluation of