-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixes Access Log not showing when error is thrown - Support for pattern based access log
- Loading branch information
Showing
19 changed files
with
427 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.flipkart.gjex.core.context; | ||
|
||
|
||
import lombok.Builder; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.text.StringSubstitutor; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents the context for access logs, encapsulating details such as client IP, resource path, | ||
* content length, response time, response status, and headers. This class provides functionality | ||
* to format these details into a string using a template. | ||
*/ | ||
@Builder | ||
public class AccessLogContext { | ||
String clientIp; | ||
String resourcePath; | ||
Integer contentLength; | ||
Long responseTime; | ||
Integer responseStatus; | ||
@Builder.Default | ||
Map<String,String> headers = new HashMap<>(); | ||
|
||
|
||
/** | ||
* Retrieves a map of field names to their values for the current instance. | ||
* This includes the fields of the class and the headers map, as well as the current thread name. | ||
* | ||
* @return A map containing field names and their corresponding values. | ||
* @throws IllegalAccessException if the field is not accessible. | ||
*/ | ||
@SneakyThrows | ||
private Map<String,Object> getValueMap() { | ||
Map<String, Object> params = new HashMap<>(); | ||
for (Field field : this.getClass().getDeclaredFields()) { | ||
field.setAccessible(true); | ||
params.put(field.getName(), field.get(this)); | ||
} | ||
for (Map.Entry<String, String> entry : headers.entrySet()) { | ||
params.put("headers." + entry.getKey(), entry.getValue()); | ||
} | ||
params.put("thread", Thread.currentThread().getName()); | ||
return params; | ||
} | ||
|
||
/** | ||
* Formats the access log context into a string based on the provided template. | ||
* The template can include placeholders for the field names, which will be replaced | ||
* with their corresponding values. | ||
* | ||
* @param templateFormat The template string containing placeholders for field names. | ||
* @return The formatted string with placeholders replaced by field values. | ||
*/ | ||
public String format(final String templateFormat) { | ||
return StringSubstitutor.replace(templateFormat, getValueMap(), "{", "}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.