Skip to content

Commit

Permalink
Refactor access logging process
Browse files Browse the repository at this point in the history
  • Loading branch information
AzeemMuzammil committed May 21, 2024
1 parent c9e26c9 commit 59ca393
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 514 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@

import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogConfig;
import io.ballerina.stdlib.http.api.logging.formatters.HttpAccessLogFormatter;
import io.ballerina.stdlib.http.api.logging.formatters.HttpTraceLogFormatter;
import io.ballerina.stdlib.http.api.logging.formatters.JsonLogFormatter;
import io.ballerina.stdlib.http.api.logging.logger.HttpAccessLogger;
import io.ballerina.stdlib.http.api.logging.logger.HttpLoggerFactory;
import io.ballerina.stdlib.http.transport.contractimpl.common.accesslog.HttpAccessLogFormat;
import io.ballerina.stdlib.http.transport.contractimpl.common.accesslog.HttpAccessLogMessage;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -44,13 +39,10 @@
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_ACCESS_LOG_ENABLED;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_CONSOLE;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FILE_PATH;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FORMAT;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FORMAT_JSON;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_TRACE_LOG;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_TRACE_LOG_ENABLED;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_TRACE_LOG_HOST;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_TRACE_LOG_PORT;
import static io.ballerina.stdlib.http.transport.contract.Constants.ACCESS_LOG;

/**
* Java util logging manager for ballerina which overrides the readConfiguration method to replace placeholders
Expand All @@ -72,15 +64,14 @@ public class HttpLogManager extends LogManager {

protected Logger httpTraceLogger;
protected Logger httpAccessLogger;
protected InternalLogger httpInternalLogger;
private String protocol;

public HttpLogManager(boolean traceLogConsole, BMap traceLogAdvancedConfig, BMap accessLogConfig,
BString protocol) {
this.protocol = protocol.getValue();
this.setHttpTraceLogHandler(traceLogConsole, traceLogAdvancedConfig);
this.setHttpAccessLogHandler(accessLogConfig);
HttpAccessLogMessage.initializeHttpAccessLogConfig(accessLogConfig);
HttpAccessLogConfig.initializeHttpAccessLogConfig(accessLogConfig);
}

/**
Expand Down Expand Up @@ -145,17 +136,13 @@ public void setHttpAccessLogHandler(BMap accessLogConfig) {
// keep a reference to prevent this logger from being garbage collected
httpAccessLogger = Logger.getLogger(HTTP_ACCESS_LOG);
}
if (httpInternalLogger == null) {
httpInternalLogger = InternalLoggerFactory.getInstance(ACCESS_LOG);
}
PrintStream stdErr = System.err;
boolean accessLogsEnabled = false;

HttpAccessLogFormat accessLogFormat = getAccessLogFormat(accessLogConfig);
Boolean consoleLogEnabled = accessLogConfig.getBooleanValue(HTTP_LOG_CONSOLE);
if (consoleLogEnabled) {
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new HttpAccessLogFormatter(accessLogFormat));
consoleHandler.setFormatter(new HttpAccessLogFormatter());
consoleHandler.setLevel(Level.INFO);
httpAccessLogger.addHandler(consoleHandler);
httpAccessLogger.setLevel(Level.INFO);
Expand All @@ -166,7 +153,7 @@ public void setHttpAccessLogHandler(BMap accessLogConfig) {
if (filePath != null && !filePath.getValue().trim().isEmpty()) {
try {
FileHandler fileHandler = new FileHandler(filePath.getValue(), true);
fileHandler.setFormatter(new HttpAccessLogFormatter(accessLogFormat));
fileHandler.setFormatter(new HttpAccessLogFormatter());
fileHandler.setLevel(Level.INFO);
httpAccessLogger.addHandler(fileHandler);
httpAccessLogger.setLevel(Level.INFO);
Expand All @@ -181,12 +168,4 @@ public void setHttpAccessLogHandler(BMap accessLogConfig) {
stdErr.println("ballerina: " + protocol + " access log enabled");
}
}

private HttpAccessLogFormat getAccessLogFormat(BMap accessLogConfig) {
BString logFormat = accessLogConfig.getStringValue(HTTP_LOG_FORMAT);
if (logFormat.getValue().equals(HTTP_LOG_FORMAT_JSON)) {
return HttpAccessLogFormat.JSON;
}
return HttpAccessLogFormat.FLAT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.ballerina.stdlib.http.api.logging.accesslog;

import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_ATTRIBUTES;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FORMAT;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FORMAT_JSON;

public class HttpAccessLogConfig {
private static final Set<String> EXCLUDED_ATTRIBUTES = new HashSet<>(List.of(
"http_referrer", "http_user_agent", "http_x_forwarded_for"
));
private static BMap accessLogConfig = null;

public static void initializeHttpAccessLogConfig(BMap accessLogConfigFromBallerina) {
accessLogConfig = accessLogConfigFromBallerina;
}

public static List<String> getCustomHeaders() {
List<String> attributes = getAccessLogAttributes();
if (attributes == null) {
return Collections.emptyList();
}

return attributes.stream()
.filter(attr -> attr.startsWith("http_") && !EXCLUDED_ATTRIBUTES.contains(attr))
.map(attr -> attr.substring(5))
.collect(Collectors.toList());
}

public static HttpAccessLogFormat getAccessLogFormat() {
BString logFormat = accessLogConfig.getStringValue(HTTP_LOG_FORMAT);
if (logFormat.getValue().equals(HTTP_LOG_FORMAT_JSON)) {
return HttpAccessLogFormat.JSON;
}
return HttpAccessLogFormat.FLAT;
}

public static List<String> getAccessLogAttributes() {
BArray logAttributes = accessLogConfig.getArrayValue(HTTP_LOG_ATTRIBUTES);
if (logAttributes != null) {
return Arrays.stream(logAttributes.getStringArray())
.collect(Collectors.toList());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* under the License.
*/

