1

i have to generate logs for each "user" of our web application. For that, we used a SiftingAppender with a Discriminator.

    <appender name="SEPARATED" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>userid</key>
            <defaultValue>unknown</defaultValue>
        </discriminator>
        <sift>
            <appender name="MYAPPLI_${userid}_SEPARATED" class="ch.qos.logback.core.FileAppender">
                <file>${LOG}/MYAPPLI/MYAPPLI_${userid}_SEPARATED.log</file>
                <append>true</append>
                <encoder>
                    <pattern>&lt;%d{yyyy-MM-dd HH:mm:ss}&gt; | %.-1level | %msg %throwable{5}%n</pattern>                    
                </encoder>
            </appender>
        </sift>
    </appender>

We added a servlet web filter in order to add the user from session to the MDC, and to remove it after...

But, we have some problems. We have some cases (why?) where "userid" variable is undefined, so logs appears with this name : MYAPPLI_unknown_SEPARATED.log.

Is the MDC a singleton of the web application ? Or is it in a scope session ?

In reality, i have to generate 2 logs / user session. So i have a second SiftingAppender with the same discriminator, and a filter (Thresholdfilter) cause the second log should be the same with a different level of trace. Is it the good way to do that ?

Thank you.

MaDa
  • 10,511
  • 9
  • 46
  • 84
MychaL
  • 969
  • 4
  • 19
  • 37
  • The MDC is thread-local, so you're doing the right thing by setting and unsetting it in a filter. I suppose the "unknowns" could be for messages that were logged by something earlier in the filter chain, you could try using a `ServletRequestListener` instead of a filter, which would fire earlier in the request processing cycle. – Ian Roberts Oct 04 '12 at 20:27
  • ... the unknowns could also be anything logged from a background thread, if you're using Quartz or something similar in your webapp. – Ian Roberts Oct 04 '12 at 20:39
  • We are in a classic Hibernate/JPA/Spring web application. The unknowns are not in a background thread, just in a new page. In the main page, the log are ok ; in a new one (the servlet filter is working fine) we have some pbs. So, we will make some research and test to see if the pb is not Tomcat... Thank you anyway. – MychaL Oct 05 '12 at 13:00

1 Answers1

2

The "Nested Diagnostic Context" is meant for such a use case - you can stamp each log statement with an id to identity the user (like an IP address, username, etc)

for more detail you can find in previous post:

A different log for every user using Log4j

Community
  • 1
  • 1
Mdhar9e
  • 1,376
  • 4
  • 23
  • 46
  • NDC is for log4j. But i presume MDC is the same thing for logback. I made the same configuration than in documentation (using slf4j & loback). But my problem is in using the proper way to refresh the userid in MDC. For that, we made a servlet filter but some unknown user id appear. The only way to have a good behaviour seems to make a new class to overload all method of slf4j where we pass the userid. So for all methods (info, warn, debug...) we have to initialize the MDC. But it's heavy.... probably there is a way to have a correct behaviour. Maybe our problem is Tomcat (5.5) too ? – MychaL Oct 04 '12 at 09:48