Skip to content

Commit

Permalink
Clean up common logging code (#5212)
Browse files Browse the repository at this point in the history
Remove Elasticsearch from HTTP header warning message. Remove unused variables, redundant method calls and simplify conditions.

Signed-off-by: Rabi Panda <adnapibar@gmail.com>

Signed-off-by: Rabi Panda <adnapibar@gmail.com>
  • Loading branch information
adnapibar authored Nov 11, 2022
1 parent 4fc5c8d commit 3b46a99
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public HttpEntity getEntity() {
* @return {@code true} if the input string matches the specification
*/
private static boolean matchWarningHeaderPatternByPrefix(final String s) {
return s.startsWith("299 OpenSearch-") || s.startsWith("299 Elasticsearch-");
return s.startsWith("299 OpenSearch-");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public class HeaderWarning {
* Regular expression to test if a string matches the RFC7234 specification for warning headers. This pattern assumes that the warn code
* is always 299. Further, this pattern assumes that the warn agent represents a version of OpenSearch including the build hash.
*/
public static final Pattern WARNING_HEADER_PATTERN = Pattern.compile("299 " + // warn code
"(?:Elasticsearch-|OpenSearch-)" + // warn agent (note: Elasticsearch needed for bwc mixedCluster testing)
public static final Pattern WARNING_HEADER_PATTERN = Pattern.compile("299 OpenSearch-" + // warn code
"\\d+\\.\\d+\\.\\d+(?:-(?:alpha|beta|rc)\\d+)?(?:-SNAPSHOT)?-" + // warn agent
"(?:[a-f0-9]{7}(?:[a-f0-9]{33})?|unknown) " + // warn agent
"\"((?:\t| |!|[\\x23-\\x5B]|[\\x5D-\\x7E]|[\\x80-\\xFF]|\\\\|\\\\\")*)\"( " + // quoted warning value, captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -182,7 +180,6 @@ private static void configure(final Settings settings, final Path configsPath, f

final LoggerContext context = (LoggerContext) LogManager.getContext(false);

final Set<String> locationsWithDeprecatedPatterns = Collections.synchronizedSet(new HashSet<>());
final List<AbstractConfiguration> configurations = new ArrayList<>();
final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
Expand All @@ -206,12 +203,8 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr

// Redirect stdout/stderr to log4j. While we ensure Elasticsearch code does not write to those streams,
// third party libraries may do that
System.setOut(
new PrintStream(new LoggingOutputStream(LogManager.getLogger("stdout"), Level.INFO), false, StandardCharsets.UTF_8.name())
);
System.setErr(
new PrintStream(new LoggingOutputStream(LogManager.getLogger("stderr"), Level.WARN), false, StandardCharsets.UTF_8.name())
);
System.setOut(new PrintStream(new LoggingOutputStream(LogManager.getLogger("stdout"), Level.INFO), false, StandardCharsets.UTF_8));
System.setErr(new PrintStream(new LoggingOutputStream(LogManager.getLogger("stderr"), Level.WARN), false, StandardCharsets.UTF_8));
}

private static void configureStatusLogger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,34 @@ public static String format(final String prefix, final String messagePattern, fi
return messagePattern;
} else { // add the tail string which contains no variables and return
// the result.
sbuf.append(messagePattern.substring(i, messagePattern.length()));
sbuf.append(messagePattern.substring(i));
return sbuf.toString();
}
} else {
if (isEscapedDelimiter(messagePattern, j)) {
if (!isDoubleEscaped(messagePattern, j)) {
L--; // DELIM_START was escaped, thus should not be incremented
sbuf.append(messagePattern.substring(i, j - 1));
sbuf.append(messagePattern, i, j - 1);
sbuf.append(DELIM_START);
i = j + 1;
} else {
// The escape character preceding the delimiter start is
// itself escaped: "abc x:\\{}"
// we have to consume one backward slash
sbuf.append(messagePattern.substring(i, j - 1));
deeplyAppendParameter(sbuf, argArray[L], new HashSet<Object[]>());
sbuf.append(messagePattern, i, j - 1);
deeplyAppendParameter(sbuf, argArray[L], new HashSet<>());
i = j + 2;
}
} else {
// normal case
sbuf.append(messagePattern.substring(i, j));
deeplyAppendParameter(sbuf, argArray[L], new HashSet<Object[]>());
sbuf.append(messagePattern, i, j);
deeplyAppendParameter(sbuf, argArray[L], new HashSet<>());
i = j + 2;
}
}
}
// append the characters following the last {} pair.
sbuf.append(messagePattern.substring(i, messagePattern.length()));
sbuf.append(messagePattern.substring(i));
return sbuf.toString();
}

Expand All @@ -116,19 +116,11 @@ static boolean isEscapedDelimiter(String messagePattern, int delimiterStartIndex
return false;
}
char potentialEscape = messagePattern.charAt(delimiterStartIndex - 1);
if (potentialEscape == ESCAPE_CHAR) {
return true;
} else {
return false;
}
return potentialEscape == ESCAPE_CHAR;
}

static boolean isDoubleEscaped(String messagePattern, int delimiterStartIndex) {
if (delimiterStartIndex >= 2 && messagePattern.charAt(delimiterStartIndex - 2) == ESCAPE_CHAR) {
return true;
} else {
return false;
}
return delimiterStartIndex >= 2 && messagePattern.charAt(delimiterStartIndex - 2) == ESCAPE_CHAR;
}

private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Set<Object[]> seen) {
Expand Down

0 comments on commit 3b46a99

Please sign in to comment.