howto use the logback discriminator value to filte

2019-07-14 14:31发布

问题:

I have multiple threads generating log entries and I use the logback SiftingAppender to know who did what. Everything works fine and now im trying to save the log to mongodb.

In the mongodb the log needs to be saved into an embedded document array. Every user document have one embedded document that has an array of embedded documents containing log lines

Since im just started to learn logback this has to be some trial and error now.

In the below test logback.xml i have the file, consol and a custom appender.

My idea was that i could catch the SiftingAppender discriminator value in the custom appender append() method. Then the getMDCPropertyMap(); in the ILoggingEventgives me the MDC values but the question is if this is an efficient technic for what i want to do

I cannot see that logback has any native MDC-mongodb-appender or maybe i have missed something.

<configuration>

  <appender name="SIFT-FILE" class="ch.qos.logback.classic.sift.SiftingAppender">
    <!-- in the absence of the class attribute, it is assumed that the
         desired discriminator type is
         ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
    <discriminator>
      <key>userid</key>
      <defaultValue>unknown</defaultValue>
    </discriminator>
    <sift>
      <appender name="FILE-${userid}" class="ch.qos.logback.core.FileAppender">
        <file>${userid}.log</file>
        <append>true</append>
        <layout class="ch.qos.logback.classic.PatternLayout">
          <pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
        </layout>
      </appender>
    </sift>
  </appender>

  <appender name="SIFT-STDOUT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <!-- in the absence of the class attribute, it is assumed that the
         desired discriminator type is
         ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
    <discriminator>
      <key>userid</key>
      <defaultValue>unknown</defaultValue>
    </discriminator>
    <sift>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} ${userid}- %msg %n</pattern>
    </encoder>
  </appender>
    </sift>
  </appender>

  <appender name="SIFT-MONGO" class="ch.qos.logback.classic.sift.SiftingAppender">
    <!-- in the absence of the class attribute, it is assumed that the
         desired discriminator type is
         ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
    <discriminator>
      <key>userid</key>
      <defaultValue>unknown</defaultValue>
    </discriminator>
    <sift>
    <appender name="MONGO" class="com.carlsberg.MongoAppender">

    </appender>
    </sift>
  </appender>

  <root level="DEBUG">
     <appender-ref ref="SIFT-FILE" />
     <appender-ref ref="SIFT-STDOUT" />
     <appender-ref ref="SIFT-MONGO" />
  </root>
</configuration>

回答1:

I used the SiftingAppender discriminator value in the my custom appender like this.

public void append(ILoggingEvent event) {

    Map<String, String> m = event.getMDCPropertyMap();
    String id = (String) m.get("userid");

    if(id != null){

        Query<UserLog> query1 = mongo.createQuery(UserLog.class);   
        query1.field("lowerCaseUserName").equal(id.toLowerCase());  


        UpdateOperations<UserLog> up2 = mongo.createUpdateOperations
                (UserLog.class).add("log", event.getLevel().levelStr +" "+  ft.format(event.getTimeStamp()) +" "+ event.getFormattedMessage(), true);
        UpdateResults<UserLog> udr1 = mongo.update(query1, up2);

        if(udr1.getError() != null){
            System.out.print("ERROR CANNOT SAVE to UserLog ip adress for " + id);
        }
    }

}