问候,亲爱的朋友们!
今天,我想演示一个很好的例子,说明Bobina如何在非常常见的情况下提供帮助-在Spring Boot中记录HTTP请求和响应。
甚至更多! 我们只会将HTTP消息记录在单独的文件中。
所以走吧!
要在Spring Boot中激活HTTP日志记录,只需在application.properties
添加一行:
logging.level.org.springframework.web=TRACE
这样的配置仍将不允许记录HTTP请求的正文。
要激活HTTP请求主体的日志记录,请创建一个配置类:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CommonsRequestLoggingFilter; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Bean public CommonsRequestLoggingFilter logFilter() { CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); filter.setIncludeQueryString(true); filter.setIncludePayload(true); filter.setMaxPayloadLength(100000); filter.setIncludeHeaders(false); filter.setAfterMessagePrefix("REQUEST DATA : "); return filter; } }
现在,让我们设置Bobbin 。
将依赖项添加到build.gradle
:
compile "io.infinite:bobbin:2.0.4"
或在pom.xml
:
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.5.6</version> <type>pom</type> </dependency> <dependency> <groupId>io.infinite</groupId> <artifactId>bobbin</artifactId> <version>2.0.4</version> </dependency>
在资源目录或应用程序的工作目录中创建Bobbin.json
文件:
{ "levels": "['debug', 'info', 'warn', 'error'].contains(level)", "destinations": [ { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/THREADS/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('io.infinite.')" }, { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/ALL/WARNINGS_AND_ERRORS_${date}.log\"" }, "levels": "['warn', 'error'].contains(level)" }, { "name": "io.infinite.bobbin.destinations.ConsoleDestination", "levels": "['warn', 'error'].contains(level)" } ] }
“梭芯”的主要功能之一是,它可以根据诸如以下标准轻松地将日志记录输出分成单独的文件:
- 消息级别(
debug
, warn
等) - 流名称
- 类和包名
- 等
那么,为什么我们还要在包含其他日志的文件中寻找我们的HTTP日志呢?
为了方便起见,我们将Spring Boot HTTP日志定向到单独的文件中!
向Bobbin.json
添加一个新destination
:
{ "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/SPRING_WEB/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('org.springframework.web')" }
如您所见,我们将包含org.springframework.web
类的日志以其包的名称写入"SPRING_WEB"
目录中!
容易吧?
这是Bobbin.json
的完整配置, Bobbin.json
所示:
{ "levels": "['debug', 'info', 'warn', 'error'].contains(level)", "destinations": [ { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/THREADS/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('io.infinite.')" }, { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/ALL/WARNINGS_AND_ERRORS_${date}.log\"" }, "levels": "['warn', 'error'].contains(level)" }, { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/SPRING_WEB/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('org.springframework.web')" }, { "name": "io.infinite.bobbin.destinations.ConsoleDestination", "levels": "['warn', 'error'].contains(level)" } ] }
这是日志文件本身的内容.\LOGS\SPRING_WEB\main\http-nio-8089-exec-1\debug\http-nio-8089-exec-1_debug_2019-03-22.log
: 链接到github.com,t .k。 文件行长
记录从未如此简单!
感谢您的关注!