ppkarwasz commented on PR #3789:
URL: https://github.com/apache/logging-log4j2/pull/3789#issuecomment-3066741660

   > @ppkarwasz, consider users having their custom instant formatters, some 
might even be relying on `{Fixed,Fast}DateFormat`. They will get excited about 
`NamedInstantPattern`, will try to use it, and it won't work. They will check 
the docs, have their _"Aha!"_ moment, enable legacy formatters, only to see 
that it still does not work. What do you think about this issue?
   Sure! Here's a clearer and more polished version of your answer. It keeps 
your technical points but improves the flow, tone, and readability:
   
   If you're suggesting that `NamedInstantPattern.getPattern()` should return 
either the DTF-style or legacy pattern depending on the value of the 
`log4j2.instantFormatter` config property — I’d consider that an anti-pattern.
   
   For example, code like:
   
   ```java
   DateTimeFormatter formatter = 
DateTimeFormatter.ofPattern(NamedInstantPattern.getPattern());
   formatter.format(Instant.now());
   ```
   
   would break if a user enables legacy formatters by setting 
`log4j2.instantFormatter=legacy`. That kind of hidden behavior change 
undermines the method’s predictability and makes it harder to use safely.
   
   If someone wants to use the named patterns with `FastDateFormat`, they’ll 
need to explicitly convert them. Here's an example of how that could be done:
   
   ```java
   public static String convertToLegacyFormat(final String pattern) {
       // Replace each consecutive 'S' beyond the first three with literal zeros
       final Matcher matcher = Pattern.compile("(?<=SSS)S+").matcher(pattern);
       final StringBuilder sb = new StringBuilder();
       while (matcher.find()) {
           matcher.appendReplacement(sb, "'" + 
"0".repeat(matcher.group().length()) + "'");
       }
       matcher.appendTail(sb);
       // Replace 'x' with 'X'
       return sb.toString().replace('x', 'X');
   }
   ```
   
   This keeps the behavior explicit and avoids magic behind the scenes.
   
   If someone wants to use the named pattern with `FixedDateFormat` they should 
consider using something more standard.


-- 
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

Reply via email to