在独立的Spring Boot Web应用程序(可执行jar)中,如何告诉Spring Boot我们希望将嵌入式Tomcat实例的HTTP访问日志发送到stdout?
如果使用Logback,则可以使用logback-access。
添加依赖 ch.qos.logback:logback-access
ch.qos.logback:logback-access
可选的Javaconfig添加TeeFilter(请求和响应日志记录):
@Bean(name = "TeeFilter") public Filter teeFilter() { return new ch.qos.logback.access.servlet.TeeFilter(); }
嵌入式tomcat的Javaconfig:
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); // put logback-access.xml in src/main/resources/conf tomcat.addContextValves(new LogbackValve()); return tomcat; }
的内容logback-access.xml(保存在中src/main/resources/conf)
logback-access.xml
src/main/resources/conf
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>combined</Pattern> <Pattern>%fullRequest%n%n%fullResponse</Pattern> </encoder> </appender> <appender-ref ref="STDOUT" /> </configuration>