Skip to content

Refactor StatusLogger to use precompiled Pattern constants #3719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.message.Message;
Expand Down Expand Up @@ -209,6 +210,44 @@ public class StatusLogger extends AbstractLogger {
*/
public static final String PROPERTIES_FILE_NAME = "log4j2.StatusLogger.properties";

/**
* Pattern that matches common separators in property names:
* <ul>
* <li>Dots ({@code .}) used in property paths</li>
* <li>Dashes ({@code -}) used in kebab-case</li>
* <li>Underscores ({@code _}) used in environment variables</li>
* </ul>
* <p>
* These are removed during normalization.
* </p>
*/
private static final Pattern SEPARATORS = Pattern.compile("[._-]");

/**
* Pattern that matches any character not in the ASCII Basic Latin block.
* <p>
* These characters are replaced with dots during normalization to avoid mismatches
* while preserving boundaries, e.g. {@code fooàö} becomes {@code foo..}.
* </p>
*/
private static final Pattern NON_ASCII = Pattern.compile("\\P{InBasic_Latin}");

/**
* Pattern that matches the {@code log4j2} prefix at the start of the property name.
* <p>
* This is replaced with {@code log4j} for internal normalization.
* </p>
*/
private static final Pattern PREFIX_LOG4J2 = Pattern.compile("^log4j2");

/**
* Pattern that matches property names starting with {@code log4j}, case-insensitively.
* <p>
* Used to determine if a given property name is relevant for Log4j configuration.
* </p>
*/
private static final Pattern LOG4J_PREFIX = Pattern.compile("^log4j.*", Pattern.CASE_INSENSITIVE);

/**
* Holder for user-provided {@link StatusLogger} configurations.
*
Expand Down Expand Up @@ -462,7 +501,8 @@ private static Map<String, Object> normalizeProperties(Properties... propertiesL
* @return {@code true}, if the property name is relevant; {@code false}, otherwise
*/
private static boolean isRelevantPropertyName(@Nullable final Object propertyName) {
return propertyName instanceof String && ((String) propertyName).matches("^(?i)log4j.*");
return propertyName instanceof String
&& LOG4J_PREFIX.matcher((String) propertyName).matches();
}

/**
Expand All @@ -476,19 +516,17 @@ private static boolean isRelevantPropertyName(@Nullable final Object propertyNam
* @return the normalized property name
*/
private static String normalizePropertyName(final String propertyName) {
return propertyName
// Remove separators:
// - dots (properties)
// - dashes (kebab-case)
// - underscores (environment variables)
.replaceAll("[._-]", "")
// Replace all non-ASCII characters.
// Don't remove, otherwise `fooàö` would incorrectly match with `foo`.
// It is safe to replace them with dots, since we've just removed all dots above.
.replaceAll("\\P{InBasic_Latin}", ".")
// Lowercase ASCII – this is safe, since we've just removed all non-ASCII
.toLowerCase(Locale.US)
.replaceAll("^log4j2", "log4j");
if (propertyName == null) {
return null;
}

String propertyNameInput = propertyName;
propertyNameInput = SEPARATORS.matcher(propertyNameInput).replaceAll("");
propertyNameInput = NON_ASCII.matcher(propertyNameInput).replaceAll(".");
propertyNameInput = propertyNameInput.toLowerCase(Locale.US);
propertyNameInput = PREFIX_LOG4J2.matcher(propertyNameInput).replaceFirst("log4j");

return propertyNameInput;
}
}

Expand Down