Issue
I am using log4j2.xml and need to print the Tomcat Access Logs to the Console in my Spring Boot Application. Please help how to do it, as I have been stuck on this. I have tried to configure as follows in the application.properties, but the logger getting printed is not in a json format.
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=/dev
server.tomcat.accesslog.prefix=stdout
server.tomcat.accesslog.console.pattern="{\"time\": \"%d\", \"level\": \"%p\", \"correlation-id\": \"%X{X-Correlation-Id}\", \"source\": \"%logger{63}:%L\", \"message\": \"%replace(%m%wEx{6}){'[\r\n]+', '\\n'}%nopex\", \"source\":\"ACCESS_LOGS\"}%n"
server.tomcat.accesslog.suffix=
server.tomcat.accesslog.file-date-format=
server.tomcat.basedir=.
The logger is getting printed in form of junk characters as follows:
"{"time": "???d???", "level": "8080", "correlation-id": "+{X-Correlation-Id}", "source": "-ogger{63}:???L???", "message": "POST /v1/plan/fetchPlans HTTP/1.1eplace(POST???w???Ex{6}){'[]+', '\n'}???n???opex", "source":"ACCESS_LOGS"}???n???"
Solution
Your pattern is invalid as it isn't in a form that's understood by Tomcat. It looks like you're trying to use Log4j2's pattern syntax. You need to use Tomcat's instead. You can learn more about the syntax and the pattern codes that are supported in Tomcat's documentation:
%a
- Remote IP address. See also%{xxx}a
below.%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 '-' (escaped if required)%U
- Requested URL path%v
- Local server name%D
- Time taken to process the request in millis. Note: In httpd%D
is microseconds. Behaviour will be aligned to httpd in Tomcat 10 onwards.%T
- Time taken to process the request, in seconds. Note: This value has millisecond resolution whereas in httpd it has second resolution. Behaviour will be align to httpd in Tomcat 10 onwards.%F
- Time taken to commit the response, in milliseconds%I
- Current request thread name (can compare later with stacktraces)%X
- Connection status when response is completed:
X
= Connection aborted before the response completed.+
= Connection may be kept alive after the response is sent.-
= Connection will be closed after the response is sent.There is also support to write information incoming or outgoing headers, cookies, session or request attributes and special timestamp formats. It is modeled after the Apache HTTP Server log configuration syntax. Each of them can be used multiple times with different xxx keys:
%{xxx}a
write remote address (client) (xxx==remote) or connection peer address (xxx=peer)%{xxx}i
write value of incoming header with name xxx (escaped if required)%{xxx}o
write value of outgoing header with name xxx (escaped if required)%{xxx}c
write value of cookie with name xxx (escaped if required)%{xxx}r
write value of ServletRequest attribute with name xxx (escaped if required)%{xxx}s
write value of HttpSession attribute with name xxx (escaped if required)%{xxx}p
write local (server) port (xxx==local) or remote (client) port (xxx=remote)%{xxx}t
write timestamp at the end of the request formatted using the enhanced SimpleDateFormat pattern xxx
Answered By - Andy Wilkinson
Answer Checked By - David Marino (JavaFixing Volunteer)