Spring Boot开启HTTP请求记录(访问日志)

40

如何在由 Spring Boot 提供的嵌入式 Tomcat 服务器中启用访问日志?我已经在application.properties中尝试过,但它既没有创建文件,也没有记录到控制台。

server.tomcat.access-log-enabled=true
server.tomcat.access-log-pattern=%a asdasd
logging.file=/home/mati/mylog.log

是的,那是我拼错了,但在我的项目中是正确的。我知道Spring会正确读取它,因为server.port=9900可以正常工作。我无法看到你指定的目录 :( - Mati
你能分享你的项目吗? - Dave Syer
很遗憾,这是我工作中的一个项目……也许需要在配置或pom.xml文件中做一些修改才能使其正常工作? - Mati
1
你的属性文件中标志的形式无所谓(连字符或驼峰命名都同样有效)。 - Dave Syer
1
在我撰写这个问题的时候,连字符并没有起作用。很高兴听到现在它已经可以使用了。 - Mati
显示剩余3条评论
5个回答

43

以下提供一种将它们显示在控制台或任何您选择的文件中的方法。在任何@Configuration类中声明Tomcat的RequestDumperFilter

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

这是输出结果:

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 ===============================================================

然后按照标准的Spring Boot日志管理。


33

尝试

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

查看 /tmp/tomcat.<随机数>.<端口号>/logs 目录以获取输出文件。设置 server.tomcat.basedir 属性以更改目录。


4
在Windows下,Tomcat会将日志文件创建在%TEMP%\tomcat.<randomID>.<portnumber>\logs目录下,而不是相对于user.dir目录。请注意,这并不影响用户使用。 - Henning
我认为这在所有平台上都是正确的(除非您明确设置了server.tomact.basedir),但当此答案首次创建时可能不是这样。 - Dave Syer
对于Spring Boot最近的版本:https://dev59.com/h2Ag5IYBdhLWcg3wq8eU#62002598 - veben

20
在Spring Boot 1.5.1中,Dave Syer提到的属性不再起作用,取而代之的是它们被重命名为:
server.tomcat.basedir=target/tomcat-logs
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)

使用以上配置,如果通过项目根目录运行该项目,则日志将可在target/tomcat-logs/log/access_log.*中找到。

14

如果你想管理访问日志,那么在你的application.yml文件中添加以下行:

使用Spring Boot 2.X版本。

server:
  tomcat:
    basedir: /home/tmp
    accesslog:
      enabled: true
它将在您定义的基目录中(此处为/home/tmp)创建名为logs的文件夹,其中包含访问日志文件。
如果您想在控制台中查看访问日志,可以这样做:
server:
  tomcat:
    accesslog:
      enabled: true
      directory: /dev
      prefix: stdout
      buffered: false
      suffix:
      file-date-format:

它将重定向日志到/dev/stdout

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


这是一个很棒的技巧,但它只适用于*nix系统。值得注意的是,在Windows上使用Eclipse时它不起作用,但在使用Docker Compose的Windows环境中(使用WSL)它可以正常工作。 - Garret Wilson

1
如果您有一个Spring Boot应用程序,并希望启用HTTP日志记录到stdout,这对于容器化应用程序可能是有用的,而无需修改任何代码或配置文件,您可以添加以下环境变量。
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=

注意,suffixfile-date-format应该设置为无。
然后重新启动您的应用程序,您应该得到日志记录。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接