Adding MDC & Request Context information to logging.
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@965 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
6dcadfa3ea
commit
f9f81cda61
|
@ -72,7 +72,42 @@ public class ConsoleAppender implements Appender
|
|||
|
||||
public void setProperty(String key, String value) throws Exception
|
||||
{
|
||||
/* nothing to do here */
|
||||
if ("formatter".equals(key))
|
||||
{
|
||||
setFormatter(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void setFormatter(String classname)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> clazz = Class.forName(classname);
|
||||
if (Formatter.class.isAssignableFrom(clazz))
|
||||
{
|
||||
setFormatter((Formatter)clazz.newInstance());
|
||||
}
|
||||
else
|
||||
{
|
||||
System.err.println("Does not implement " + Formatter.class.getName() + " : " + classname);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
System.err.println("Cannot find formatter: " + classname);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
catch (InstantiationException e)
|
||||
{
|
||||
System.err.println("Cannot instantiate formatter: " + classname);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
catch (IllegalAccessException e)
|
||||
{
|
||||
System.err.println("Cannot instantiate formatter: " + classname);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -193,7 +193,42 @@ public class RollingFileAppender implements Appender
|
|||
return;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("No such key \"" + key + "\"");
|
||||
if ("formatter".equals(key))
|
||||
{
|
||||
setFormatter(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void setFormatter(String classname)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> clazz = Class.forName(classname);
|
||||
if (Formatter.class.isAssignableFrom(clazz))
|
||||
{
|
||||
setFormatter((Formatter)clazz.newInstance());
|
||||
}
|
||||
else
|
||||
{
|
||||
System.err.println("Does not implement " + Formatter.class.getName() + " : " + classname);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
System.err.println("Cannot find formatter: " + classname);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
catch (InstantiationException e)
|
||||
{
|
||||
System.err.println("Cannot instantiate formatter: " + classname);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
catch (IllegalAccessException e)
|
||||
{
|
||||
System.err.println("Cannot instantiate formatter: " + classname);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRetainDays(int retainDays)
|
||||
|
|
|
@ -4,14 +4,14 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.log4j.MDC;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jetty.logging.impl.TestAppender.LogEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.MDC;
|
||||
import org.slf4j.impl.StaticLoggerBinder;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class CentralMDCAdapterTest extends TestCase
|
||||
{
|
||||
public void testMDCInfo() throws Exception
|
||||
|
|
|
@ -165,7 +165,7 @@ public class ConfiguredLoggerTest extends TestCase
|
|||
assertAppenders(root,ConsoleAppender.class);
|
||||
}
|
||||
|
||||
public void testCapturedAppender() throws Exception
|
||||
public void testTestAppender() throws Exception
|
||||
{
|
||||
Properties props = new Properties();
|
||||
props.setProperty("root.level","DEBUG");
|
||||
|
@ -196,6 +196,7 @@ public class ConfiguredLoggerTest extends TestCase
|
|||
props.setProperty("appender.roll.zone","GMT");
|
||||
props.setProperty("appender.roll.dateFormat","yyyy-MM-dd");
|
||||
props.setProperty("appender.roll.backupFormat","HH-mm-ss.SSS");
|
||||
props.setProperty("appender.roll.formatter",DefaultFormatter.class.getName());
|
||||
|
||||
CentralLoggerConfig root = CentralLoggerConfig.load(props);
|
||||
assertNotNull("Root Logger should not be null",root);
|
||||
|
|
|
@ -168,4 +168,9 @@ public class TestAppender implements Appender
|
|||
System.out.println(event);
|
||||
}
|
||||
}
|
||||
|
||||
public void setFormatter(Formatter formatter)
|
||||
{
|
||||
/* nothing to do here */
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
// ========================================================================
|
||||
// Copyright (c) Webtide LLC
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0.txt
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
package org.eclipse.jetty.webapp.logging;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.log4j.MDC;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
||||
|
||||
/**
|
||||
* Adds Logging specific MDC information about the incoming request information.
|
||||
*/
|
||||
public class ContextLogHandler extends HandlerWrapper
|
||||
{
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
// Collect Info for NDC/MDC
|
||||
MDC.put("target",target);
|
||||
String contextPath = request.getContextPath();
|
||||
if (contextPath != null)
|
||||
{
|
||||
MDC.put("contextPath",contextPath);
|
||||
}
|
||||
MDC.put("remoteAddr",request.getRemoteAddr());
|
||||
String remoteUser = request.getRemoteUser();
|
||||
if (remoteUser != null)
|
||||
{
|
||||
MDC.put("remoteUser",remoteUser);
|
||||
}
|
||||
Principal principal = request.getUserPrincipal();
|
||||
if (principal != null)
|
||||
{
|
||||
MDC.put("principal",principal.getName());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
super.handle(target,baseRequest,request,response);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Pop info out / clear the NDC/MDC
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package org.eclipse.jetty.webapp.logging;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.eclipse.jetty.logging.impl.Formatter;
|
||||
import org.eclipse.jetty.logging.impl.Severity;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
public class WebappContextLogFormatter implements Formatter
|
||||
{
|
||||
private String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
|
||||
|
||||
public String format(Date date, Severity severity, String name, String message)
|
||||
{
|
||||
// Take the information out of the NDC/MDC and log it, along with the standard log message
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
buf.append(severity.name()).append(' ');
|
||||
buf.append(new SimpleDateFormat(dateFormat).format(date)).append(' ');
|
||||
buf.append('[').append(name).append("] ");
|
||||
|
||||
String target = MDC.get("target");
|
||||
String userAddr = MDC.get("remoteAddr");
|
||||
String userName = MDC.get("remoteUser");
|
||||
String principal = MDC.get("principal");
|
||||
String contextPath = MDC.get("contextPath");
|
||||
if ((target != null) || (contextPath != null) || (userAddr != null) || (userName != null) || (principal != null))
|
||||
{
|
||||
buf.append('[');
|
||||
// The user info
|
||||
if (principal != null)
|
||||
{
|
||||
buf.append(principal).append(':');
|
||||
}
|
||||
if (userName != null)
|
||||
{
|
||||
buf.append(userName).append(':');
|
||||
}
|
||||
if (userAddr != null)
|
||||
{
|
||||
buf.append(userAddr).append(':');
|
||||
}
|
||||
// The path requested
|
||||
if (contextPath != null)
|
||||
{
|
||||
buf.append(contextPath);
|
||||
}
|
||||
buf.append(target).append("] ");
|
||||
}
|
||||
|
||||
buf.append(message);
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getDateFormat()
|
||||
{
|
||||
return dateFormat;
|
||||
}
|
||||
|
||||
public void setDateFormat(String dateFormat)
|
||||
{
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
}
|
|
@ -135,7 +135,10 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
|
|||
handlers.addHandler(createWebapp("/clogging","dummy-webapp-logging-commons.war"));
|
||||
handlers.addHandler(createWebapp("/javalogging","dummy-webapp-logging-java.war"));
|
||||
|
||||
server.setHandler(handlers);
|
||||
ContextLogHandler loghandler = new ContextLogHandler();
|
||||
loghandler.setHandler(handlers);
|
||||
|
||||
server.setHandler(loghandler);
|
||||
|
||||
server.start();
|
||||
|
||||
|
|
|
@ -17,10 +17,16 @@ package org.eclipse.jetty.webapp.logging;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.eclipse.jetty.logging.impl.Appender;
|
||||
import org.eclipse.jetty.logging.impl.Formatter;
|
||||
import org.eclipse.jetty.logging.impl.Severity;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
/**
|
||||
* Test Appender, records the logging events.
|
||||
|
@ -29,13 +35,15 @@ public class TestAppender implements Appender
|
|||
{
|
||||
public static class LogEvent
|
||||
{
|
||||
String date;
|
||||
Date date;
|
||||
Severity severity;
|
||||
String name;
|
||||
String message;
|
||||
Throwable t;
|
||||
String mdc;
|
||||
|
||||
public LogEvent(String date, Severity severity, String name, String message, Throwable t)
|
||||
@SuppressWarnings("unchecked")
|
||||
public LogEvent(Date date, Severity severity, String name, String message, Throwable t)
|
||||
{
|
||||
super();
|
||||
this.date = date;
|
||||
|
@ -43,6 +51,28 @@ public class TestAppender implements Appender
|
|||
this.name = name;
|
||||
this.message = message;
|
||||
this.t = t;
|
||||
this.mdc = "";
|
||||
|
||||
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
|
||||
if (mdcMap != null)
|
||||
{
|
||||
Set<String> keys = new TreeSet<String>();
|
||||
keys.addAll(mdcMap.keySet());
|
||||
boolean delim = false;
|
||||
for (String key : keys)
|
||||
{
|
||||
if (delim)
|
||||
{
|
||||
mdc += ", ";
|
||||
}
|
||||
mdc += key + "=" + mdcMap.get(key);
|
||||
delim = true;
|
||||
}
|
||||
if (mdc.length() > 0)
|
||||
{
|
||||
System.out.println("mdc: " + mdc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public LogEvent(Severity severity, String name, String message)
|
||||
|
@ -192,7 +222,7 @@ public class TestAppender implements Appender
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public void append(String date, Severity severity, String name, String message, Throwable t)
|
||||
public void append(Date date, Severity severity, String name, String message, Throwable t)
|
||||
{
|
||||
if (name.equals("org.eclipse.jetty.util.log")) // standard jetty logger
|
||||
{
|
||||
|
@ -213,39 +243,6 @@ public class TestAppender implements Appender
|
|||
|
||||
public boolean contains(LogEvent expectedEvent)
|
||||
{
|
||||
/*
|
||||
// System.out.println("Looking for: " + expectedEvent);
|
||||
for (LogEvent event : events)
|
||||
{
|
||||
// System.out.println("Event: " + event);
|
||||
if (!event.name.equals(expectedEvent.name))
|
||||
{
|
||||
continue; // not a match. skip.
|
||||
}
|
||||
if (!event.severity.equals(expectedEvent.severity))
|
||||
{
|
||||
continue; // not a match. skip.
|
||||
}
|
||||
if (expectedEvent.t != null)
|
||||
{
|
||||
if (event.t == null)
|
||||
{
|
||||
continue; // not a match. skip.
|
||||
}
|
||||
if (!event.t.getClass().equals(expectedEvent.t.getClass()))
|
||||
{
|
||||
continue; // not a match. skip.
|
||||
}
|
||||
if (!event.t.getMessage().equals(expectedEvent.t.getMessage()))
|
||||
{
|
||||
continue; // not a match. skip.
|
||||
}
|
||||
}
|
||||
if (event.message.equals(expectedEvent.message))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}*/
|
||||
return events.contains(expectedEvent);
|
||||
}
|
||||
|
||||
|
@ -277,4 +274,9 @@ public class TestAppender implements Appender
|
|||
System.out.println(event);
|
||||
}
|
||||
}
|
||||
|
||||
public void setFormatter(Formatter formatter)
|
||||
{
|
||||
/* nothing to do here */
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,7 +91,7 @@ public class XmlConfiguredJetty
|
|||
|
||||
public void addConfiguration(File xmlConfigFile) throws MalformedURLException
|
||||
{
|
||||
xmlConfigurations.add(xmlConfigFile.toURL());
|
||||
xmlConfigurations.add(xmlConfigFile.toURI().toURL());
|
||||
}
|
||||
|
||||
public void addConfiguration(String testConfigName) throws MalformedURLException
|
||||
|
|
Loading…
Reference in New Issue