<div dir="ltr"><div><div><div>I'm not sure if I should be commenting directly in JIRA (or how to get an account), but I submitted a pull request for this:<br><br><a href="https://github.com/qos-ch/logback/pull/339">https://github.com/qos-ch/logback/pull/339</a><br><br>>The MDC is not available when writing the access log. The MDC variables 
are removed by the filter (post-processing) running way before the 
access log.<br><br></div>I don't quite understand because my PR worked when I pulled it into my dropwizard project, and access logs included the values I inserted in the MDC at the beginning of the request.<br><br></div>Thanks,<br></div>Paul<br><div><div><div><br><div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 7, 2016 at 1:24 AM, Erik Lund Jensen (JIRA) <span dir="ltr"><<a href="mailto:noreply-jira@qos.ch" target="_blank">noreply-jira@qos.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u> 
 
     
         
          
         
     
    <div class="gmail-m_-1055914225687737633jira" style="color:rgb(51,51,51);font-family:arial,sans-serif;font-size:14px;line-height:1.429"> 
        <table id="gmail-m_-1055914225687737633background-table" style="background-color:rgb(245,245,245);border-collapse:collapse" width="100%" cellspacing="0" cellpadding="0"> 
             
            <tbody><tr> 
                <td id="gmail-m_-1055914225687737633header-pattern-container" style="border-collapse:collapse;padding:10px 20px"> 
                    <table id="gmail-m_-1055914225687737633header-pattern" style="border-collapse:collapse" cellspacing="0" cellpadding="0" border="0"> 
                        <tbody><tr> 
                            <td id="gmail-m_-1055914225687737633header-avatar-image-container" style="padding:0px 8px 0px 0px;border-collapse:collapse;vertical-align:top;width:32px" valign="top"> <img id="gmail-m_-1055914225687737633header-avatar-image" class="gmail-m_-1055914225687737633image_fix" style="border-radius: 3px; vertical-align: top;" width="32" height="32" border="0"> 
                            </td> 
                            <td id="gmail-m_-1055914225687737633header-text-container" style="padding:0px;border-collapse:collapse;vertical-align:middle;font-family:arial,sans-serif;font-size:14px;line-height:20px" valign="middle"> <a class="gmail-m_-1055914225687737633user-hover" rel="ErikLundJensen" id="gmail-m_-1055914225687737633email_ErikLundJensen" href="http://jira.qos.ch/secure/ViewProfile.jspa?name=ErikLundJensen" style="color:rgb(59,115,175);text-decoration:none" target="_blank">Erik Lund Jensen</a> <b>commented</b> on <a href="http://jira.qos.ch/browse/LOGBACK-1016" style="color:rgb(59,115,175);text-decoration:none" target="_blank"><img src="cid:jira-generated-image-static-newfeature-ffb66ede-1d21-43c5-b068-3ba962c98cd9" alt="New Feature" width="16" height="16" border="0" align="absmiddle"> LOGBACK-1016</a> 
                            </td> 
                        </tr> 
                    </tbody></table> 
                </td> 
            </tr> 
            <tr> 
                <td id="gmail-m_-1055914225687737633email-content-container" style="border-collapse:collapse;padding:0px 20px"> 
                    <table id="gmail-m_-1055914225687737633email-content-table" style="border-spacing:0px;border-collapse:separate" width="100%" cellspacing="0" cellpadding="0" border="0"> 
                        <tbody><tr> 
                             
                            <td class="gmail-m_-1055914225687737633email-content-rounded-top gmail-m_-1055914225687737633mobile-expand" style="border-collapse:collapse;color:rgb(255,255,255);background-color:rgb(255,255,255);border-width:1px 1px 0px;border-style:solid solid none;border-color:rgb(204,204,204) rgb(204,204,204) -moz-use-text-color;border-top-right-radius:5px;border-top-left-radius:5px;height:10px;line-height:10px;padding:0px 15px 0px 16px">
                                 
                            </td> 
                        </tr> 
                        <tr> 
                            <td class="gmail-m_-1055914225687737633email-content-main gmail-m_-1055914225687737633mobile-expand" style="border-collapse:collapse;border-width:0px 1px;border-style:none solid;border-color:-moz-use-text-color rgb(204,204,204);padding:0px 15px 0px 16px;background-color:rgb(255,255,255)"> 
                                <table class="gmail-m_-1055914225687737633page-title-pattern" style="border-collapse:collapse" width="100%" cellspacing="0" cellpadding="0" border="0"> 
                                    <tbody><tr> 
                                        <td style="vertical-align:top;padding:0px 5px 0px 0px;border-collapse:collapse;font-size:20px;line-height:30px" class="gmail-m_-1055914225687737633page-title-pattern-header-container"> <span class="gmail-m_-1055914225687737633page-title-pattern-header" style="font-family:arial,sans-serif;padding:0px;font-size:20px;line-height:30px;vertical-align:middle"> <a href="http://jira.qos.ch/browse/LOGBACK-1016" style="color:rgb(59,115,175);text-decoration:none" target="_blank">Re: logback-access: Enable MDC support</a> </span> 
                                        </td> 
                                    </tr> 
                                </tbody></table> 
                            </td> 
                        </tr> 
                        <tr> 
                            <td id="gmail-m_-1055914225687737633text-paragraph-pattern-top" class="gmail-m_-1055914225687737633email-content-main gmail-m_-1055914225687737633mobile-expand gmail-m_-1055914225687737633comment-top-pattern" style="border-collapse:collapse;border-width:0px 1px medium;border-style:none solid;border-color:-moz-use-text-color rgb(204,204,204);padding:0px 15px 0px 16px;background-color:rgb(255,255,255)"> 
                                <table class="gmail-m_-1055914225687737633text-paragraph-pattern" style="border-collapse:collapse;font-family:arial,sans-serif;font-size:14px;line-height:20px" width="100%" cellspacing="0" cellpadding="0" border="0"> 
                                    <tbody><tr> 
                                        <td class="gmail-m_-1055914225687737633text-paragraph-pattern-container gmail-m_-1055914225687737633mobile-resize-text" style="border-collapse:collapse;padding:0px 0px 10px"> 
                                            <p style="margin:10px 0px 0px">The MDC is not available when writing the access log. The MDC variables are removed by the filter (post-processing) running way before the access log.It is not a solution just to not remove the MDC variables, as you wouldn't want the MDC variables to stay around for the next request.</p> 
                                            <p style="margin:10px 0px 0px">In stead you may write a few classes for handling this.First, write a class for holding your variables. For example:</p> 
                                            <div class="gmail-m_-1055914225687737633code gmail-m_-1055914225687737633panel" style="background:rgb(245,245,245) none repeat scroll 0% 0%;font-size:12px;line-height:1.333;font-family:monospace;border-width:1px;border-style:solid;border-color:rgb(204,204,204);border-radius:3px;margin:9px 0px">
                                                <div class="gmail-m_-1055914225687737633codeContent gmail-m_-1055914225687737633panelContent" style="padding:9px 12px"> 
                                                    <pre class="gmail-m_-1055914225687737633code-java" style="margin:10px 0px 0px;max-height:30em;overflow:auto;white-space:pre-wrap"><span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> class AccessLogger {
    <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">private</span> <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">static</span> ThreadLocal<AdditionalInfo> currentInfo = <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">new</span> ThreadLocal<>();

    <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">static</span> <span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> getAndRemoveAdditionalInfo() {
        AdditionalInfo additionalInfo = currentInfo.get();
        currentInfo.remove();
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">return</span> additionalInfo.getInfo();
    }
    <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">static</span> void setAdditionalInfo(<span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> info){
        currentInfo.set(<span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">new</span> AdditionalInfo(info));
    }
    <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">static</span> <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">final</span> class AdditionalInfo {
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">private</span> <span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> info;
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> AdditionalInfo(<span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> info) {
            <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">super</span>();
            <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">this</span>.info = info;
        }
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> <span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> getInfo() {
            <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">return</span> info;
        }
    }
}
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin:10px 0px 0px">Secondly, use that class in a custom access converter:</p> 
                                            <div class="gmail-m_-1055914225687737633code gmail-m_-1055914225687737633panel" style="background:rgb(245,245,245) none repeat scroll 0% 0%;font-size:12px;line-height:1.333;font-family:monospace;border-width:1px;border-style:solid;border-color:rgb(204,204,204);border-radius:3px;margin:9px 0px">
                                                <div class="gmail-m_-1055914225687737633codeContent gmail-m_-1055914225687737633panelContent" style="padding:9px 12px"> 
                                                    <pre class="gmail-m_-1055914225687737633code-java" style="margin:10px 0px 0px;max-height:30em;overflow:auto;white-space:pre-wrap"><span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> class AdditionalInfoConverter <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">extends</span> AccessConverter  {
    @Override
    <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> <span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> convert(IAccessEvent event) {
        <span class="gmail-m_-1055914225687737633code-object" style="color:rgb(145,0,145)">String</span> info = AccessLogger.<wbr>getAndRemoveAdditionalInfo();
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">return</span> info!=<span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">null</span> ? info : <span class="gmail-m_-1055914225687737633code-quote" style="color:rgb(0,145,0)">"-"</span>;
    }
}
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin:10px 0px 0px">Thirdly, add the customer converter to a new wrapper class. This class adds %X to get the additional info.It would be nice if Logback supported a callback to extend PatterLayout, however, this is how it can be done without changing the Logback source code.</p> 
                                            <div class="gmail-m_-1055914225687737633code gmail-m_-1055914225687737633panel" style="background:rgb(245,245,245) none repeat scroll 0% 0%;font-size:12px;line-height:1.333;font-family:monospace;border-width:1px;border-style:solid;border-color:rgb(204,204,204);border-radius:3px;margin:9px 0px">
                                                <div class="gmail-m_-1055914225687737633codeContent gmail-m_-1055914225687737633panelContent" style="padding:9px 12px"> 
                                                    <pre class="gmail-m_-1055914225687737633code-java" style="margin:10px 0px 0px;max-height:30em;overflow:auto;white-space:pre-wrap"><span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> class AccessLogLayoutWrapper <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">extends</span> PatternLayoutEncoderBase<<wbr>IAccessEvent> {
    @Override
    <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">public</span> void start() {
        PatternLayout patternLayout = <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">new</span> PatternLayout();
        patternLayout.<wbr>getDefaultConverterMap().put(<span class="gmail-m_-1055914225687737633code-quote" style="color:rgb(0,145,0)">"<wbr>X"</span>, AdditionalInfoConverter.class.<wbr>getName());
        patternLayout.setContext(<wbr>context);
        patternLayout.setPattern(<wbr>getPattern());
        patternLayout.start();
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">this</span>.layout = patternLayout;
        <span class="gmail-m_-1055914225687737633code-keyword" style="color:rgb(0,0,145)">super</span>.start();
    }
}
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin:10px 0px 0px">Finally, use some filter to set the additional info in AccessLogger.<br> ...<br> AccessLogger.<wbr>setAdditionalInfo( userName);</p> 
                                        </td> 
                                    </tr> 
                                </tbody></table> 
                            </td> 
                        </tr> 
                        <tr> 
                            <td class="gmail-m_-1055914225687737633email-content-main gmail-m_-1055914225687737633mobile-expand" style="border-collapse:collapse;border-width:0px 1px;border-style:none solid;border-color:-moz-use-text-color rgb(204,204,204);padding:0px 15px 0px 16px;background-color:rgb(255,255,255)"> 
                                <table id="gmail-m_-1055914225687737633actions-pattern" style="border-collapse:collapse;font-family:arial,sans-serif;font-size:14px;line-height:20px" width="100%" cellspacing="0" cellpadding="0" border="0"> 
                                    <tbody><tr> 
                                        <td id="gmail-m_-1055914225687737633actions-pattern-container" style="border-collapse:collapse;padding:10px 0px;vertical-align:middle" valign="middle"> 
                                            <table style="border-collapse:collapse" align="left"> 
                                                <tbody><tr> 
                                                    <td class="gmail-m_-1055914225687737633actions-pattern-action-icon-container" style="padding:0px;border-collapse:collapse;font-family:arial,sans-serif;font-size:14px;line-height:20px;vertical-align:middle"> <a href="http://jira.qos.ch/browse/LOGBACK-1016#add-comment" title="Add Comment" style="color:rgb(59,115,175);text-decoration:none" target="_blank"> <img class="gmail-m_-1055914225687737633actions-pattern-action-icon-image" src="cid:jira-generated-image-static-comment-icon-bb680da2-0b36-4ceb-ad9e-dff0eb505926" alt="Add Comment" title="Add Comment" style="vertical-align: middle;" width="16" height="16" border="0"> </a> 
                                                    </td> 
                                                    <td class="gmail-m_-1055914225687737633actions-pattern-action-text-container" style="padding:0px 0px 0px 5px;border-collapse:collapse;font-family:arial,sans-serif;font-size:14px;line-height:20px"> <a href="http://jira.qos.ch/browse/LOGBACK-1016#add-comment" title="Add Comment" style="color:rgb(59,115,175);text-decoration:none" target="_blank">Add Comment</a> 
                                                    </td> 
                                                </tr> 
                                            </tbody></table> 
                                        </td> 
                                    </tr> 
                                </tbody></table> 
                            </td> 
                        </tr> 
                         
                        <tr> 
                            <td class="gmail-m_-1055914225687737633email-content-rounded-bottom gmail-m_-1055914225687737633mobile-expand" style="border-collapse:collapse;color:rgb(255,255,255);padding:0px 15px 0px 16px;height:5px;line-height:5px;background-color:rgb(255,255,255);border-width:0px 1px 1px;border-style:none solid solid;border-color:-moz-use-text-color rgb(204,204,204) rgb(204,204,204);border-bottom-right-radius:5px;border-bottom-left-radius:5px">
                                 
                            </td> 
                        </tr> 
                    </tbody></table> 
                </td> 
            </tr> 
            <tr> 
                <td id="gmail-m_-1055914225687737633footer-pattern" style="border-collapse:collapse;padding:12px 20px"> 
                    <table id="gmail-m_-1055914225687737633footer-pattern-container" style="border-collapse:collapse" cellspacing="0" cellpadding="0" border="0"> 
                        <tbody><tr> 
                            <td id="gmail-m_-1055914225687737633footer-pattern-text" class="gmail-m_-1055914225687737633mobile-resize-text" style="padding:0px;border-collapse:collapse;color:rgb(153,153,153);font-size:12px;line-height:18px;font-family:arial,sans-serif" width="100%">
                                 This message was sent by Atlassian JIRA <span id="gmail-m_-1055914225687737633footer-build-information">(v6.4.12#64027-<span title="e3691cc1283c0f3cef6d65d3ea82d47743692b57">sha1:e3691cc</span>)</span> 
                            </td> 
                            <td id="gmail-m_-1055914225687737633footer-pattern-logo-desktop-container" style="padding:0px 0px 0px 20px;border-collapse:collapse;vertical-align:top" valign="top"> 
                                <table style="border-collapse:collapse"> 
                                    <tbody><tr> 
                                        <td id="gmail-m_-1055914225687737633footer-pattern-logo-desktop-padding" style="padding:3px 0px 0px;border-collapse:collapse"> <img id="gmail-m_-1055914225687737633footer-pattern-logo-desktop" src="cid:jira-generated-image-static-footer-desktop-logo-8e6a963e-a870-48fe-9958-bfecdd5fcf91" alt="Atlassian logo" title="Atlassian logo" class="gmail-m_-1055914225687737633image_fix" width="169" height="36"> 
                                        </td> 
                                    </tr> 
                                </tbody></table> 
                            </td> 
                        </tr> 
                    </tbody></table> 
                </td> 
            </tr> 
        </tbody></table>   
    </div>
<br>______________________________<wbr>_________________<br>
logback-dev mailing list<br>
<a href="mailto:logback-dev@qos.ch">logback-dev@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-dev" rel="noreferrer" target="_blank">http://mailman.qos.ch/mailman/<wbr>listinfo/logback-dev</a><br></blockquote></div><br></div></div></div></div></div></div>