Spring Boot启用http请求记录

时间:2014-04-27 15:42:25

标签: java http tomcat logging spring-boot

如何在spring boot提供的嵌入式tomcat服务器中启用http服务器日志?我已经在application.properties中尝试了这个,但它没有创建文件,也没有登录到控制台

#application.properties
server.tomcat.access-log-enabled=true
server.tomcat.access-log-pattern=%a asdasd
logging.file=/home/mati/mylog.log
spring boot 1.0.1.RELEASE

4 个答案:

答案 0 :(得分:36)

这里有一种方法可以将它们显示在控制台或您选择的任何文件中。在任何@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日志进行管理。

答案 1 :(得分:30)

尝试

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

并在/tmp/tomcat.<random>.<port>/logs中查找输出文件。设置server.tomcat.basedir属性以更改目录。

答案 2 :(得分:17)

在Spring Boot 1.5.1中,properties mentioned by 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中可用。*

答案 3 :(得分:1)

使用Spring Boot 2.X ,如果您要管理访问日志,请将这些行添加到您的application.yml文件中:

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

它将在您定义的基础目录(此处为logs)中创建一个名为/home/tmp的文件夹,其中包含访问日志文件。

如果要在控制台中拥有访问日志,请执行以下操作:

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