简体   繁体   English

Spring 引导启用 http 请求日志记录(访问日志)

[英]Spring Boot enable http requests logging (access logs)

How to enable access logs in an embedded tomcat server provided by spring boot ?如何在spring 引导提供的嵌入式tomcat服务器中启用访问日志? I've tried this in application.properties but it doesn't create file, neither logs to console.我已经在application.properties中尝试过,但它不会创建文件,也不会记录到控制台。

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

Here it goes a way to have them displayed in console or whatever file you choose.这是一种让它们显示在控制台或您选择的任何文件中的方法。 Declare Tomcat's RequestDumperFilter in any @Configuration class:在任何@Configuration类中声明 Tomcat 的RequestDumperFilter

@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.然后将其作为任何标准 Spring Boot 日志进行管理。

Try尝试

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

and look in /tmp/tomcat.<random>.<port>/logs for the output files.并在/tmp/tomcat.<random>.<port>/logs查找输出文件。 Set server.tomcat.basedir property to change the directory.设置server.tomcat.basedir属性以更改目录。

In Spring Boot 1.5.1 the properties mentioned by Dave Syer no longer works, instead they're renamed into:在 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)

Using the configuration above, if running the project via its root directory the log will be available at target/tomcat-logs/log/access_log.*使用上面的配置,如果通过其根目录运行项目,日志将在 target/tomcat-logs/log/access_log.* 中可用

With Spring Boot 2.X , if you want to manage Access logs , add these lines to your application.yml file:使用 Spring Boot 2.X ,如果您想管理访问日志,请将这些行添加到您的application.yml文件中:

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.它将在您定义的 basedir(此处为/home/tmp )中创建一个名为logs的文件夹,其中包含访问日志文件。

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它将日志重定向到/dev/stdout

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

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如果您有一个 Spring 启动应用程序,并希望启用 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=

Note suffix and file-date-format should be set to nothing注意suffixfile-date-format应该设置为空

Then restart your app and you should get logging然后重新启动您的应用程序,您应该可以进行日志记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM