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

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


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 65551ce98f Add ConnectionID to AccessLogValve
65551ce98f is described below

commit 65551ce98f2a653f6b7045d1fca9a2ddafc3f374
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Feb 24 12:55:08 2025 +0000

    Add ConnectionID to AccessLogValve
---
 .../catalina/valves/AbstractAccessLogValve.java    | 61 ++++++++++++++++++++--
 webapps/docs/changelog.xml                         |  7 +++
 webapps/docs/config/valve.xml                      |  2 +
 3 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/catalina/valves/AbstractAccessLogValve.java 
b/java/org/apache/catalina/valves/AbstractAccessLogValve.java
index 2b1b064ca8..b462ebc470 100644
--- a/java/org/apache/catalina/valves/AbstractAccessLogValve.java
+++ b/java/org/apache/catalina/valves/AbstractAccessLogValve.java
@@ -115,6 +115,8 @@ import org.apache.tomcat.util.net.IPv6Utils;
  * <li><code>%{xxx}s</code> xxx is an attribute in the HttpSession
  * <li><code>%{xxx}t</code> xxx is an enhanced SimpleDateFormat pattern (see 
Configuration Reference document for
  * details on supported time patterns)
+ * <li><code>%{xxx}L</code> xxx is the identifier to log (see Configuration 
Reference document for details on supported
+ * identifiers)
  * <li><code>%{xxx}T</code> xxx is the unit for the time taken to process the 
request (see Configuration Reference
  * document for details on supported units)
  * </ul>
@@ -167,7 +169,12 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
         PEER
     }
 
-    // ------------------------------------------------------ Constructor
+    private enum IdentifierType {
+        CONNECTION,
+        UNKNOWN
+    }
+
+
     public AbstractAccessLogValve() {
         super(true);
     }
@@ -1677,6 +1684,48 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
         }
     }
 
+
+
+    /**
+     * Write identifier element %{xxx}L
+     */
+    protected class IdentifierElement implements AccessLogElement {
+
+        /**
+         * Type of identifier to log
+         */
+        private final IdentifierType identifierType;
+
+        public IdentifierElement() {
+            this(null);
+        }
+
+
+        public IdentifierElement(String type) {
+            switch (type) {
+                case "c":
+                    identifierType = IdentifierType.CONNECTION;
+                    break;
+                default:
+                    
log.error(sm.getString("accessLogValve.invalidIdentifierType", type));
+                    identifierType = IdentifierType.UNKNOWN;
+                    break;
+            }
+        }
+
+        @Override
+        public void addElement(CharArrayWriter buf, Date date, Request 
request, Response response, long time) {
+            switch(identifierType) {
+                case CONNECTION:
+                    
buf.append(request.getServletConnection().getConnectionId());
+                    break;
+                case UNKNOWN:
+                    buf.append("???");
+            }
+        }
+    }
+
+
     /**
      * Parse pattern string and create the array of AccessLogElement.
      *
@@ -1749,14 +1798,16 @@ public abstract class AbstractAccessLogValve extends 
ValveBase implements Access
      */
     protected AccessLogElement createAccessLogElement(String name, char 
pattern) {
         switch (pattern) {
-            case 'i':
-                return new HeaderElement(name);
+            case 'a':
+                return new RemoteAddrElement(name);
             case 'c':
                 return new CookieElement(name);
+            case 'i':
+                return new HeaderElement(name);
+            case 'L':
+                return new IdentifierElement(name);
             case 'o':
                 return new ResponseHeaderElement(name);
-            case 'a':
-                return new RemoteAddrElement(name);
             case 'p':
                 return new PortElement(name);
             case 'r':
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 8ad82411bd..1e358d206e 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -133,6 +133,13 @@
         Improve the checks for exposure to and protection against 
CVE-2024-56337
         so that reflection is not used unless required. (markt)
       </fix>
+      <add>
+        Add support for logging the connection ID (as returned by
+        <code>ServletRequest.getServletConnection().getConnectionId()</code>)
+        with the <code>AccessLogValve</code> and
+        <code>ExtendedAccessLogValve</code>. Based on pull request <pr>814</pr>
+        by Dmole. (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">
diff --git a/webapps/docs/config/valve.xml b/webapps/docs/config/valve.xml
index 1c9ae867b8..24963a9938 100644
--- a/webapps/docs/config/valve.xml
+++ b/webapps/docs/config/valve.xml
@@ -337,6 +337,8 @@
         remote (client) port (<code>xxx=remote</code>)</li>
     <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}L</code></b> write an identifier associated with the 
request where the only valid value for
+        <code>xxx</code> is <code>c</code> for connection.</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>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.


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

Reply via email to