package com.logentries.log4j;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import com.logentries.net.AsyncLogger;

/**
 * Logentries appender for log4j.
 * 
 * @author Mark Lacomber
 * 
 */
public class LogentriesAppender extends AppenderSkeleton {
	
	/*
	 * Fields
	 */
	/** Asynchronous Background logger */
	AsyncLogger le_async;

	public LogentriesAppender()
	{
		le_async = new AsyncLogger();
	}

	/*
	 * Public methods to send log4j parameters to AsyncLogger
	 */
	/**
	 * Sets the token
	 * 
	 * @param token
	 */
	public void setToken( String token) {
		this.le_async.setToken(token);
	}
	
	/**
	 *  Sets the HTTP PUT boolean flag. Send logs via HTTP PUT instead of default Token TCP
	 *  
	 *  @param httpput HttpPut flag to set
	 */
	public void setHttpPut( boolean HttpPut) {
		this.le_async.setHttpPut(HttpPut);
	}
	
	/** Sets the ACCOUNT KEY value for HTTP PUT 
	 * 
	 * @param account_key
	 */
	public void setKey( String account_key)
	{
		this.le_async.setKey(account_key);
	}
	
	/**
	 * Sets the LOCATION value for HTTP PUT
	 * 
	 * @param log_location
	 */
	public void setLocation( String log_location)
	{
		this.le_async.setLocation(log_location);
	}
	
	/**
	 * Sets the SSL boolean flag
	 * 
	 * @param ssl
	 */
	public void setSsl( boolean ssl)
	{
		this.le_async.setSsl(ssl);
	}
	
	/**
	 * Sets the debug flag. Appender in debug mode will print error messages on
	 * error console.
	 * 
	 * @param debug debug flag to set
	 */
	public void setDebug( boolean debug) {
		this.le_async.setDebug(debug);
	}

	/**
	 * Sets the flag which determines if DataHub instance is used instead of Logentries service.
	 *
	 * @param useDataHub set to true to send log messaged to a DataHub instance.
	 */
	public void setIsUsingDataHub(boolean useDataHub){
		this.le_async.setUseDataHub(useDataHub);
	}

	/**
	 * Sets the address where DataHub server resides.
	 *
	 * @param dataHubAddr address like "127.0.0.1"
	 */
	public void setDataHubAddr(String dataHubAddr){
		this.le_async.setDataHubAddr(dataHubAddr);
	}

	/**
	 * Sets the port number on which DataHub instance waits for log messages.
	 *
	 * @param dataHubPort
	 */
	public void setDataHubPort(int dataHubPort){
		this.le_async.setDataHubPort(dataHubPort);
	}

	/**
	 * Determines whether to send HostName alongside with the log message
	 *
	 * @param logHostName
	 */
	public void setLogHostName(boolean logHostName) { 
		this.le_async.setLogHostName(logHostName); 
	}

	/**
	 * Sets the HostName from the configuration
	 *
	 * @param hostName
	 */
	public void setHostName(String hostName) { 
		this.le_async.setHostName(hostName); 
	}

	/**
	 * Sets LogID parameter from the configuration
	 *
	 * @param logID
	 */
	public void setLogID(String logID) { 
		this.le_async.setLogID(logID); 
	}

	/**
	 * Implements AppenderSkeleton Append method, handles time and format
	 * 
	 * @event event to log
	 */
	@Override
	protected void append( LoggingEvent event) {

		// Render the event according to layout
		String formattedEvent = layout.format( event);

		// Append stack trace if present
		String[] stack = event.getThrowableStrRep();
		if (stack != null)
		{
			int len = stack.length;
			formattedEvent += ", ";
			for(int i = 0; i < len; i++)
			{
				formattedEvent += stack[i];
				if(i < len - 1)
					formattedEvent += "\u2028";
			}
		}
				
		// Prepare to be queued
		this.le_async.addLineToQueue(formattedEvent);
	}

	/**
	 * Closes all connections to Logentries
	 */
	@Override
	public void close() {
		this.le_async.close();
	}

	@Override
	public boolean requiresLayout() {
		return true;
	}
}

@@ -3091,17 +3091,83 @@
  present
-%0A
+ and layout does not handle it%0A%09%09if (layout.ignoresThrowable()) %7B%0A%09
 %09%09String
@@ -3207,16 +3207,17 @@
 ep();%0A%09%09
+%09
 if (stac
@@ -3229,18 +3229,20 @@
 null)%0A%09%09
+%09
 %7B%0A
+%09
 %09%09%09int l
@@ -3263,16 +3263,17 @@
 gth;%0A%09%09%09
+%09
 formatte
@@ -3288,16 +3288,17 @@
 = %22, %22;%0A
+%09
 %09%09%09for(i
@@ -3320,26 +3320,28 @@
 en; i++)%0A%09%09%09
+%09
 %7B%0A
+%09
 %09%09%09%09formatte
@@ -3360,16 +3360,17 @@
 ack%5Bi%5D;%0A
+%09
 %09%09%09%09if(i
@@ -3382,24 +3382,25 @@
 n - 1)%0A%09%09%09%09%09
+%09
 formattedEve
@@ -3415,16 +3415,22 @@
 u2028%22;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

fix #40 ignoresThrowable()