Logback : using different patterns for different markers

2k views Asked by At

The software I'm working on uses MDCs for specific data (username, ip, execution time and so on). I want some logs to use a specific pattern using some MDCs, and some logs to use another pattern using other MDCs.

I thought that markers would do the job, and I set up different markers, let say : PROFILE_MARKER - used for profile logs AUDIT_MARKER - used for audit logs

I have 3 appenders in my logback.xml config file, one for both my markers, and one for all the other logs :

<configuration>
  <appender name="profiler" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d [%t] [%X{USER}] [PROFILER] %-5p %m %X{TIME} ms %n</pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>PROFILER</marker>
      </evaluator>
      <onMismatch>DENY</onMismatch>
      <onMatch>ACCEPT</onMatch>
    </filter>
  </appender>

  <appender name="audit" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d [%t] [%X{USER}] [AUDIT] %-5p IP=%X{IP},ID=%X{ID} %m %n</pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>AUDIT</marker>
      </evaluator>
      <onMismatch>DENY</onMismatch>
      <onMatch>ACCEPT</onMatch>
    </filter>
  </appender> 

  <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d [%t] [%X{USER}] %-5p %c - %m %n</pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>AUDIT</marker>
        <marker>PROFILE</marker>
      </evaluator>
      <onMismatch>ACCEPT</onMismatch>
      <onMatch>DENY</onMatch>
    </filter>
  </appender>
  <root level="INFO">
    <appender-ref ref="audit" />
    <appender-ref ref="profiler" />
    <appender-ref ref="stdout" />
  </root>

I have the feeling I am doing this wrong, and that I should use only one appender, and maybe use filters differently. However I can't figure out how. Maybe markers aren't the solution here ?

Any help would be greatly appreciated.

0

There are 0 answers