ppkarwasz commented on code in PR #3338: URL: https://github.com/apache/logging-log4j2/pull/3338#discussion_r1899417205
########## log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatter.java: ########## @@ -806,26 +674,114 @@ private static String removePadding(final String content) { } } - static final class CompositePatternSequence extends PatternSequence { + static class SecondPatternFormatterFactory extends PatternFormatterFactory { + + private final boolean printSeconds; + private final String separator; + private final int fractionalDigits; + + SecondPatternFormatterFactory(boolean printSeconds, String separator, int fractionalDigits) { + super( + createPattern(printSeconds, separator, fractionalDigits), + determinePrecision(printSeconds, fractionalDigits)); + this.printSeconds = printSeconds; + this.separator = separator; + this.fractionalDigits = fractionalDigits; + } - CompositePatternSequence(final List<PatternSequence> sequences) { - super(concatSequencePatterns(sequences), findSequenceMaxPrecision(sequences)); - // Only allow two or more sequences - if (sequences.size() < 2) { - throw new IllegalArgumentException("was expecting two or more sequences: " + sequences); + private static String createPattern(boolean printSeconds, String separator, int fractionalDigits) { + StringBuilder builder = new StringBuilder(); + if (printSeconds) { + builder.append("ss"); } + builder.append(StaticPatternFormatterFactory.escapeLiteral(separator)); + if (fractionalDigits > 0) { + builder.append(Strings.repeat("S", fractionalDigits)); + } + return builder.toString(); + } + + private static ChronoUnit determinePrecision(boolean printSeconds, int digits) { + return digits > 6 + ? ChronoUnit.NANOS + : digits > 3 + ? ChronoUnit.MICROS + : digits > 0 ? ChronoUnit.MILLIS : printSeconds ? ChronoUnit.SECONDS : ChronoUnit.FOREVER; } - @SuppressWarnings("OptionalGetWithoutIsPresent") - private static ChronoUnit findSequenceMaxPrecision(List<PatternSequence> sequences) { - return sequences.stream() - .map(sequence -> sequence.precision) - .min(Comparator.comparing(ChronoUnit::getDuration)) - .get(); + private static void formatSeconds(StringBuilder buffer, Instant instant) { + long secondsInMinute = instant.getEpochSecond() % 60L; + buffer.append((char) ((secondsInMinute / 10L) + '0')); + buffer.append((char) ((secondsInMinute % 10L) + '0')); } - private static String concatSequencePatterns(List<PatternSequence> sequences) { - return sequences.stream().map(sequence -> sequence.pattern).collect(Collectors.joining()); + private void formatFractionalDigits(StringBuilder buffer, Instant instant) { + final int offset = buffer.length(); + buffer.setLength(offset + fractionalDigits); + long value = instant.getNanoOfSecond(); + int valuePrecision = 9; + // Skip digits beyond the requested precision + while (fractionalDigits < valuePrecision) { + valuePrecision--; + value = value / 10L; + } + // Print the digits + while (0 < valuePrecision--) { + buffer.setCharAt(offset + valuePrecision, (char) ('0' + value % 10L)); + value = value / 10L; + } + } + + @Override + InstantPatternFormatter createFormatter(Locale locale, TimeZone timeZone) { + if (!printSeconds) { + return new AbstractFormatter(pattern, locale, timeZone, precision) { + @Override + public void formatTo(StringBuilder buffer, Instant instant) { + buffer.append(separator); + formatFractionalDigits(buffer, instant); + } + }; + } + if (fractionalDigits == 0) { + return new AbstractFormatter(pattern, locale, timeZone, precision) { + @Override + public void formatTo(StringBuilder buffer, Instant instant) { + formatSeconds(buffer, instant); + buffer.append(separator); + } + }; + } + return new AbstractFormatter(pattern, locale, timeZone, precision) { + @Override + public void formatTo(StringBuilder buffer, Instant instant) { + formatSeconds(buffer, instant); + buffer.append(separator); + formatFractionalDigits(buffer, instant); + } + }; + } + + @Override + @Nullable + PatternFormatterFactory tryMerge(PatternFormatterFactory other, ChronoUnit thresholdPrecision) { Review Comment: I improved the Javadoc of `tryMerge`: https://github.com/apache/logging-log4j2/pull/3338/commits/756ee61ac4573f0e5121de3a73a60ed94f8b6635 See comment above. -- 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