Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AzeemMuzammil committed Jun 28, 2024
1 parent fd9eb2b commit 896e886
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogConfig;
Expand All @@ -37,6 +38,7 @@

import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_ACCESS_LOG;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_ACCESS_LOG_ENABLED;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_ATTRIBUTES;
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;
Expand Down Expand Up @@ -167,11 +169,18 @@ public void setHttpAccessLogHandler(BMap accessLogConfig) {
}

BString logFormat = accessLogConfig.getStringValue(HTTP_LOG_FORMAT);
if (!(logFormat.getValue().equals(HTTP_LOG_FORMAT_JSON) || logFormat.getValue().equals(HTTP_LOG_FORMAT_FLAT))) {
if (logFormat != null &&
!(logFormat.getValue().equals(HTTP_LOG_FORMAT_JSON) ||
logFormat.getValue().equals(HTTP_LOG_FORMAT_FLAT))) {
stdErr.println("WARNING: Unsupported log format '" + logFormat.getValue() +
"'. Defaulting to 'flat' format.");
}

BArray logAttributes = accessLogConfig.getArrayValue(HTTP_LOG_ATTRIBUTES);
if (logAttributes != null && logAttributes.getLength() == 0) {
accessLogsEnabled = false;
}

if (accessLogsEnabled) {
System.setProperty(HTTP_ACCESS_LOG_ENABLED, "true");
stdErr.println("ballerina: " + protocol + " access log enabled");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public void initializeHttpAccessLogConfig(BMap accessLogConfig) {

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

return attributes.stream()
.filter(attr -> attr.startsWith("http_") && !excludedAttributes.contains(attr))
Expand All @@ -85,6 +82,6 @@ public List<String> getAccessLogAttributes() {
.collect(Collectors.toList());
}
}
return null;
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public static void log(HttpAccessLogMessage inboundMessage, List<HttpAccessLogMe
private static String formatAccessLogMessage(HttpAccessLogMessage inboundMessage,
List<HttpAccessLogMessage> outboundMessages, HttpAccessLogFormat format,
List<String> attributes) {

Map<String, String> inboundMap = mapAccessLogMessage(inboundMessage, format, attributes);
if (format == HttpAccessLogFormat.FLAT) {
Map<String, String> inboundMap = mapAccessLogMessage(inboundMessage, format, attributes);
String inboundFormatted = inboundMap.values().stream()
.filter(Objects::nonNull)
.collect(Collectors.joining(" "));
Expand All @@ -80,7 +81,6 @@ private static String formatAccessLogMessage(HttpAccessLogMessage inboundMessage
return inboundFormatted;
}
} else {
Map<String, String> inboundMap = mapAccessLogMessage(inboundMessage, format, attributes);
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();

Expand Down Expand Up @@ -111,7 +111,7 @@ private static Map<String, String> mapAccessLogMessage(HttpAccessLogMessage http
Map<String, String> attributeValues = new LinkedHashMap<>();
allAttributes.forEach(attr -> attributeValues.put(attr, null));

if (attributes != null) {
if (!attributes.isEmpty()) {
attributes.forEach(attr -> {
attributeValues.put(attr, formatAccessLogAttribute(httpAccessLogMessage, format, attr));
});
Expand Down

0 comments on commit 896e886

Please sign in to comment.