You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2021/03/15 16:19:15 UTC
[logging-log4j2] branch release-2.x updated: LOG4J2-3044 - Add
RepeatPatternConverter
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 32369ad LOG4J2-3044 - Add RepeatPatternConverter
32369ad is described below
commit 32369ad3517ebe9cd1767a50054b337fd8c14cbf
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Mar 15 09:19:02 2021 -0700
LOG4J2-3044 - Add RepeatPatternConverter
---
.../log4j/core/pattern/RepeatPatternConverter.java | 103 +++++++++++++++++++++
.../core/pattern/RepeatPatternConverterTest.java | 51 ++++++++++
src/changes/changes.xml | 3 +
src/site/xdoc/manual/layouts.xml.vm | 10 ++
4 files changed, 167 insertions(+)
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..df3179b
--- /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.core.config.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({ ":|","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/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/src/changes/changes.xml b/src/changes/changes.xml
index 1c4ffad..b2039cc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
- "remove" - Removed
-->
<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/xdoc/manual/layouts.xml.vm b/src/site/xdoc/manual/layouts.xml.vm
index e8f6368..0106562 100644
--- a/src/site/xdoc/manual/layouts.xml.vm
+++ b/src/site/xdoc/manual/layouts.xml.vm
@@ -1604,6 +1604,16 @@ WARN [main]: Message 2</pre>
</tr>
<tr>
<td align="center">
+ <a name="PatternRepeat"/>
+ <b>R</b>{string}{length}<br />
+ <b>repeat</b>{string}{length}
+ </td>
+ <td>Produces a string containing the requested number of instances of the specified string. For example,
+ "%repeat{*}{2}" will result in the string "**".
+ </td>
+ </tr>
+ <tr>
+ <td align="center">
<a name="PatternReplace"/>
<b>replace</b>{pattern}{regex}{substitution}
</td>