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

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


The following commit(s) were added to refs/heads/main by this push:
     new e9046d96a6 Add support for timescales with time-taken access log 
token. (#721)
e9046d96a6 is described below

commit e9046d96a6fd3b23b9b3288154f4bb7ea2f7f2cd
Author: Christopher Schultz <ch...@christopherschultz.net>
AuthorDate: Fri Apr 26 13:17:57 2024 -0400

    Add support for timescales with time-taken access log token. (#721)
    
    Add support for timescales with time-taken access log token.
    
    Add support for nanosecond and fractional-second timescales.
---
 .../catalina/valves/AbstractAccessLogValve.java    | 32 +++++++++++++++++++---
 .../catalina/valves/ExtendedAccessLogValve.java    | 14 +++++++++-
 webapps/docs/changelog.xml                         |  5 ++++
 webapps/docs/config/valve.xml                      |  9 ++++--
 4 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/java/org/apache/catalina/valves/AbstractAccessLogValve.java 
b/java/org/apache/catalina/valves/AbstractAccessLogValve.java
index dd29a5ec37..2628c654e2 100644
--- a/java/org/apache/catalina/valves/AbstractAccessLogValve.java
+++ b/java/org/apache/catalina/valves/AbstractAccessLogValve.java
@@ -1314,6 +1314,19 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
                     
buf.append(Long.toString(TimeUnit.NANOSECONDS.toSeconds(time)));
                 }
             },
+            SECONDS_FRACTIONAL {
+                @Override
+                public void append(CharArrayWriter buf, long time) {
+                    time = time / 1000000; // Convert to millis
+                    buf.append(Long.toString(time / 1000));
+                    buf.append('.');
+                    int remains = (int) (time % 1000);
+                    buf.append(Long.toString(remains / 100));
+                    remains = remains % 100;
+                    buf.append(Long.toString(remains / 10));
+                    buf.append(Long.toString(remains % 10));
+                }
+            },
             MILLISECONDS {
                 @Override
                 public void append(CharArrayWriter buf, long time) {
@@ -1325,6 +1338,12 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
                 public void append(CharArrayWriter buf, long time) {
                     
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMicros(time)));
                 }
+            },
+            NANOSECONDS {
+                @Override
+                public void append(CharArrayWriter buf, long time) {
+                    buf.append(Long.toString(time));
+                }
             };
 
             /**
@@ -1335,10 +1354,11 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
              */
             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.
+         * Creates a new ElapsedTimeElement that will log the time in the 
specified style.
          *
          * @param style The elapsed-time style to use.
          */
@@ -1758,10 +1778,14 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
                 return new DateAndTimeElement(name);
             case 'T':
                 // ms for milliseconds, us for microseconds, and s for seconds
-                if ("ms".equals(name)) {
-                    return new ElapsedTimeElement(false, true);
+                if ("ns".equals(name)) {
+                    return new 
ElapsedTimeElement(ElapsedTimeElement.Style.NANOSECONDS);
                 } else if ("us".equals(name)) {
-                    return new ElapsedTimeElement(true, false);
+                    return new 
ElapsedTimeElement(ElapsedTimeElement.Style.MICROSECONDS);
+                } else if ("ms".equals(name)) {
+                    return new 
ElapsedTimeElement(ElapsedTimeElement.Style.MILLISECONDS);
+                } else if ("fracsec".equals(name)) {
+                    return new 
ElapsedTimeElement(ElapsedTimeElement.Style.SECONDS_FRACTIONAL);
                 } else {
                     return new ElapsedTimeElement(false, false);
                 }
diff --git a/java/org/apache/catalina/valves/ExtendedAccessLogValve.java 
b/java/org/apache/catalina/valves/ExtendedAccessLogValve.java
index f7368b9523..6f5fbe6c6e 100644
--- a/java/org/apache/catalina/valves/ExtendedAccessLogValve.java
+++ b/java/org/apache/catalina/valves/ExtendedAccessLogValve.java
@@ -541,7 +541,19 @@ public class ExtendedAccessLogValve extends AccessLogValve 
{
             if (tokenizer.hasSubToken()) {
                 String nextToken = tokenizer.getToken();
                 if ("taken".equals(nextToken)) {
-                    return new ElapsedTimeElement(false, false);
+                    nextToken = tokenizer.getToken();
+
+                    if ("ns".equals(nextToken)) {
+                        return new 
ElapsedTimeElement(ElapsedTimeElement.Style.NANOSECONDS);
+                    } else if("us".equals(nextToken)) {
+                        return new 
ElapsedTimeElement(ElapsedTimeElement.Style.MICROSECONDS);
+                    } else if ("ms".equals(nextToken)) {
+                        return new 
ElapsedTimeElement(ElapsedTimeElement.Style.MILLISECONDS);
+                    } else if ("fracsec".equals(nextToken)) {
+                        return new 
ElapsedTimeElement(ElapsedTimeElement.Style.SECONDS_FRACTIONAL);
+                    } else {
+                        return new 
ElapsedTimeElement(ElapsedTimeElement.Style.SECONDS);
+                    }
                 }
             } else {
                 return new TimeElement();
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index d6bea23d29..cc82d7932b 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -137,6 +137,11 @@
         Re-factor ElapsedTimeElement in AbstractAccessLogValve to use a 
customizable
         style. (schultz)
       </add>
+      <add>
+        Add more timescale options to AccessLogValve and 
ExtendedAccessLogValve.
+        Allow timescales to apply to "time-taken" token in 
ExtendedAccessLogValve.
+        (schultz)
+      </add>
       <fix>
         Fix WebDAV lock null (locks for non existing resources) thread safety
         and removal. (remm)
diff --git a/webapps/docs/config/valve.xml b/webapps/docs/config/valve.xml
index 9e5fb8d20d..ae71ae0751 100644
--- a/webapps/docs/config/valve.xml
+++ b/webapps/docs/config/valve.xml
@@ -338,8 +338,9 @@
     <li><b><code>%{xxx}t</code></b> write timestamp at the end of the request 
formatted using the
         enhanced SimpleDateFormat pattern <code>xxx</code></li>
     <li><b><code>%{xxx}T</code></b> write time taken to process the request 
using unit <code>xxx</code>
-        where valid units are <code>ms</code> for milliseconds, 
<code>us</code> for microseconds,
-        and <code>s</code> for seconds. <code>%{s}T</code> is equivalent to 
<code>%T</code> as well
+        where valid units are <code>ns</code> for nanoseconds, <code>us</code> 
for microseconds,
+        <code>ms</code> for milliseconds, <code>fracsec</code> for fractional 
seconds, or <code>s</code> for whole seconds.
+        <code>%{s}T</code> is equivalent to <code>%T</code> as well
         as <code>%{us}T</code> is equivalent to <code>%D</code>.</li>
     </ul>
 
@@ -465,7 +466,9 @@
     <li><b>s-ip</b> - Local IP address</li>
     <li><b>sc-status</b> - HTTP status code of the response</li>
     <li><b>time</b> - Time the request was served in HH:mm:ss format for 
GMT</li>
-    <li><b>time-taken</b> - Time (in seconds) taken to serve the request</li>
+    <li><b>time-taken</b> - Time (in seconds) taken to serve the request. Can 
also use the
+      suffixes <code>-ns</code> for nanoseconds, <code>-us</code> for 
microseconds,
+      <code>-ms</code> for milliseconds, <code>-fracsec</code> for fractional 
seconds.</li>
     <li><b>x-threadname</b> - Current request thread name (can compare later 
with stacktraces)</li>
     </ul>
 


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

Reply via email to