[jboss-svn-commits] JBoss Common SVN: r2439 - in common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk: format and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Jul 9 17:31:43 EDT 2007
Author: jason.greene at jboss.com
Date: 2007-07-09 17:31:43 -0400 (Mon, 09 Jul 2007)
New Revision: 2439
Added:
common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKMDCProvider.java
common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKNDCProvider.java
Modified:
common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java
common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/format/PatternParser.java
Log:
Add a JUL implementation of NDC/MDC - JBCOMMON-28
Modified: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java
===================================================================
--- common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java 2007-07-09 21:30:31 UTC (rev 2438)
+++ common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java 2007-07-09 21:31:43 UTC (rev 2439)
@@ -26,13 +26,17 @@
import java.util.logging.Level;
import org.jboss.logging.LoggerPlugin;
+import org.jboss.logging.MDCProvider;
+import org.jboss.logging.MDCSupport;
+import org.jboss.logging.NDCProvider;
+import org.jboss.logging.NDCSupport;
/** An example LoggerPlugin which uses the JDK java.util.logging framework.
- *
+ *
* @author Scott.Stark at jboss.org
* @version $Revison:$
*/
-public class JDK14LoggerPlugin implements LoggerPlugin
+public class JDK14LoggerPlugin implements LoggerPlugin, MDCSupport, NDCSupport
{
private Logger log;
@@ -115,4 +119,14 @@
{
log.log(Level.SEVERE, message.toString(), t);
}
+
+ public NDCProvider getNDCProvider()
+ {
+ return new JDKNDCProvider();
+ }
+
+ public MDCProvider getMDCProvider()
+ {
+ return new JDKMDCProvider();
+ }
}
Added: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKMDCProvider.java
===================================================================
--- common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKMDCProvider.java (rev 0)
+++ common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKMDCProvider.java 2007-07-09 21:31:43 UTC (rev 2439)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.logging.jdk;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.logging.MDCProvider;
+
+/**
+ * MDC implementation for JDK logging.
+ *
+ * @author Jason T. Greene
+ */
+public class JDKMDCProvider implements MDCProvider
+{
+ private ThreadLocal<Map<String, Object>> map = new ThreadLocal<Map<String, Object>>();
+
+ public Object get(String key)
+ {
+ return map.get() == null ? null : map.get().get(key);
+ }
+
+ public Map<String, Object> getMap()
+ {
+ return map.get();
+ }
+
+ public void put(String key, Object value)
+ {
+ Map<String, Object> map = this.map.get();
+ if (map == null)
+ {
+ map = new HashMap<String, Object>();
+ this.map.set(map);
+ }
+
+ map.put(key, value);
+ }
+
+ public void remove(String key)
+ {
+ Map<String, Object> map = this.map.get();
+ if (map == null)
+ return;
+
+ map.remove(key);
+ }
+}
\ No newline at end of file
Property changes on: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKMDCProvider.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKNDCProvider.java
===================================================================
--- common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKNDCProvider.java (rev 0)
+++ common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKNDCProvider.java 2007-07-09 21:31:43 UTC (rev 2439)
@@ -0,0 +1,149 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.logging.jdk;
+
+import java.util.ArrayList;
+import java.util.EmptyStackException;
+import java.util.Stack;
+
+import org.jboss.logging.NDCProvider;
+
+/**
+ * NDC implementation for JDK logging
+ *
+ * @author Jason T. Greene
+ */
+public class JDKNDCProvider implements NDCProvider
+{
+ private class ArrayStack<E> extends ArrayList<E>
+ {
+ private static final long serialVersionUID = -8520038422243642840L;
+
+ public E pop()
+ {
+ int size = size();
+ if (size == 0)
+ throw new EmptyStackException();
+
+ return remove(size - 1);
+ }
+
+ public E peek()
+ {
+ int size = size();
+ if (size == 0)
+ throw new EmptyStackException();
+
+ return get(size - 1);
+ }
+
+ public void push(E val)
+ {
+ add(val);
+ }
+
+ public void setSize(int newSize)
+ {
+ int size = size();
+ if (newSize >= size || newSize < 0)
+ return;
+
+ removeRange(newSize, size);
+ }
+ }
+
+ private class Entry
+ {
+ private String merged;
+ private String current;
+
+ public Entry(String current)
+ {
+ this.merged = current;
+ this.current = current;
+ }
+
+ public Entry(Entry parent, String current)
+ {
+ this.merged = parent.merged + ' ' + current;
+ this.current = current;
+ }
+ }
+
+ private ThreadLocal<ArrayStack<Entry>> stack = new ThreadLocal<ArrayStack<Entry>>();
+
+ public void clear()
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+ if (stack != null)
+ stack.clear();
+ }
+
+ public String get()
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+
+ return stack == null || stack.isEmpty() ? null : stack.peek().merged;
+ }
+
+ public int getDepth()
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+
+ return stack == null ? 0 : stack.size();
+ }
+
+ public String peek()
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+
+ return stack == null || stack.isEmpty() ? "" : stack.peek().current;
+ }
+
+ public String pop()
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+
+ return stack == null || stack.isEmpty() ? "" : stack.pop().current;
+ }
+
+ public void push(String message)
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+
+ if (stack == null)
+ {
+ stack = new ArrayStack<Entry>();
+ this.stack.set(stack);
+ }
+
+ stack.push(stack.isEmpty() ? new Entry(message) : new Entry(stack.peek(), message));
+ }
+
+ public void setMaxDepth(int maxDepth)
+ {
+ ArrayStack<Entry> stack = this.stack.get();
+
+ if (stack != null)
+ stack.setSize(maxDepth);
+ }
+}
Property changes on: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDKNDCProvider.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/format/PatternParser.java
===================================================================
--- common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/format/PatternParser.java 2007-07-09 21:30:31 UTC (rev 2438)
+++ common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/format/PatternParser.java 2007-07-09 21:31:43 UTC (rev 2439)
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2005 The Apache Software Foundation.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,6 +20,9 @@
import java.util.Date;
import java.util.logging.LogRecord;
+import org.jboss.logging.MDC;
+import org.jboss.logging.NDC;
+
// Contributors: Nelson Minar <(nelson at monkey.org>
// Igor E. Poteryaev <jah at mail.ru>
// Reinhard Deschler <reinhard.deschler at web.de>
@@ -371,11 +374,13 @@
}
break;*/
case 'x':
+ case 'z':
pc = new BasicPatternConverter(formattingInfo, NDC_CONVERTER);
//LogLog.debug("NDC converter.");
currentLiteral.setLength(0);
break;
case 'X':
+ case 'Z':
String xOpt = extractOption();
pc = new MDCPatternConverter(formattingInfo, xOpt);
currentLiteral.setLength(0);
@@ -432,7 +437,8 @@
case LEVEL_CONVERTER:
return event.getLevel().toString();
case NDC_CONVERTER:
- return "NDC not supported";
+ // For now rely on the thread local
+ return NDC.get();
case MESSAGE_CONVERTER:
{
return event.getMessage();
@@ -492,9 +498,6 @@
}
}
- /**
- * @todo how can this functionality be restored
- */
private static class MDCPatternConverter extends PatternConverter
{
private String key;
@@ -507,7 +510,8 @@
public String convert(LogRecord event)
{
- Object val = null; // event.getMDC(key);
+ // For now, we rely on the thread local
+ Object val = MDC.get(key);
if (val == null)
{
return null;
More information about the jboss-svn-commits
mailing list