This is an automated email from the ASF dual-hosted git repository.

schultz pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 1448eccfd86ef6385e0af629c138ab28405ed6c2
Author: Christopher Schultz <ch...@christopherschultz.net>
AuthorDate: Thu Apr 18 10:22:16 2024 -0400

    Re-factor ElapsedTimeElement to use a customizable Style
---
 .../catalina/valves/AbstractAccessLogValve.java    | 52 +++++++++++++++++-----
 webapps/docs/changelog.xml                         |  4 ++
 2 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/java/org/apache/catalina/valves/AbstractAccessLogValve.java 
b/java/org/apache/catalina/valves/AbstractAccessLogValve.java
index df942110ab..03acb492fa 100644
--- a/java/org/apache/catalina/valves/AbstractAccessLogValve.java
+++ b/java/org/apache/catalina/valves/AbstractAccessLogValve.java
@@ -1309,8 +1309,44 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
      * write time taken to process the request - %D, %T
      */
     protected static class ElapsedTimeElement implements AccessLogElement {
-        private final boolean micros;
-        private final boolean millis;
+        enum Style {
+            SECONDS {
+                @Override
+                public void append(CharArrayWriter buf, long time) {
+                    
buf.append(Long.toString(TimeUnit.NANOSECONDS.toSeconds(time)));
+                }
+            },
+            MILLISECONDS {
+                @Override
+                public void append(CharArrayWriter buf, long time) {
+                    
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMillis(time)));
+                }
+            },
+            MICROSECONDS {
+                @Override
+                public void append(CharArrayWriter buf, long time) {
+                    
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMicros(time)));
+                }
+            };
+
+            /**
+             * Append the time to the buffer in the appropriate format.
+             *
+             * @param buf The buffer to append to.
+             * @param time The time to log in nanoseconds.
+             */
+            public abstract void append(CharArrayWriter buf, long time);
+        }
+        private final Style style;
+
+        /**
+         * Create a new ElapsedTimeElement that will log the time in the 
specified style.
+         *
+         * @param style The elapsed-time style to use.
+         */
+        public ElapsedTimeElement(Style style) {
+            this.style = style;
+        }
 
         /**
          * @param micros <code>true</code>, write time in microseconds - %D
@@ -1318,20 +1354,12 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
          *                   time in seconds - %T
          */
         public ElapsedTimeElement(boolean micros, boolean millis) {
-            this.micros = micros;
-            this.millis = millis;
+            this(micros ? Style.MICROSECONDS : millis ? Style.MILLISECONDS : 
Style.SECONDS);
         }
 
         @Override
         public void addElement(CharArrayWriter buf, Date date, Request 
request, Response response, long time) {
-            if (micros) {
-                buf.append(Long.toString(TimeUnit.NANOSECONDS.toMicros(time)));
-            } else if (millis) {
-                buf.append(Long.toString(TimeUnit.NANOSECONDS.toMillis(time)));
-            } else {
-                // second
-                
buf.append(Long.toString(TimeUnit.NANOSECONDS.toSeconds(time)));
-            }
+            style.append(buf, time);
         }
     }
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e724ed92a6..3a295c0937 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -135,6 +135,10 @@
         dispatch is now performed rather than completing the request using the
         error page mechanism. (markt)
       </fix>
+      <add>
+        Re-factor ElapsedTimeElement in AbstractAccessLogValve to use a 
customizable
+        style. (schultz)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to