package mil.dds.anet.resources;

import java.util.List;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.dropwizard.auth.Auth;
import mil.dds.anet.beans.Person;

@Path("/api/logging")
@Consumes(MediaType.APPLICATION_JSON)
@PermitAll

public class LoggingResource {

	static class LogEntry {
		@JsonProperty String severity;
		@JsonProperty String url;
		@JsonProperty String lineNr;
		@JsonProperty String message;
	  }

	private final Logger logger = Logger.getLogger(LoggingResource.class);
	
	/**
	 * Creates a log entry based on the following inputs
	 * - severity: one of 'DEBUG','ERROR','FATAL','INFO','WARN'
	 * - url: the context url
	 * - lineNr: line number of the error
	 * - message: The error/log message
	 */
	@POST
	@Timed
	@Path("/log")
	@PermitAll
	public void logMessage(final @Context HttpServletRequest requestContext, final @Auth Person user, final List<LogEntry> logEntries) {
		for (LogEntry logEntry : logEntries)
			{
			final StringBuilder messageBuilder = new StringBuilder();			
			messageBuilder.append(user.getId());
			messageBuilder.append(" ");
			messageBuilder.append(requestContext.getRemoteAddr());
			messageBuilder.append(" ");
			messageBuilder.append(logEntry.url);
			messageBuilder.append(" ");
			messageBuilder.append(logEntry.lineNr);
			messageBuilder.append(" ");
			messageBuilder.append(logEntry.message);
			logger.log(Level.toLevel(logEntry.severity), messageBuilder.toString());		
			}
		}
}

@@ -26,16 +26,55 @@
 urces;%0A%0A
+import java.lang.invoke.MethodHandles;%0A
 import j
@@ -630,17 +630,16 @@
 rmitAll%0A
-%0A
 public c
@@ -667,31 +667,180 @@
 %7B%0A%0A%09
-static class LogEntry %7B
+private static final Logger logger = Logger.getLogger(MethodHandles.lookup().lookupClass());%0A%0A%09static class LogEntry %7B%0A%09%09/** one of 'DEBUG','ERROR','FATAL','INFO','WARN' */
 %0A%09%09@
@@ -869,16 +869,41 @@
 verity;%0A
+%09%09/** the context url */%0A
 %09%09@JsonP
@@ -928,146 +928,136 @@
 ;%0A%09%09
-@JsonProperty String lineNr;%0A%09%09@JsonProperty String message;%0A%09  %7D%0A%0A%09private final Logger logger = Logger.getLogger(LoggingResource.class);
+/** line number of the error */%0A%09%09@JsonProperty String lineNr;%0A%09%09/** the error/log message */%0A%09%09@JsonProperty String message;%0A%09%7D
 %0A%09%0A%09
@@ -1070,17 +1070,16 @@
 * Create
-s
  a log e
@@ -1116,172 +1116,202 @@
 puts
+:
 %0A%09 * 
-- severity: one of 'DEBUG','ERROR','FATAL','INFO','WARN'%0A%09 * - url: the context url%0A%09 * - lineNr: line number of the error%0A%09 * - message: The error/log message
+@param requestContext the HTTP request context (used for getting the remote address)%0A%09 * @param user the authenticated user logging the messages%0A%09 * @param logEntries a list of log entries
 %0A%09 *
@@ -1495,16 +1495,22 @@
 %0A%09%09for (
+final 
 LogEntry
@@ -1536,263 +1536,142 @@
 ies)
-%0A%09%09%09
+ 
 %7B%0A%09%09%09
-final StringBuilder messageBuilder = new StringBuilder();%09%09%09%0A%09%09%09messageBuilder.append(user.getId());%0A%09%09%09messageBuilder.append(%22 %22);%0A%09%09%09messageBuilder.append(requestContext.getRemoteAddr());%0A%09%09%09messageBuilder.append(%22 %22);%0A%09%09%09messageBuilder.append(
+logger.log(Level.toLevel(logEntry.severity), String.format(%22%7B%7D %7B%7D %7B%7D %7B%7D %7B%7D%22,%0A%09%09%09%09%09user.getId(), requestContext.getRemoteAddr(), 
 logE
@@ -1682,244 +1682,56 @@
 .url
-);
+,
 %0A%09%09%09
-messageBuilder.append(%22 %22);%0A%09%09%09messageBuilder.append(logEntry.lineNr);%0A%09%09%09messageBuilder.append(%22 %22);%0A%09%09%09messageBuilder.append(logEntry.message);%0A%09%09%09logger.log(Level.toLevel(logEntry.severity), messageBuilder.toString());%09%09%0A%09
+%09%09logEntry.lineNr, logEntry.message));%0A
 %09%09%7D%0A
-%09
 %09%7D%0A%7D

Refactor class for readability