Springboot项目开启Tomcat的AccessLog,并打印指定的Header内容

"本文介绍了如何在SpringBoot项目中开启Tomcat的AccessLog,通过设置`server.tomcat.accesslog.enabled=true`等配置参数来定制日志样式,例如 `%{xxx}
摘要由CSDN通过智能技术生成

Springboot项目开启Tomcat的AccessLog。设置AccessLog样式,并打印指定的Header内容

SpringBoot项目开启Tomcat的AccessLog

server.tomcat.basedir=my-tomcat
server.tomcat.accesslog.enabled=true

指定打印Header

server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)  %{xxx}i

%{xxx}i 就是打印指定Header的写法,其中xxx 就是header的name

SpringBoot配置文件参考示例

server.tomcat.accesslog.buffered=true # 缓冲输出,使其只定期刷新。
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # 要放在日志文件名中的日期格式。rotate为false的时候,这个也就不生效了。
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for IP address, Hostname, protocol and port used for the request.
server.tomcat.accesslog.rotate=true # Enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.

常用的样式参数

  • %a - Remote IP address,远程ip地址,注意不一定是原始ip地址,中间可能经过nginx等的转发
  • %A - Local IP address,本地ip
  • %b - Bytes sent, excluding HTTP headers, or ‘-’ if no bytes were sent
  • %B - Bytes sent, excluding HTTP headers
  • %h - Remote host name (or IP address if enableLookups for the connector is false),远程主机名称(如果resolveHosts为false则展示IP)
  • %H - Request protocol,请求协议
  • %l - Remote logical username from identd (always returns ‘-’)
  • %m - Request method,请求方法(GET,POST)
  • %p - Local port,接受请求的本地端口
  • %q - Query string (prepended with a ‘?’ if it exists, otherwise an empty string
  • %r - First line of the request,HTTP请求的第一行(包括请求方法,请求的URI)
  • %s - HTTP status code of the response,HTTP的响应代码,如:200,404
  • %S - User session ID
  • %t - Date and time, in Common Log Format format,日期和时间,Common Log Format格式
  • %u - Remote user that was authenticated
  • %U - Requested URL path
  • %v - Local server name
  • %D - Time taken to process the request, in millis,处理请求的时间,单位毫秒
  • %T - Time taken to process the request, in seconds,处理请求的时间,单位秒
  • %I - current Request thread name (can compare later with stacktraces),当前请求的线程名,可以和打印的log对比查找问题
Access log 也支持将cookie、header、session或者其他在ServletRequest中的对象信息打印到日志中,其配置遵循Apache配置的格式({xxx}指值的名称)
  • %{xxx}i for incoming headers,request header信息
  • %{xxx}o for outgoing response headers,response header信息
  • %{xxx}c for a specific cookie
  • %{xxx}r xxx is an attribute in the ServletRequest
  • %{xxx}s xxx is an attribute in the HttpSession
  • %{xxx}t xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)

配置参考:https://blog.csdn.net/u013732378/article/details/100151205

### 回答1: Spring Boot项目配置Tomcat可以通过以下步骤实现: 1. 在pom.xml文件中添加Tomcat依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ``` 2. 在application.properties文件中配置Tomcat端口号: ``` server.port=808 ``` 3. 在启动类中添加@EnableAutoConfiguration注解: ``` @SpringBootApplication @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 打包项目并运行: ``` mvn clean package java -jar target/myproject.jar ``` 以上就是Spring Boot项目配置Tomcat的简单步骤。 ### 回答2: Spring Boot 是一个基于 Spring 框架的快速开发框架,能够非常简便地创建独立的、生产级别的、基于 Spring 的应用程序。当需要将 Spring Boot 应用程序部署到生产环境中时,我们可以将其打包成 war 包或 jar 包,再配置 Tomcat 作为应用程序的 Web 服务器。 首先,在 pom.xml 文件中引入 Spring Boot 的 web 运行时依赖项,即: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 然后,在应用程序的主类上添加注解 `@EnableAutoConfiguration` 和 `@SpringBootApplication`。如下: ``` @SpringBootApplication @EnableAutoConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 接着,我们可以在 `application.properties` 或 `application.yml` 中进行相关配置,例如: ``` server.port=8081 server.context-path=/myapp ``` 以上配置将 Tomcat 服务器端口号设置为 8081,同时将应用程序部署到 http://localhost:8081/myapp。 最后,使用 Maven 命令将应用程序打包成 war 包,例如: ``` mvn clean package ``` 打包完成后,将 war 包复制到 Tomcat 服务器的 webapps 目录下,并启动 Tomcat 即完成项目的部署。 总结:Spring Boot 配置 Tomcat 可以通过引入 web 运行时依赖项、添加启动注解、配置服务器相关属性等操作,最终通过 Maven 命令打包成 war 包部署到 Tomcat 服务器中。 ### 回答3: Spring Boot是一个非常流行的Java Web应用程序框架,它简化了开发者在创建和部署Web应用程序时所需的步骤。Spring Boot支持使用嵌入式web服务器(如Tomcat,Jetty,Undertow等)运行应用程序,也支持将应用程序打成war包并在外部web服务器上运行。本文将详细介绍如何在Spring Boot项目中配置Tomcat服务器。 1.添加Tomcat依赖 在Spring Boot项目的pom.xml文件中添加Tomcat依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ``` 2.配置Tomcat连接器 在application.properties文件中配置Tomcat连接器: ``` server.port=8080 server.tomcat.max-connections=200 server.tomcat.max-threads=100 server.tomcat.uri-encoding=UTF-8 ``` 上述配置中,我们指定Tomcat服务器的端口、最大连接数、最大线程数、编码方式。 3.构建war包 如果想要将应用程序部署到外部Tomcat服务器上,则需要将应用程序打成war包。在pom.xml文件中添加如下配置: ``` <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 上述配置中,我们指定了打包方式为war,并添加了spring-boot-maven-plugin插件来打包应用程序。 4.部署war包 将打包好的war包复制到外部tomcat服务器的webapps目录下,并启动Tomcat服务器,即可部署应用程序。 最后,通过以上步骤我们已经成功配置了Tomcat服务器并将应用程序部署到了外部Tomcat服务器上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值