package com.googlecode.jmxtrans.model.output;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

import com.googlecode.jmxtrans.model.Query;
import com.googlecode.jmxtrans.model.Result;
import com.googlecode.jmxtrans.util.BaseOutputWriter;
import com.googlecode.jmxtrans.util.JmxUtils;
import com.googlecode.jmxtrans.util.ValidationException;


/**
 * 
 * A writer for Log4J. It may be a nice way to send JMX metrics to Logstash for example. </br> </br>
 * 
 * Here is an example of MDC variables that are set by this class. </br> </br> server: localhost_9003 </br> metric:
 * sun_management_MemoryImpl.HeapMemoryUsage_committed </br> value: 1251999744 </br> resultAlias: myHeapMemoryUsage
 * </br> attributeName: HeapMemoryUsage </br> key: committed </br> epoch: 1388343325728 </br>
 * 
 * @author Yannick Robin
 */
public class Log4JWriter extends BaseOutputWriter
{
	private static Logger LOG;

	public Log4JWriter()
	{
	}

	/**
	 * Get the logger
	 */
	@Override
	public void validateSetup(final Query query) throws ValidationException
	{
		String loggerName = (String) this.getSettings().get("logger");

		if (loggerName == null || loggerName.equals(""))
		{
			loggerName = "Log4JWriter";
		}

		LOG = Logger.getLogger("Log4JWriter." + loggerName);
	}

	/**
	 * Set the log context and log
	 */
	@Override
	public void doWrite(final Query query) throws Exception
	{
		final List<String> typeNames = getTypeNames();

		for (final Result result : query.getResults())
		{
			final Map<String, Object> resultValues = result.getValues();
			if (resultValues != null)
			{
				for (final Entry<String, Object> values : resultValues.entrySet())
				{
					if (JmxUtils.isNumeric(values.getValue()))
					{
						String alias = null;
						if (query.getServer().getAlias() != null)
						{
							alias = query.getServer().getAlias();
						}
						else
						{
							alias = query.getServer().getHost() + "_" + query.getServer().getPort();
							alias = cleanupStr(alias);
						}

						MDC.put("server", alias);
						MDC.put("metric", JmxUtils.getKeyString2(query, result, values, typeNames, null));
						MDC.put("value", values.getValue());
						if (result.getClassNameAlias() != null)
						{
							MDC.put("resultAlias", result.getClassNameAlias());
						}
						MDC.put("attributeName", result.getAttributeName());
						MDC.put("key", values.getKey());
						MDC.put("Epoch", String.valueOf(result.getEpoch()));
						LOG.info("");
					}
				}
			}
		}
	}
}

@@ -44,144 +44,8 @@
 t;%0A%0A
-import java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0A%0Aimport org.apache.log4j.Logger;%0Aimport org.apache.log4j.MDC;%0A%0A
 impo
@@ -286,16 +286,151 @@
 eption;%0A
+import org.apache.log4j.Logger;%0Aimport org.apache.log4j.MDC;%0A%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0A
 %0A%0A/**%0A *
@@ -521,25 +521,27 @@
 ample. %3C
-/
 br
+ /
 %3E %3C
-/
 br
+ /
 %3E%0A * %0A *
@@ -610,15 +610,13 @@
 s. %3C
-/
 br%3E %3C
-/
 br%3E 
@@ -639,19 +639,20 @@
 t_9003 %3C
-/
 br
+ /
 %3E metric
@@ -709,19 +709,20 @@
 mitted %3C
-/
 br
+ /
 %3E value:
@@ -734,19 +734,20 @@
 999744 %3C
-/
 br
+ /
 %3E result
@@ -775,19 +775,20 @@
 age%0A * %3C
-/
 br
+ /
 %3E attrib
@@ -813,19 +813,20 @@
 yUsage %3C
-/
 br
+ /
 %3E key: c
@@ -835,19 +835,20 @@
 mitted %3C
-/
 br
+ /
 %3E epoch:
@@ -867,11 +867,12 @@
 28 %3C
-/
 br
+ /
 %3E%0A *
@@ -983,16 +983,24 @@
 r LOG;%0A%0A
+%09/** */%0A
 %09public 
@@ -1012,21 +1012,19 @@
 Writer()
-%0A%09%7B%0A%09
+ %7B 
 %7D%0A%0A%09/**%0A
@@ -1043,35 +1043,24 @@
 logger%0A%09 */%0A
-%09@Override%0A
 %09public void
@@ -1385,19 +1385,8 @@
  */%0A
-%09@Override%0A
 %09pub
@@ -2129,17 +2129,16 @@
 eyString
-2
 (query, 

fix build for log4jwriter