[logback-dev] [JIRA] (LOGBACK-1016) logback-access: Enable MDC support
Paul Gross
pgross at gmail.com
Mon Nov 7 19:14:14 UTC 2016
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:
https://github.com/qos-ch/logback/pull/339
>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.
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.
Thanks,
Paul
On Mon, Nov 7, 2016 at 1:24 AM, Erik Lund Jensen (JIRA) <noreply-jira at qos.ch
> wrote:
> Erik Lund Jensen
> <http://jira.qos.ch/secure/ViewProfile.jspa?name=ErikLundJensen>
> *commented* on [image: New Feature] LOGBACK-1016
> <http://jira.qos.ch/browse/LOGBACK-1016>
>
> Re: logback-access: Enable MDC support
> <http://jira.qos.ch/browse/LOGBACK-1016>
>
> 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.
>
> In stead you may write a few classes for handling this.First, write a
> class for holding your variables. For example:
>
> public class AccessLogger {
> private static ThreadLocal<AdditionalInfo> currentInfo = new ThreadLocal<>();
>
> public static String getAndRemoveAdditionalInfo() {
> AdditionalInfo additionalInfo = currentInfo.get();
> currentInfo.remove();
> return additionalInfo.getInfo();
> }
> public static void setAdditionalInfo(String info){
> currentInfo.set(new AdditionalInfo(info));
> }
> public static final class AdditionalInfo {
> private String info;
> public AdditionalInfo(String info) {
> super();
> this.info = info;
> }
> public String getInfo() {
> return info;
> }
> }
> }
>
> Secondly, use that class in a custom access converter:
>
> public class AdditionalInfoConverter extends AccessConverter {
> @Override
> public String convert(IAccessEvent event) {
> String info = AccessLogger.getAndRemoveAdditionalInfo();
> return info!=null ? info : "-";
> }
> }
>
> 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.
>
> public class AccessLogLayoutWrapper extends PatternLayoutEncoderBase<IAccessEvent> {
> @Override
> public void start() {
> PatternLayout patternLayout = new PatternLayout();
> patternLayout.getDefaultConverterMap().put("X", AdditionalInfoConverter.class.getName());
> patternLayout.setContext(context);
> patternLayout.setPattern(getPattern());
> patternLayout.start();
> this.layout = patternLayout;
> super.start();
> }
> }
>
> Finally, use some filter to set the additional info in AccessLogger.
> ...
> AccessLogger.setAdditionalInfo( userName);
> [image: Add Comment] <http://jira.qos.ch/browse/LOGBACK-1016#add-comment> Add
> Comment <http://jira.qos.ch/browse/LOGBACK-1016#add-comment>
>
> This message was sent by Atlassian JIRA (v6.4.12#64027-sha1:e3691cc)
> [image: Atlassian logo]
>
> _______________________________________________
> logback-dev mailing list
> logback-dev at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-dev/attachments/20161107/6bee9d18/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 1008 bytes
Desc: not available
URL: <http://mailman.qos.ch/pipermail/logback-dev/attachments/20161107/6bee9d18/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 1084 bytes
Desc: not available
URL: <http://mailman.qos.ch/pipermail/logback-dev/attachments/20161107/6bee9d18/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 2983 bytes
Desc: not available
URL: <http://mailman.qos.ch/pipermail/logback-dev/attachments/20161107/6bee9d18/attachment-0005.png>
More information about the logback-dev
mailing list