Logging http request/response with separate timing

2019-05-10 09:31发布

问题:

I wanted to log http request and response to Neo4j server. I searched and got the answer on stackoverflow itself in below question: How to log query statement to Neo4j server, is it possible?

The configuration in the answer to above question results in same time logged for both request/response. I was wondering what that time represents ,i.e, when the server received the request or when it generated the response?

Also, I want to log the request and response with individual time for each(i.e, request with time when server received it and response with time when it was generated)? I tried to add a separate pattern for response but it didn't work:

<pattern>%h %l %user [%t{dd/MMM/yyyy:HH:mm:ss Z}] "%r" %s %b "%i{Referer}" "%i{User-Agent}" \nRequest:\n%fullRequest</pattern>
<pattern>%h %l %user [%t{dd/MMM/yyyy:HH:mm:ss Z}] "%r" %s %b "%i{Referer}" "%i{User-Agent}" \nResponse:\n%fullResponse</pattern>

Any ideas to achieve it?

Regards, Rahul

回答1:

You need to configure three different appenders generating three different patterns, don't know if it is possible to have multiple patterns for the same appender though.

make sure to add this line to your conf/neo4j-server.properties file

org.neo4j.server.http.unsafe.content_log.enabled=true

Here is a neo4j-http-logging.xml example for your needs

    <configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>data/log/http.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>data/log/http.%d{yyyy-MM-dd_HH}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <!-- Note the deliberate misspelling of "referer" in accordance with RFC1616 -->
      <pattern>%h %l %user [%t{dd/MMM/yyyy:HH:mm:ss Z}] "%r" %s %b "%i{Referer}" "%i{User-Agent}" %D</pattern>
    </encoder>
  </appender>

  <appender name="REQUEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>data/log/http.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>data/log/http.%d{yyyy-MM-dd_HH}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <!-- Note the deliberate misspelling of "referer" in accordance with RFC1616 -->
      <pattern>%h %l %user [%t{dd/MMM/yyyy:HH:mm:ss Z}] "%r" %s %b "%i{Referer}" "%i{User-Agent}" %D \n%fullRequest</pattern>
    </encoder>
  </appender>

  <appender name="RESPONSE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>data/log/http.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>data/log/http.%d{yyyy-MM-dd_HH}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <!-- Note the deliberate misspelling of "referer" in accordance with RFC1616 -->
      <pattern>%h %l %user [%t{dd/MMM/yyyy:HH:mm:ss Z}] "%r" %s %b "%i{Referer}" "%i{User-Agent}" %D \n%fullResponse</pattern>
    </encoder>
  </appender>

  <appender-ref ref="FILE"/>
  <appender-ref ref="REQUEST"/>
  <appender-ref ref="RESPONSE"/>
</configuration>

Restart your Neo4j server