001/* 002 * Copyright 2001-2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.apache.log4j; 018 019import org.slf4j.MDC; 020 021import java.util.Stack; 022 023/** 024 * A log4j's NDC implemented in terms of SLF4J MDC primitives. 025 * 026 * @since SLF4J 1.6.0 027 */ 028 029public class NDC { 030 031 public final static String PREFIX = "NDC"; 032 033 public static void clear() { 034 int depth = getDepth(); 035 for (int i = 0; i < depth; i++) { 036 String key = PREFIX + i; 037 MDC.remove(key); 038 } 039 } 040 041 @SuppressWarnings("rawtypes") 042 public static Stack cloneStack() { 043 return null; 044 } 045 046 @SuppressWarnings("rawtypes") 047 public static void inherit(Stack stack) { 048 } 049 050 static public String get() { 051 return null; 052 } 053 054 public static int getDepth() { 055 int i = 0; 056 while (true) { 057 String val = MDC.get(PREFIX + i); 058 if (val != null) { 059 i++; 060 } else { 061 break; 062 } 063 } 064 return i; 065 } 066 067 public static String pop() { 068 int next = getDepth(); 069 if (next == 0) { 070 return ""; 071 } 072 int last = next - 1; 073 String key = PREFIX + last; 074 String val = MDC.get(key); 075 MDC.remove(key); 076 return val; 077 } 078 079 public static String peek() { 080 int next = getDepth(); 081 if (next == 0) { 082 return ""; 083 } 084 int last = next - 1; 085 String key = PREFIX + last; 086 String val = MDC.get(key); 087 return val; 088 } 089 090 public static void push(String message) { 091 int next = getDepth(); 092 MDC.put(PREFIX + next, message); 093 } 094 095 static public void remove() { 096 clear(); 097 } 098 099 static public void setMaxDepth(int maxDepth) { 100 } 101 102}