如何使用logback.xml记录特定的HTTP标头

时间:2017-08-31 17:53:54

标签: logback

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

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

我创建了一个logback-spring.xml文件,如下所示,但“my-header”只打印出空白。

<?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

如何访问请求标头?

2 个答案:

答案 0 :(得分:0)

此配置对我有用:

    <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

来源:

答案 1 :(得分:0)

请在下面找到 Spring 的例子

如果您想记录 http 标头,则需要使用 MDC 功能:http://logback.qos.ch/manual/mdc.html

创建过滤器:

@Component
class RequestHeaderFilterConfig : Filter {

    private val xRequestId = "X-Request-Id"

    override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain?) {
        val httpRequest = request as HttpServletRequest
        MDC.put(xRequestId, httpRequest.getHeader(xRequestId))
        chain?.doFilter(request, response)
    }

    override fun destroy() = MDC.remove(xRequestId)
}

控制器:

@PostMapping
    fun operation(@RequestHeader(value = "X-Request-Id", required = false) xRequestId: String? = null): ResponseEntity<Output> {
        ....
    }
    

logback.xml

<Pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level [X-Request-Id: %X{X-Request-Id}] %logger{36}.%M:%line - %msg%n</Pattern>