25

I enabled tomcat access logs as per the spring boot reference documentation. But its not working properly. When I enabled it, access log file got created and I can see requests being logged there for that day. But at the start of the next day I don't see any new file. It started logging at 21hrs. Third day it started logging from 02hrs. From 4th day no access logs created.

Here are the properties that I used.

server.tomcat.access-log-enabled=true
server.tomcat.access-log-pattern=%h %l %u %t "%r" %s %b %D
server.tomcat.basedir=/var/lib/org_name/tracking_server 

under tracking_server folder logs and work folder got created.

Please let me know if I'm missing something. Regular logging is working perfectly according to the configuration specified in logback.xml

Thanks for your help in advance.

user3626166
  • 311
  • 2
  • 4
  • 5
  • Works for me. See also [here](https://github.com/spring-io/sagan/blob/master/sagan-site/src/main/resources/application.yml#L93) for an app that runs 24x7 and as far as I know always has access logs. Maybe no-one used yours in the times it wasn't logging? – Dave Syer May 11 '14 at 20:59
  • 1
    From 4th day onwards the access logs are going into /tmp/tomcat..8081/logs folder. 8081 is our management port. – user3626166 May 12 '14 at 07:16
  • one more observation is access logs between 00 to 21hrs on 2day and access logs between 00hrs to 02hrs on 3rd day is in /tmp/tomcat..8081 folder. I don't understand why spring is logging requests under management port folder. – user3626166 May 12 '14 at 07:30
  • The "/tmp/*" folder is the default value of the basedir, so I guess that's not difficult to understand. What do you mean by "management"? Is it the Actuator (i.e. you set "management.port=8081" somewhere)? BTW it's not Spring logging anything in these files, it's Tomcat. – Dave Syer May 12 '14 at 07:37
  • Yes, I set management.port=8081. I have overridden basedir value in the application.properties file. But why is it still going to "/tmp" folder. And also why some of the access logs are in overriden directory and some are in "/tmp/*" directory. – user3626166 May 12 '14 at 08:45
  • I can't explain all the data yet - that's why I'm still asking questions. I would expect a "tmp/*/work" directory for the management server, but not an access log there. Can you share a project that exhibits this behaviour? – Dave Syer May 12 '14 at 09:50
  • +1. The problem can be reproduced as simply as to have application.properties, server.port=8080 and management.port=7081, i.e. setting different ports. My logs went into the /tmp/xxxx.7081 directory. Worse still, no access log entries are logged after the few days. Just for your info, I also changed server.tomcat.basedir=/opt/mycompany. Do you want me to file a bug? – simonso Aug 27 '14 at 18:25
  • 1
    Hi, would you be able to fix this one? The problem is that when the management port is different, a totally different tomcat is created with a totally different web app context. And that webapp context has no knowledge of the base path. I don't think it's a tomcat problem - it's more of EndpointWebMvCAutoConfiguration.createChildManagementContext() should have picked up either a management. specific basePath or just use the basePath of the original webapp. Need a clear, defined path for logrotate the access log. So please help. – simonso Sep 18 '14 at 06:20
  • https://jira.spring.io/browse/SPR-12220 – simonso Sep 18 '14 at 08:15

1 Answers1

21

That configuration works for me in spring boot version 1.2.3.RELEASE. However, if you have the current version of spring boot, those parameters are a little bit different, reference here:

server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.

As you note, the difference is the hyphen (-).

Additionally, tomcat access log configuration include the following parameters, according to this document:

  • fileDateFormat. Default value is yyyy-MM-dd. It means that the log file is going to change daily. If you change for yyyy-MM-dd.HH, the log is going to change hourly.
  • rotatable. Default value is true. If you set to false, I understood that it is going to have just only file. it do not use fileDateFormat parameter.

However, in my version of spring boot (1.2.3.RELEASE) the class org.springframework.boot.autoconfigure.web.ServerProperties there are no values to change those properties (Tomcat subclass). But if you check org.apache.catalina.valves.AccessLogValve you could change this properties:

/**
 * Should we rotate our log file? Default is true (like old behavior)
 */
protected boolean rotatable = true;

/**
 * Date format to place in log file name.
 */
protected String fileDateFormat = ".yyyy-MM-dd";

I know maybe you should play with those parameters. I hope this post help to solve your problem.

jmgoyesc
  • 2,677
  • 1
  • 18
  • 16
  • 2
    Just to note, the upcoming 2.2.0.M1 release will introduce the additional field `server.tomcat.accesslog.max-days` for automatic deletion after x days. See [PR](https://github.com/spring-projects/spring-boot/pull/15954) – jasonoriordan Mar 06 '19 at 16:37
  • 3
    This was very useful, but I had trouble finding my access logs until I figured out the hard way that I should set basedir, see [here](https://stackoverflow.com/a/55505770/18573). – Miserable Variable Apr 03 '19 at 23:45