You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Rob Mitchell <Ro...@comcast.net> on 2003/10/16 17:50:14 UTC
SocketAppender and MDC?
Hello,
I have a Java web app that creates a SocketAppender like this:
myLogger = LogManager.getLogger(sName);
myLogger.setAdditivity(false); // prevents double output to console
SocketAppender sa = new SocketAppender(sRemoteHost, iRemotePort);
myLogger.addAppender(sa);
myLogger.setLevel(Level.INFO);
I send messages from an HTTP Request using MDC as well like this:
// now iterate over all the request parameters
try {
Enumeration enum = request.getParameterNames();
while ( enum.hasMoreElements() ) {
String paramName = (String) enum.nextElement();
String paramValue = (String) request.getParameter(paramName);
if ( (paramName!=null) && (paramValue!=null) ) {
MDC.put(paramName, paramValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
myLogger.info("test MDC");
and on the remote machine, I'm using JDBCAppender classes. I wrote a JDBCSqlHandler class that gets those MDC values by key name, but then I want to dump to whole MDC context (the entire Hashtable) but the MDC always returns null.
// Remote side parses HTTP Request params from MDC's hashtable into XML
private String toXML() {
StringBuffer sb = new StringBuffer("<HTTP_REQ_PARAMS>");
Hashtable hash = MDC.getContext();
if (hash != null) {
System.out.println("\n\tHashtable size=" + hash.size());
Iterator iter = hash.keySet().iterator();
while (iter.hasNext()) {
String sKey = (String)iter.next();
String sVal = (String)hash.get(sKey);
System.out.println (" Key=" + sKey + " Value=" + sVal);
if ( (sKey!=null) && (sVal!=null) ) {
sb.append("<").append(sKey).append(">")
.append("<![CDATA[").append(sVal).append("]]>")
.append("</").append(quotedString(sKey)).append(">");
}
}
} else {
System.out.println("\n\tHashtable is NULL!!");
}
sb.append("</HTTP_REQ_PARAMS>");
return sb.toString();
}
The above always says the MDC.getContext() is null. Yet I can make calls on the remote side like this that work:
String sEnv = "'" + ( (event.getMDC("env")==null) ? "" : (String)event.getMDC("env") ) + "'";
String sObj = "'" + ( (event.getMDC("obj")==null) ? "" : (String)event.getMDC("obj") ) + "'";
String sCmd = "'" + ( (event.getMDC("cmd")==null) ? "" : (String)event.getMDC("cmd") ) + "'";
I get actual values for sEnv, sObj, sCmd ... but the MDC.getContext() returns null. Any ideas?
Thanks!
_______________________________________
Rob Mitchell
Base Class Technologies, Inc.
Java, WebSphere, Domino, Oracle, Web development
Rob.Mitchell@comcast.net