ankit.vishen
ankit.vishen

Reputation: 1170

Best way of logging REST API response time in spring + tomcat

I'm using spring mvc with tomcat servlet container for web services and want to log response time(total execution time) of APIs, so is there any spring service or what is the best way to log such information related to api execution time at spring controller level.

Upvotes: 2

Views: 3054

Answers (1)

ankit.vishen
ankit.vishen

Reputation: 1170

as the number of views is increasing and there is no proper answer of this post so I thought to answer my finding. So there are two ways if you are using Spring and tomcat.

Method 1#

Implement an interceptor and add current time in a new parameter(requestReceivingTime) in request preHandle block and log the subtraction of current time and requestReceivingTime in postHandle block.

Method 2#

You can use tomcat access log to log the request time, you have to edit your conf/server.xml and change the format according to your need. for exp

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%{X-Forwarded-For}i , %t Bytes sent: [%B] , Time taken millis: [%D] , Time taken seconds: [%T]  &quot;%r&quot; %s" />

Note : Pattern could be change in your case, it depends on tomcat version.

Hope this will help !

Upvotes: 1

Related Questions