如何使用logback鉴别器值来过滤mongodb插入

时间:2012-02-22 22:25:24

标签: mongodb logback

我有多个线程生成日志条目,我使用logback SiftingAppender来了解谁做了什么。一切正常,现在我试图将日志保存到mongodb。

在mongodb中,日志需要保存到嵌入式文档数组中。每个用户文档都有一个嵌入式文档,其中包含一个包含日志行的嵌入文档数组

由于我刚开始学习回溯,现在必须进行一些反复试验。

在下面的测试logback.xml中,我有fileconsol和自定义appender。

我的想法是我可以在自定义appender discriminator value方法中捕获SiftingAppender append()。然后是getMDCPropertyMap();在ILoggingEvent中给出了MDC值,但问题是这是否是我想要做的有效技术

我看不到logback有任何本机MDC-mongodb-appender或者我错过了什么。

<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 个答案:

答案 0 :(得分:1)

我在我的自定义appender中使用了SiftingAppender discriminator值。

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);
        }
    }

}