package io.ballerina.stdlib.http.transport.contractimpl.common.accesslog;
package io.ballerina.stdlib.http.api.logging.accesslog;

public enum HttpAccessLogFormat {
FLAT, JSON
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.stdlib.http.api.logging.accesslog;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class HttpAccessLogMessage {
private String ip;
private Calendar dateTime;
private String requestMethod;
private String requestUri;
private String scheme;
private int status;
private long requestBodySize;
private long responseBodySize;
private long requestTime;
private String httpReferrer;
private String httpUserAgent;
private String httpXForwardedFor;
private String host;
private int port;
private Map<String, String> customHeaders;

public HttpAccessLogMessage() {
this.customHeaders = new HashMap<>();
}

public HttpAccessLogMessage(String ip, Calendar dateTime, String requestMethod, String requestUri, String scheme,
int status, long responseBodySize, String httpReferrer, String httpUserAgent) {
this.ip = ip;
this.dateTime = dateTime;
this.requestMethod = requestMethod;
this.requestUri = requestUri;
this.scheme = scheme;
this.status = status;
this.responseBodySize = responseBodySize;
this.httpReferrer = httpReferrer;
this.httpUserAgent = httpUserAgent;
this.customHeaders = new HashMap<>();
}

public Calendar getDateTime() {
return dateTime;
}

public void setDateTime(Calendar dateTime) {
this.dateTime = dateTime;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public String getRequestMethod() {
return requestMethod;
}

public void setRequestMethod(String requestMethod) {
this.requestMethod = requestMethod;
}

public String getRequestUri() {
return requestUri;
}

public void setRequestUri(String requestUri) {
this.requestUri = requestUri;
}

public String getScheme() {
return scheme;
}

public void setScheme(String scheme) {
this.scheme = scheme;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public long getRequestBodySize() {
return requestBodySize;
}

public void setRequestBodySize(Long requestBodySize) {
this.requestBodySize = requestBodySize;
}

public long getResponseBodySize() {
return responseBodySize;
}

public void setResponseBodySize(Long responseBodySize) {
this.responseBodySize = responseBodySize;
}

public long getRequestTime() {
return requestTime;
}

public void setRequestTime(Long requestTime) {
this.requestTime = requestTime;
}

public String getHttpUserAgent() {
return httpUserAgent;
}

public String getHttpReferrer() {
return httpReferrer;
}

public void setHttpReferrer(String httpReferrer) {
this.httpReferrer = httpReferrer;
}

public void setHttpUserAgent(String httpUserAgent) {
this.httpUserAgent = httpUserAgent;
}

public String getHttpXForwardedFor() {
return httpXForwardedFor;
}

public void setHttpXForwardedFor(String httpXForwardedFor) {
this.httpXForwardedFor = httpXForwardedFor;
}

public String getHost() {
return this.host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return this.port;
}

public void setPort(int port) {
this.port = port;
}

public Map<String, String> getCustomHeaders() {
return customHeaders;
}

public void setCustomHeaders(Map<String, String> customHeaders) {
this.customHeaders = customHeaders;
}

public void putCustomHeader(String headerKey, String headerValue) {
this.customHeaders.put(headerKey, headerValue);
}
}
Loading

0 comments on commit 59ca393

Please sign in to comment.