How do I configure the location and name of tomcat access log in spring-boot?

13,609

Solution 1

You can use the EmbeddedServletContainerCustomizer interface to add a completely custom valve to your embedded tomcat. Here is what works for me:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container;
            AccessLogValve accessLogValve = new AccessLogValve();
            accessLogValve.setDirectory("/var/log/test");
            accessLogValve.setPattern("common");
            accessLogValve.setSuffix(".log");
            factory.addContextValves(accessLogValve);
        } else {
            logger.error("WARNING! this customizer does not support your configured container");
        }
    }

}

Solution 2

Configuration using application.yml (https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html):

server.tomcat.accesslog:
  # Enable access log:
  enabled: true

  # Directory in which log files are created. Can be relative to the tomcat base dir or absolute:
  directory: /var/log/test 

  # Format pattern for access logs:
  # https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Log_Valve
  pattern: "%h %l %u %t "%r" %s %b %D"

  # Log file name suffix:
  suffix: ".log"

Solution 3

If you are using application.yml for configuration.

You can refer this:

server:
  tomcat:
    basedir: tomcat/
    accesslog:
      enabled: true
      prefix: access-log
      suffix: .log
      # datetime remote-ip "request-referr" status (time-taken) 
      pattern: '%t %a "%r" %s %D'

You will generate a file with name like access-log.2018.08.22.log. And the log format will be

[22/Aug/2018:16:00:34 +0800] 0:0:0:0:0:0:0:1 "GET /search-query/video/123 HTTP/1.1" 200 666

In the example above, the logs will be available in tomcat/logs relative to the working directory of the application.

You can set this to change your log file name:

server:
  tomcat:
    accesslog:
      prefix: access
      file-date-format: .yyyy-MM-dd
      suffix: .log

Then the log format will be : access.2018-08-22.log

If you want to custom the log format, you can update the pattern

There are two internal pattern:

server.tomcat.accesslog.pattern=common

server.tomcat.accesslog.pattern=combined

Refer here for more info : https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Logging

Share:
13,609
Gwyn Jensen
Author by

Gwyn Jensen

Updated on July 17, 2022

Comments

  • Gwyn Jensen
    Gwyn Jensen almost 2 years

    I have a spring-boot app with the following configuration in application.yml

    server:
    contextPath: /rti
    tomcat:
        access-log-enabled: true
        access-log-pattern: "%h %l %u %t \"%r\" %s %b %D"
        basedir: tomcat
    

    This prompts the creation of an access log tomcat/logs/access_log.2015-02-12.txt.

    I would like to be able to configure where the access log is created and what it is named; but after much searching I am starting to think this isn't possible. Does any one know how to achieve this?

    Application logging is working fine using logback and configuration in logback.xml