ppkarwasz commented on code in PR #3338: URL: https://github.com/apache/logging-log4j2/pull/3338#discussion_r1899416722
########## 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}. + */ + static final class DateTimeFormatterPatternFormatterFactory extends PatternFormatterFactory { - DynamicPatternSequence(final String content) { - super(content, contentPrecision(content)); + /** + * @param simplePattern A {@link DateTimeFormatter} pattern containing a single letter. + */ + DateTimeFormatterPatternFormatterFactory(final String simplePattern) { + this(simplePattern, patternPrecision(simplePattern)); } /** - * @param content a single-letter directive content complying (e.g., {@code H}, {@code HH}, or {@code pHH}) - * @return the time precision of the directive + * @param pattern Any {@link DateTimeFormatter} pattern. + * @param precision The maximum interval of time over which this pattern is constant. */ + DateTimeFormatterPatternFormatterFactory(final String pattern, final ChronoUnit precision) { + super(pattern, precision); + } + + InstantPatternFormatter createFormatter(final Locale locale, final TimeZone timeZone) { + final DateTimeFormatter dateTimeFormatter = + DateTimeFormatter.ofPattern(pattern, locale).withZone(timeZone.toZoneId()); + return new AbstractFormatter(pattern, locale, timeZone, precision) { + @Override + public void formatTo(final StringBuilder buffer, final Instant instant) { + final TemporalAccessor instantAccessor = toTemporalAccessor(instant); + dateTimeFormatter.formatTo(instantAccessor, buffer); + } + }; + } + + @Override @Nullable - private static ChronoUnit contentPrecision(final String content) { + PatternFormatterFactory tryMerge(PatternFormatterFactory other, ChronoUnit thresholdPrecision) { Review Comment: I improved the Javadoc of `tryMerge` in https://github.com/apache/logging-log4j2/pull/3338/commits/756ee61ac4573f0e5121de3a73a60ed94f8b6635. In general there are only two requirements for the `tryMerge` method: - the merged pattern sequence must be equivalent to the concatenation of the two sequences. - the merged pattern sequence must have the correct precision. All the remaining logic is there only to optimize the performance of formatting and we might change it in the future. That is why I don't want to document the exact rules for each `tryMerge` implementation. -- 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