Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:34:47+00:00 2026-06-07T04:34:47+00:00

I have multiple threads generating log entries and I use the logback SiftingAppender to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:34:49+00:00Added an answer on June 7, 2026 at 4:34 am

    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);
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple threads accessing variables. I know how to write spinlocks and use
I have multiple threads who all need to write to the same Dictionary. I
I have multiple threads that share use of a semaphore. Thread A holds the
I have an application which runs multiple threads. I use MadExcept to catch errors
I have a program that works with multiple threads. For each I set uncaught
I understand that Javascript doesn't have multiple threads, but I'd like to know if
I have multiple threads each one with its own private concurrent queue and all
I'm using Posix Message Queues on Linux. Basically, I have multiple threads that receive
The problem is this: I have multiple competing threads (100+) that need to access
I have a system that takes Samples. I have multiple client threads in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.