Skip to content

Commit

Permalink
javadoc - Adding missing javadocs
Browse files Browse the repository at this point in the history
Signed-off-by: Usman Saleem <usman@usmans.info>
  • Loading branch information
usmansaleem committed Jun 27, 2024
1 parent be74a5c commit f77e927
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void createConsoleAppender() {
dim("%t"),
colorize("%-5level"),
dim("%c{1}"),
colorize("%msg%n%throwable")))
colorize("%msgc%n%throwable")))
.build();
final ConsoleAppender consoleAppender =
ConsoleAppender.newBuilder().setName("Console").setLayout(patternLayout).build();
Expand Down
2 changes: 1 addition & 1 deletion ethereum/evmtool/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSSZZZ} | %t | %-5level | %c{1} | %msg%n" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSSZZZ} | %t | %-5level | %c{1} | %msgc%n" />
</Console>
</Appenders>
<Loggers>
Expand Down
2 changes: 2 additions & 0 deletions util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jar {
dependencies {
api 'org.slf4j:slf4j-api'

annotationProcessor 'org.apache.logging.log4j:log4j-core'

implementation 'com.google.guava:guava'
implementation 'net.java.dev.jna:jna'
implementation 'org.apache.commons:commons-lang3'
Expand Down
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();
}
}
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);
}
}

0 comments on commit f77e927

Please sign in to comment.