[logback-dev] [JIRA] Created: (LBCLASSIC-126) NDC implementation

Anders Wallgren (JIRA) noreply-jira at qos.ch
Fri May 8 23:54:10 CEST 2009

NDC implementation

                 Key: LBCLASSIC-126
                 URL: http://jira.qos.ch/browse/LBCLASSIC-126
             Project: logback-classic
          Issue Type: New Feature
    Affects Versions: 0.9.15
            Reporter: Anders Wallgren
            Assignee: Logback dev list
         Attachments: NDC.java

The discussion about NDC vs. MDC has been done going for quite a while. I don't think MDC is a suitable replacement for NDC, except insofar as NDC can be implemented using MDC.

Enclosed is the NDC implementation we're started using at Electric Cloud. Please feel free to adapt/modify/enhance it as you see fit. We place this code in the public domain in case others find it useful.

Attachment to come as well.

// NDC.java --
// NDC.java is part of the ElectricCommander server.
// Copyright (c) 2005-2009 Electric Cloud, Inc.
// All rights reserved.

package com.electriccloud.log;

import java.util.Deque;
import java.util.LinkedList;

import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import org.slf4j.MDC;

 * Utility object that implements NDC using MDC.
@SuppressWarnings({"UtilityClass", "StringContatenationInLoop"})
public class NDC

    //~ Static fields/initializers ---------------------------------------------

    private static final ThreadLocal<Deque<String>> s_stack =
        new ThreadLocal<Deque<String>>() {
            @Override @SuppressWarnings({"RefusedBequest"})
            protected Deque<String> initialValue()
                return new LinkedList<String>();

    //~ Constructors -----------------------------------------------------------

    private NDC()

    //~ Methods ----------------------------------------------------------------

    public static void pop()
        Deque<String> stack = s_stack.get();

        // Pop the stack if isn't already empty
        if (!stack.isEmpty()) {

        // Put the previous value in the MDC (null if the stack is now empty)
        MDC.put("NDC", stack.peek());

    public static void push(@NonNls @NotNull String format, Object... args)
        Deque<String> stack    = s_stack.get();
        String        previous = stack.peek();
        String        segment  = args.length == 0
            ? format
            : String.format(format, args);
        String        newValue = previous == null
            ? segment
            : String.format("%s %s", previous, segment);

        MDC.put("NDC", newValue);

This message is automatically generated by JIRA.
If you think it was sent incorrectly contact one of the administrators: http://jira.qos.ch/secure/Administrators.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the logback-dev mailing list