删除向导请求响应日志记录 [英] Drop-wizard request response logging
问题描述
我想将dropwizard中的每个请求和响应记录到不同的文件中。例如,我希望将所有请求记录在 /var/log/applicationname-request.log 中,并将所有响应记录到 /var/log/applicationname-response.log
I want to log each request and response in dropwizard into different files. For example I want all the requests to be logged in /var/log/applicationname-request.log and all responses into /var/log/applicationname-response.log
我们有什么方法可以实现相同的目标吗?
Is there any way in which we can achieve the same?
推荐答案
不幸的是DropWizard目前不支持这种原生。有关详细信息,请参见热烈讨论。
Unfortunately DropWizard currently doesn't support this natively. See this lively discussion for more information.
幸运的是,如果您愿意放弃使用DropWizard配置日志记录,则可以采用解决方法。在运行
方法的某个地方调用以下代码来重置日志记录并使用sfl4j的标准机制重新配置它:
Fortunately there's a workaround if you're willing to forego configuring logging with DropWizard all together. Somewhere in your run
method call the following code to reset the logging and reconfigure it using sfl4j's standard mechanism:
LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
context.reset();
ContextInitializer initializer = new ContextInitializer(context);
initializer.autoConfig();
然后你可以通过添加 logback.xml来配置日志记录
到你的类路径并配置如下:
Then you can configure logging by adding logback.xml
to your classpath and configuring it something like this:
<configuration>
<appender name="REQUEST" class="ch.qos.logback.core.FileAppender">
<file>/var/log/applicationname-request.log</file>
<append>false</append>
<encoder>
<pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="RESPONSE" class="ch.qos.logback.core.FileAppender">
<file>/var/log/applicationame-response.log</file>
<append>false</append>
<encoder>
<pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.example.request">
<level value="DEBUG" />
<appender-ref ref="REQUEST" />
</logger>
<logger name="org.example.response">
<level value="DEBUG" />
<appender-ref ref="RESPONSE" />
</logger>
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>
然后你可以在资源中获取相应的记录器并注销:
Then you can grab those corresponding loggers in your resource and log away:
static final Logger REQUEST_LOG = LoggerFactory.getLogger("org.example.request");
static final Logger RESPONSE_LOG = LoggerFactory.getLogger("org.example.response");
这篇关于删除向导请求响应日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!