logback / LOGBACK-1659 [Open]
Unnecessary OffsetDateTime creation in CachingDateFormat

==============================

Here's what changed in this issue in the last few minutes.
This issue has been created
This issue is now assigned to you.

View or comment on issue using this link
https://jira.qos.ch/browse/LOGBACK-1659

==============================
 Issue created
------------------------------

Bertrand Renuart created this issue on 13/Aug/22 4:27 PM
Summary:              Unnecessary OffsetDateTime creation in CachingDateFormat
Issue Type:           Improvement
Affects Versions:     1.3.0-beta0
Assignee:             Logback dev list
Components:           logback-core
Created:              13/Aug/22 4:27 PM
Priority:             Major
Reporter:             Bertrand Renuart
Description:
  CachingDateFormat creates a temporary OffsetDateTime from the given instant 
to apply the configured time zone (see 
[here|https://github.com/qos-ch/logback/blob/master/logback-core/src/main/java/ch/qos/logback/core/util/CachingDateFormatter.java#L68-L69])
 before passing it to the DateTimeFormatter for rendering.
  
  This temporary OffsetDateTime can be avoided by setting the ZoneId directly 
on the DateTimeFormatter when it is initialized. This is apparently what was 
foreseen in the constructor with the following 
[lines|https://github.com/qos-ch/logback/blob/master/logback-core/src/main/java/ch/qos/logback/core/util/CachingDateFormatter.java#L58]:
  {code:java}
  public CachingDateFormatter(String pattern, ZoneId aZoneId) {
     dtf = DateTimeFormatter.ofPattern(pattern);
     ...
     dtf.withZone(this.zoneId);
     ...
  }{code}
  Unfortunately this won't work because "{{withZone()}}" returns a new 
formatter which is not assigned to the "{{dtf}}" instance variable...
  
   
  
  *To summarise:*
  
  (1) In the constructor do something like:
  {code:java}
  dtf = DateTimeFormatter.ofPattern(pattern).withZone(this.zoneId)
  {code}
  (2) In the "format()" method do something like:
  {code:java}
  Instant instant = Instant.ofEpochMilli(now);
  String result = dtf.format(instant);
  {code}
  --> no need for an intermediate OffsetDateTime anymore


==============================
 This message was sent by Atlassian Jira (v8.8.0#808000-sha1:e2c7e59)

_______________________________________________
logback-dev mailing list
logback-dev@qos.ch
http://mailman.qos.ch/mailman/listinfo/logback-dev

Reply via email to