Frage

I'm trying to change logback output from my code. I develop a web service and I would like to log some header information the service receive from the request.

I have this config :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE XML>
<configuration debug="true" scan="true" scanPeriod="1 hour">
  <!-- sends logs to logback-beagle -->
  <consolePlugin />
  <timestamp key="freqLogFile" datePattern="yyyyMMdd" />
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d [%thread] %level %class\(%method\) %line - %m%n</Pattern>
    </layout>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d [%thread] %level %class\(%method\) %line - %m%n</Pattern>
    </layout>
    <file>logs/webservice.logs_${freqLogFile}.log</file>
  </appender>
</configuration>

Is there a way to change the pattern to include these header information ?

War es hilfreich?

Lösung

You can put those headers into the MDC, then use them in the pattern.

In your service:

MDC.put("header1", header1);
MDC.put("header2", header2);

In your logback config:

<Pattern>%d [%thread] %level %class\(%method\) %line - %X{header1} %X{header2} %m%n</Pattern>

More info on the MDC and how to use it can be found in the documentation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top