gpt4 book ai didi

logback - 如何使用 logback.xml 记录特定的 HTTP header

转载 作者:行者123 更新时间:2023-12-03 23:56:22 26 4
gpt4 key购买 nike

我想创建一个控制台 appender 来显示一些日志信息,并打印出一个特定的 http header ,类似于:

> [INFO] { "time": "2017-08-31 12:14:32,583", "app-id": "my-app", "my-header": "my-header-value" } -- "Hello, World"

我创建了一个 logback-spring.xml文件如下,但“我的标题”只是打印出空白。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<springProperty name="appId" source="spring.app.application_id"/>

<!-- Appender to log to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- Minimum logging level to be presented in the console logs-->
<level>INFO</level>
</filter>
<encoder>
<pattern>
%clr(%5p) %clr({ "time": "%date{ISO8601}", "app-id": "${appId}", "my-header": "%X{my-header}"}){faint} -- %msg%n
</pattern>
<charset>utf8</charset>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="console"/>
</root>
</configuration>

我已经阅读了使用 logback-access使您可以访问 HTTP 请求/响应属性,但是当我尝试设置编码器类时,我无法使用任何经典的 logback 转换词:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<!-- Appender to log to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- Minimum logging level to be presented in the console logs-->
<level>INFO</level>
</filter>
<encoder class="ch.qos.logback.access.PatternLayoutEncoder">
<pattern class="ch.qos.logback.access.PatternLayoutEncoder">
%clr(%5p) %clr({ "time": "%date{ISO8601}", "app-id": "${appId}", "my-header": "%header{my-header}"}){faint} -- %msg%n
</pattern>
<charset>utf8</charset>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="console"/>
</root>
</configuration>

上面的 logback 给出了这些错误:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.0.RELEASE:run (default-cli) on project pd-thundera-server: An exception occurred while running. null: InvocationTargetException: Logback configuration error detected:
[ERROR] ERROR in ch.qos.logback.core.pattern.parser.Compiler@4782f0a4 - There is no conversion class registered for conversion word [p]
[ERROR] ERROR in ch.qos.logback.core.pattern.parser.Compiler@4782f0a4 - [p] is not a valid conversion word
[ERROR] ERROR in ch.qos.logback.core.pattern.parser.Compiler@6071227e - There is no conversion class registered for conversion word [msg]
[ERROR] ERROR in ch.qos.logback.core.pattern.parser.Compiler@6071227e - [msg] is not a valid conversion word

如何访问请求 header ?

最佳答案

这个配置对我有用:

    <encoder>
<charset>utf-8</charset>
<pattern>%t{yyyy-MM-dd HH:mm:ss,SSS} %h X-Forwarded-For: %header{X-Forwarded-For} "%r", Response status:%s, Bytes sent:%b, Response time:%D</pattern>
</encoder>

标题的名称在花括号之间,例如 %header{Content-type}%header{My-header} ,或只是 %header如果您希望记录所有标题。

来源:
  • https://logback.qos.ch/access.html#configuration
  • https://logback.qos.ch/manual/layouts.html#AccessPatternLayout
  • 关于logback - 如何使用 logback.xml 记录特定的 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45987464/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com