Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log Thread Context value in Tomcat access logs?

I am logging Thread Context values in application logs successfully with %X pattern. Now, I want to log the same value in Tomcat access logs.

Is there any pattern for that?

like image 853
Balaji Avatar asked Nov 19 '25 16:11

Balaji


2 Answers

You can't log thread context in access log, I'm afraid.

Here's available options:

%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if enableLookups for the connector is false)
%H - Request protocol
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%p - Local port on which this request was received. See also %{xxx}p below.
%q - Query string (prepended with a '?' if it exists)
%r - First line of the request (method and request URI)
%s - HTTP status code of the response
%S - User session ID
%t - Date and time, in Common Log Format
%u - Remote user that was authenticated (if any), else '-'
%U - Requested URL path
%v - Local server name
%D - Time taken to process the request, in millis
%T - Time taken to process the request, in seconds
%F - Time taken to commit the response, in millis
%I - Current request thread name (can compare later with stacktraces)

As far as I know, the only thing you can do is to log the current thread name (%I) and then look it up in the application logs in order get its context information.

like image 187
jfcorugedo Avatar answered Nov 21 '25 04:11

jfcorugedo


I had a similar requirement where I have to log correlation Id in my access logs which was set in MDC. I could not find a way to write MDC to access logs, but in my cases since I was writing the correlationId from MDC to outbound request headers, I leverage this to copy the value from headers into access logs.

Ex

server.tomcat.accesslog.pattern=%A %t %m %U %q %H %s %D %{X-Correlation-Id}o

X-Correlation-Id was my header name and o in the end says to read the value from outbound request.

This helped me out.

like image 29
Anunay Avatar answered Nov 21 '25 05:11

Anunay