package org.stagemonitor.web.logging;

import java.util.UUID;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.MDC;
import org.stagemonitor.core.CorePlugin;
import org.stagemonitor.core.MeasurementSession;
import org.stagemonitor.core.Stagemonitor;

/**
 * This class adds the {@link MDC} properties requestId, application, host and instance.
 * <p/>
 * If you are using logback or log4j, you can append this to your pattern to append the properties to each log entry:
 * <code>R:[%X{requestId}] A:[%X{application}] H:[%X{host}] I:[%X{instance}]</code>
 */
@WebListener
public class MDCListener implements ServletRequestListener {

	public static final String STAGEMONITOR_REQUEST_ID_ATTR = "stagemonitor-request-id";

	public static final CorePlugin corePlugin = Stagemonitor.getConfiguration(CorePlugin.class);

	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		if (corePlugin.isStagemonitorActive()) {
			final MeasurementSession measurementSession = Stagemonitor.getMeasurementSession();
			MDC.put("application", measurementSession.getApplicationName());
			MDC.put("host", measurementSession.getHostName());
			String instanceName = measurementSession.getInstanceName();
			if (instanceName == null) {
				instanceName = sre.getServletRequest().getServerName();
			}
			MDC.put("instance", instanceName);

			final String requestId = UUID.randomUUID().toString();
			sre.getServletRequest().setAttribute(STAGEMONITOR_REQUEST_ID_ATTR, requestId);

			if (Stagemonitor.isStarted()) {
				// don't store the requestId in MDC if stagemonitor is not active
				// so that thread pools that get created on startup don't inherit the requestId
				MDC.put("requestId", requestId);
			}
		}
	}

	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		// application, host and instance don't have to be removed, because they stay the same for all threads
		MDC.remove("requestId");
	}
}

@@ -1108,16 +1108,75 @@
 sion();%0A
+%09%09%09if (measurementSession.getApplicationName() != null) %7B%0A%09
 %09%09%09MDC.p
@@ -1235,16 +1235,73 @@
 ame());%0A
+%09%09%09%7D%0A%09%09%09if (measurementSession.getHostName() != null) %7B%0A%09
 %09%09%09MDC.p
@@ -1346,16 +1346,21 @@
 ame());%0A
+%09%09%09%7D%0A
 %09%09%09Strin

Add more null checks do MDCListener