40

How to enable access logs in an embedded tomcat server provided by spring boot? I've tried this in application.properties but it doesn't create file, neither logs to console.

server.tomcat.access-log-enabled=true
server.tomcat.access-log-pattern=%a asdasd
logging.file=/home/mati/mylog.log
8
  • I assume you meant "application.properties"? The spelling error in the file name is the only obvious error I can see. Note that the logging.file has no effect on the tomcat access log though (although the file ought to be created if you are using a "normal" project setup). Normally the Tomcat valve creates logs in "./logs" (i.e. relative to user.dir).
    – Dave Syer
    Apr 27, 2014 at 15:51
  • Yes, that was my misspell, but in my project it is correct. I know that Spring reads it correctly, because server.port=9900 works properly. I can't see directory specified by you :(
    – Mati
    Apr 27, 2014 at 16:05
  • Can you share your project?
    – Dave Syer
    Apr 27, 2014 at 16:11
  • 1
    The form of the flag in your properties file makes no different (hyphens or camel case work equally well).
    – Dave Syer
    Jun 8, 2015 at 9:29
  • 1
    At the time I have written this questions, hyphens just didn't work. I'm glad to hear that now it works.
    – Mati
    Jun 8, 2015 at 9:38

5 Answers 5

44

Here it goes a way to have them displayed in console or whatever file you choose. Declare Tomcat's RequestDumperFilter in any @Configuration class:

@Bean
public FilterRegistrationBean requestDumperFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    Filter requestDumperFilter = new RequestDumperFilter();
    registration.setFilter(requestDumperFilter);
    registration.addUrlPatterns("/*");
    return registration;
}

And that's the output:

http-nio-8765-exec-1 START TIME        =30-may-2016 12:45:41
http-nio-8765-exec-1         requestURI=/info
http-nio-8765-exec-1           authType=null
http-nio-8765-exec-1  characterEncoding=UTF-8
http-nio-8765-exec-1      contentLength=-1
http-nio-8765-exec-1        contentType=null
http-nio-8765-exec-1        contextPath=
http-nio-8765-exec-1             cookie=JSESSIONID=E7259F5F9ED6B04CBE5A294C5F8CA5C6
http-nio-8765-exec-1             header=host=mies-057:8765
http-nio-8765-exec-1             header=connection=keep-alive
http-nio-8765-exec-1             header=accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
http-nio-8765-exec-1             header=upgrade-insecure-requests=1
http-nio-8765-exec-1             header=user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
http-nio-8765-exec-1             header=referer=http://mies-057:1111/
http-nio-8765-exec-1             header=accept-encoding=gzip, deflate, sdch
http-nio-8765-exec-1             header=accept-language=es-ES,es;q=0.8
http-nio-8765-exec-1             header=cookie=JSESSIONID=E7259F5F9ED6B04CBE5A294C5F8CA5C6
http-nio-8765-exec-1             locale=es_ES
http-nio-8765-exec-1             method=GET
http-nio-8765-exec-1           pathInfo=null
http-nio-8765-exec-1           protocol=HTTP/1.1
http-nio-8765-exec-1        queryString=null
http-nio-8765-exec-1         remoteAddr=192.168.56.1
http-nio-8765-exec-1         remoteHost=192.168.56.1
http-nio-8765-exec-1         remoteUser=null
http-nio-8765-exec-1 requestedSessionId=E7259F5F9ED6B04CBE5A294C5F8CA5C6
http-nio-8765-exec-1             scheme=http
http-nio-8765-exec-1         serverName=mies-057
http-nio-8765-exec-1         serverPort=8765
http-nio-8765-exec-1        servletPath=/info
http-nio-8765-exec-1           isSecure=false
http-nio-8765-exec-1 ------------------=--------------------------------------------
http-nio-8765-exec-1 ------------------=--------------------------------------------
http-nio-8765-exec-1           authType=null
http-nio-8765-exec-1        contentType=application/json;charset=UTF-8
http-nio-8765-exec-1             header=Strict-Transport-Security=max-age=31536000 ; includeSubDomains
http-nio-8765-exec-1             header=X-Application-Context=EDGE:8765
http-nio-8765-exec-1             header=Content-Type=application/json;charset=UTF-8
http-nio-8765-exec-1             header=Transfer-Encoding=chunked
http-nio-8765-exec-1             header=Date=Mon, 30 May 2016 10:45:41 GMT
http-nio-8765-exec-1             status=200
http-nio-8765-exec-1 END TIME          =30-may-2016 12:45:41
http-nio-8765-exec-1 ===============================================================

Then manage it as any standard Spring Boot log.

0
33

Try

server.tomcat.accessLogEnabled=true
server.tomcat.accessLogPattern=%a asdasd

and look in /tmp/tomcat.<random>.<port>/logs for the output files. Set server.tomcat.basedir property to change the directory.

3
  • 4
    Under Windows, Tomcat creates the log files under %TEMP%\tomcat.<randomID>.<portnumber>\logs instead. It's not relative to user.dir
    – Henning
    Jun 8, 2015 at 1:49
  • I think that's true on all platforms (unless you set server.tomact.basedir explicitly), but it probably wasn't when this answer was first created.
    – Dave Syer
    Jun 8, 2015 at 9:27
  • For Spring boot recent versions: stackoverflow.com/a/62002598/8718377
    – veben
    May 25, 2020 at 12:49
20

In Spring Boot 1.5.1 the properties mentioned by Dave Syer no longer works, instead they're renamed into:

server.tomcat.basedir=target/tomcat-logs
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)

Using the configuration above, if running the project via its root directory the log will be available at target/tomcat-logs/log/access_log.*

14

With Spring Boot 2.X, if you want to manage Access logs, add these lines to your application.yml file:

server:
  tomcat:
    basedir: /home/tmp
    accesslog:
      enabled: true

It will create a folder named logs in the basedir you defined (/home/tmp here) containing the access log files.

If you want to have access logs in the console do like that:

server:
  tomcat:
    accesslog:
      enabled: true
      directory: /dev
      prefix: stdout
      buffered: false
      suffix:
      file-date-format:

It will rediect logs to /dev/stdout

More informations: https://community.pivotal.io/s/article/how-to-configure-access-log-entries-for-a-spring-boot-app?language=en_US

1
  • That's a neat trick, but it only works on *nix machines. Notably it does not work when running in Eclipse on Windows, but does work in Docker Compose on Windows (which uses WSL). Aug 26, 2023 at 20:36
1

If you have a Spring Boot app, and would like to enable http logging to stdout, as might be useful in a containerised application, without modifying and code or config files you can add the following environment variables

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=/dev
server.tomcat.accesslog.prefix=stdout
server.tomcat.accesslog.suffix=
server.tomcat.accesslog.file-date-format=

Note suffix and file-date-format should be set to nothing

Then restart your app and you should get logging

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.