-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Usman Saleem <usman@usmans.info>
- Loading branch information
1 parent
be74a5c
commit f77e927
Showing
5 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
util/src/main/java/org/hyperledger/besu/util/log4j/plugin/BesuLogMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* Licensed 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.util.log4j.plugin; | ||
|
||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.pattern.ConverterKeys; | ||
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; | ||
import org.apache.logging.log4j.core.pattern.PatternConverter; | ||
|
||
/** | ||
* Besu Log4j2 plugin for cleaner message logging. | ||
* | ||
* <p>Usage: In the pattern layout configuration, replace {@code %msg} with {@code %msgc}. | ||
*/ | ||
@Plugin(name = "BesuLogMessageConverter", category = PatternConverter.CATEGORY) | ||
@ConverterKeys({"msgc"}) | ||
public class BesuLogMessageConverter extends LogEventPatternConverter { | ||
|
||
private BesuLogMessageConverter() { | ||
super("BesuLogMessageConverter", null); | ||
} | ||
|
||
/** | ||
* Creates new instance of this class. Required by Log4j2. | ||
* | ||
* @param options Array of options | ||
* @return instance of this class | ||
*/ | ||
@SuppressWarnings("unused") // used by Log4j2 | ||
public static BesuLogMessageConverter newInstance(final String[] options) { | ||
return new BesuLogMessageConverter(); | ||
} | ||
|
||
@Override | ||
public void format(final LogEvent event, final StringBuilder toAppendTo) { | ||
final String filteredString = formatBesuLogMessage(event.getMessage().getFormattedMessage()); | ||
toAppendTo.append(filteredString); | ||
} | ||
|
||
/** | ||
* Format Besu log message. | ||
* | ||
* @param input The log message | ||
* @return The formatted log message | ||
*/ | ||
public static String formatBesuLogMessage(final String input) { | ||
final StringBuilder builder = new StringBuilder(input.length()); | ||
char prevChar = 0; | ||
|
||
for (int i = 0; i < input.length(); i++) { | ||
final char c = input.charAt(i); | ||
|
||
if (c == 0x0A) { | ||
if (prevChar == 0x0D) { | ||
builder.append(prevChar); | ||
} | ||
builder.append(c); | ||
} else if (c == 0x09 || !Character.isISOControl(c)) { | ||
builder.append(c); | ||
} | ||
|
||
prevChar = c; | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
util/src/test/java/org/hyperledger/besu/util/log4j/plugin/BesuLogMessageConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* Licensed 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.util.log4j.plugin; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class BesuLogMessageConverterTest { | ||
|
||
@Test | ||
public void logCleanup() { | ||
final StringBuilder testDataBuilder = new StringBuilder("log "); | ||
for (int i = 0; i <= 0x001F; i++) { | ||
testDataBuilder.append((char) i); | ||
} | ||
for (int i = 0x007F; i <= 0x009F; i++) { | ||
testDataBuilder.append((char) i); | ||
} | ||
testDataBuilder.append((char) 0x0D).append((char) 0x0A).append("message"); | ||
|
||
String testData = testDataBuilder.toString(); | ||
String cleanedData = BesuLogMessageConverter.formatBesuLogMessage(testData); | ||
String expectedData = String.format("log %c%c%c%cmessage", 0x09, 0x0A, 0x0D, 0x0A); | ||
assertThat(cleanedData).isEqualTo(expectedData); | ||
} | ||
} |