ppkarwasz commented on code in PR #3338: URL: https://github.com/apache/logging-log4j2/pull/3338#discussion_r1898979322
########## log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatter.java: ########## @@ -711,23 +488,114 @@ public void formatTo(final StringBuilder buffer, final Instant instant) { } }; } + + @Override + @Nullable + PatternFormatterFactory tryMerge(PatternFormatterFactory other, ChronoUnit thresholdPrecision) { + // We always merge consecutive static pattern factories + if (other instanceof StaticPatternFormatterFactory) { + final StaticPatternFormatterFactory otherStatic = (StaticPatternFormatterFactory) other; + return new StaticPatternFormatterFactory(this.literal + otherStatic.literal); + } + // We also merge a static pattern factory with a DTF factory + if (other instanceof DateTimeFormatterPatternFormatterFactory) { + final DateTimeFormatterPatternFormatterFactory otherDtf = + (DateTimeFormatterPatternFormatterFactory) other; + return new DateTimeFormatterPatternFormatterFactory( + this.pattern + otherDtf.pattern, otherDtf.precision); + } + return super.tryMerge(other, thresholdPrecision); + } + + static String escapeLiteral(String literal) { + StringBuilder sb = new StringBuilder(literal.length() + 2); + boolean inSingleQuotes = false; + for (int i = 0; i < literal.length(); i++) { + char c = literal.charAt(i); + if (c == '\'') { + if (inSingleQuotes) { + sb.append("'"); + } + inSingleQuotes = false; + sb.append("''"); + } else { + if (!inSingleQuotes) { + sb.append("'"); + } + inSingleQuotes = true; + sb.append(c); + } + } + if (inSingleQuotes) { + sb.append("'"); + } + return sb.toString(); + } } - static final class DynamicPatternSequence extends PatternSequence { + /** + * Formats the pattern using {@link DateTimeFormatter}. Review Comment: Fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org