Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable default access logger and utilize log4j2 for http access logs #2185

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.params.HttpParams;
import org.apache.synapse.transport.http.conn.LoggingUtils;
import org.apache.synapse.transport.http.wrapper.HttpRequestWrapper;
import org.apache.synapse.transport.http.wrapper.HttpResponseWrapper;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* The class to handle the HTTP Access Logs, patterns and the major functionality.
Expand All @@ -43,6 +42,7 @@
*/
public class Access {
private static Log log = LogFactory.getLog(Access.class);
private final Log accesslog = LogFactory.getLog(LoggingUtils.ACCESS_LOG_ID);

/**
* Array of AccessLogElement, they will be used to make log message.
Expand All @@ -53,10 +53,8 @@ public class Access {

private static AccessLogger accessLogger;

private static ConcurrentLinkedQueue<HttpRequestWrapper> requestQueue;
private static ConcurrentLinkedQueue<HttpResponseWrapper> responseQueue;

private static final int LOG_FREQUENCY_IN_SECONDS = 30;
private static LinkedBlockingQueue<HttpRequestWrapper> requestQueue;
private static LinkedBlockingQueue<HttpResponseWrapper> responseQueue;

private Date date;

Expand All @@ -70,8 +68,8 @@ public Access(final Log log, AccessLogger accessLogger) {
super();
Access.log = log;
Access.accessLogger = accessLogger;
requestQueue = new ConcurrentLinkedQueue<HttpRequestWrapper>();
responseQueue = new ConcurrentLinkedQueue<HttpResponseWrapper>();
requestQueue = new LinkedBlockingQueue<HttpRequestWrapper>();
responseQueue = new LinkedBlockingQueue<HttpResponseWrapper>();
logElements = createLogElements();
logAccesses();
}
Expand Down Expand Up @@ -104,30 +102,36 @@ public void addAccessToQueue(HttpResponse response) {
* logs the request and response accesses.
*/
public void logAccesses() {
TimerTask logRequests = new LogRequests();
TimerTask logResponses = new LogResponses();
Timer requestTimer = new Timer();
Timer responseTimer = new Timer();
// Retry in 30 seconds
long retryIn = 1000 * LOG_FREQUENCY_IN_SECONDS;
requestTimer.schedule(logRequests, 0, retryIn);
responseTimer.schedule(logResponses, 0, retryIn);
Thread logRequests = new LogRequests();
Thread logResponses = new LogResponses();
logRequests.start();
logResponses.start();
}

private class LogRequests extends TimerTask {
private class LogRequests extends Thread {
public void run() {
while (!requestQueue.isEmpty()) {
HttpRequestWrapper req = requestQueue.poll();
log(req, null);
while (true) {
HttpRequestWrapper req = null;
try {
req = requestQueue.take();
log(req, null);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

private class LogResponses extends TimerTask {
private class LogResponses extends Thread {
public void run() {
while (!responseQueue.isEmpty()) {
HttpResponseWrapper res = responseQueue.poll();
log(null, res);
while (true) {
HttpResponseWrapper res = null;
try {
res = responseQueue.take();
log(null, res);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
Expand Down Expand Up @@ -874,7 +878,7 @@ public void log(HttpRequestWrapper request, HttpResponseWrapper response) {
}
}
String logString = result.toString();
log.debug(logString); //log to the console
accesslog.info(logString); //log to the console
if (accessLogger.isLoggingEnabled) {
accessLogger.log(logString); //log to the file
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public AccessLogger(final Log log) {
/**
* Enable logging.
*/
public boolean isLoggingEnabled = getBooleanValue(CONFIG_ENABLE_LOGGING, true);
public boolean isLoggingEnabled = getBooleanValue(CONFIG_ENABLE_LOGGING, false);

/**
* Log the specified message to the log file, switching files if the date
Expand Down
Loading