Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-534

NDC implementation

    XMLWordPrintable

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 0.9.15
    • None
    • None

    Description

      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())

      { stack.pop(); }

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

      @SuppressWarnings(

      {"HardCodedStringLiteral"}

      )
      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); stack.push(newValue); MDC.put("NDC", newValue); }

      }

      Attachments

        1. NDC.java
          2 kB
          Anders Wallgren

        Activity

          People

            logback-dev Logback dev list
            anders Anders Wallgren
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: