0

I have my spring application, which is running on the Tomcat server, and I have an acsess_logfile, which is written logs that I need to use in java Spring Boot.
It looks like that:

IP - - [17/Feb/2023:17:27:20 +0400] "GET /web-services/112233.jpg HTTP/1.1"
IP - - [17/Feb/2023:17:27:29 +0400] "GET /web-services/ HTTP/1.1" 200 189
IP - - [17/Feb/2023:17:27:29 +0400] "GET /web-services/112233.jpg HTTP/1.1" 304

Here Tomcat writes logs, which are sent to visiting URL/SITE.

I need to print/read it into Java and get access to them.
Is there any possible way to do it?

My application.properties:

spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp

server.tomcat.accesslog.enabled= true

I try many ways to solve it, I try to inject AccessLogValve but it doesn't work:

@GetMapping(value = "/")
public String root(AccessLogValve acc) {
   // When I try to call AccessLogValve methods, it's always null
   return "index";
}

I add some properties in application.properties but it doesn't help:

server.tomcat.accesslog.directory= "/dev"
server.tomcat.accesslog.prefix= stdout
server.tomcat.accesslog.buffered= false
server.tomcat.accesslog.suffix=
server.tomcat.accesslog.file-date-format=
server.tomcat.accesslog.pattern= "[ACCESS] %{org.apache.catalina.AccessLog.RemoteAddr}r %l %t %D %F %B %S vcap_request_id:%{X-Vcap-Request-Id}i"

Please help me

1 Answers1

0

Your attempt contains extra spaces, the setting should without a space after the equals sign. Like this:

server.tomcat.accesslog.directory=/dev
server.tomcat.accesslog.prefix=stdout
server.tomcat.accesslog.buffered=false
server.tomcat.accesslog.suffix=
server.tomcat.accesslog.file-date-format=
server.tomcat.accesslog.pattern="[ACCESS] %{org.apache.catalina.AccessLog.RemoteAddr}r %l %t %D %F %B %S vcap_request_id:%{X-Vcap-Request-Id}i"

Note that I have also removed the quotes around /dev. Next, if this still is not working, redirect the logs to regular file like this:

server.tomcat.accesslog.directory=/tmp
server.tomcat.accesslog.buffered=false
server.tomcat.accesslog.pattern="[ACCESS] %{org.apache.catalina.AccessLog.RemoteAddr}r %l %t %D %F %B %S vcap_request_id:%{X-Vcap-Request-Id}i"

The resulting log file will be available under /tmp directory, as access_log.<date>.log

All of this assumes, you are deploying on Linux-like OS. If you are on Windows, you have to change the path to valid Windows path.

jnovacho
  • 2,825
  • 6
  • 27
  • 44