Skip to content

Commit 2c4a386

Browse files
authored
Common-Log format for access.log (#95)
1 parent efef954 commit 2c4a386

File tree

8 files changed

+17
-13
lines changed

8 files changed

+17
-13
lines changed

core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.lang.reflect.Field;
99
import java.time.Instant;
1010
import java.time.LocalDateTime;
11+
import java.time.OffsetDateTime;
1112
import java.time.ZoneId;
13+
import java.time.format.DateTimeFormatter;
1214
import java.util.HashMap;
1315
import java.util.Map;
1416
import java.util.function.Supplier;
@@ -58,7 +60,7 @@ private Map<String,Object> getValueMap() {
5860
}
5961

6062
if (requestTime != null) {
61-
String localDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(requestTime), ZoneId.systemDefault()).toString();
63+
String localDate = OffsetDateTime.ofInstant(Instant.ofEpochMilli(requestTime), ZoneId.systemDefault()).format(DateTimeFormatter.ISO_DATE_TIME);
6264
params.put("request-time", localDate);
6365
}
6466
params.put("thread", Thread.currentThread().getName());

core/src/main/java/com/flipkart/gjex/core/filter/grpc/AccessLogGrpcFilter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public void doProcessRequest(R req, RequestParams<Metadata> requestParamsInput)
8181
accessLogContextBuilder = AccessLogContext.builder()
8282
.requestTime(startTime)
8383
.clientIp(requestParamsInput.getClientIp())
84-
.resourcePath(requestParamsInput.getResourcePath());
84+
.resourcePath(requestParamsInput.getResourcePath())
85+
.method(requestParamsInput.getMethod());
8586

8687
Map<String, String> headers = requestParamsInput.getMetadata().keys().stream()
8788
.collect(Collectors.toMap(String::toLowerCase, key ->
@@ -92,7 +93,7 @@ public void doProcessRequest(R req, RequestParams<Metadata> requestParamsInput)
9293
}
9394

9495
/**
95-
* Placeholder method for processing response headers. Currently does not perform any operations.
96+
* Placeholder method for processing response headers. Currently, does not perform any operations.
9697
*
9798
* @param responseHeaders The metadata associated with the gRPC response.
9899
*/

core/src/main/java/com/flipkart/gjex/core/filter/grpc/GrpcFilterConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ public class GrpcFilterConfig {
3434
private boolean enableAccessLogs = true;
3535

3636
@JsonProperty("accessLogFormat")
37-
private String accessLogFormat = "{clientIp} {resourcePath} {responseStatus} {contentLength} {responseTime}";
37+
private String accessLogFormat = "{clientIp} - [{request-time}] \"{method} {resourcePath}\" {responseStatus} {contentLength} {responseTime}";
3838
}

core/src/main/java/com/flipkart/gjex/core/filter/http/HttpFilterConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public class HttpFilterConfig {
3434
private boolean enableAccessLogs = true;
3535

3636
@JsonProperty("accessLogFormat")
37-
private String accessLogFormat = "{clientIp} \"{method} {resourcePath} {protocol}\" {responseStatus} {contentLength} {responseTime}";
37+
private String accessLogFormat = "{clientIp} - [{request-time}] \"{method} {resourcePath} {protocol}\" {responseStatus} {contentLength} {responseTime} \"{referer}\" \"{userAgent}\"";
3838

3939
}

examples/src/main/java/com/flipkart/gjex/examples/helloworld/service/GreeterService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private void invokeGrpcCall(HelloRequest req, HelloReply reply) {
126126

127127
@Override
128128
@Timed // the Timed annotation for publishing JMX metrics via MBean
129-
@MethodFilters({LoggingFilter.class, AuthFilter.class}) // Method level filters
129+
@MethodFilters({LoggingFilter.class}) // Method level filters
130130
@Traced(withSamplingRate = 0.0f) // Start a new Trace or participate in a Client-initiated distributed trace
131131
public StreamObserver<Ping> pingPong(StreamObserver<Pong> responseObserver) {
132132

examples/src/main/resources/hello_world_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Grpc:
33
server.executorThreads : 4
44
filterConfig:
55
enableAccessLogs: true
6-
accessLogFormat: "{clientIp} {resourcePath} {contentLength} {responseStatus} {responseTime}"
6+
accessLogFormat: '{clientIp} - [{request-time}] "{method} {resourcePath} GRPC" {responseStatus} {contentLength} {responseTime} "-" "-"'
77

88
Dashboard:
99
service.port: 9999

examples/src/main/resources/log4j2.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Configuration shutdownHook="disable">
33
<Properties>
4-
<Property name="log-path">logs</Property>
4+
<Property name="log-path">/tmp/logs</Property>
55
</Properties>
66
<Appenders>
77
<Console name="console-log" target="SYSTEM_OUT">
88
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{5} %X - %msg%n"/>
99
</Console>
10-
<Console name="ACCESS-LOG" target="SYSTEM_OUT">
11-
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{5} %X - %msg%n"/>
12-
</Console>
10+
<File name="access-log" fileName="${log-path}/access.log">
11+
<PatternLayout pattern="%msg%n"/>
12+
</File>
1313
</Appenders>
1414
<Loggers>
15-
<Logger name="io.netty.channel" level="info" additivity="false">
16-
<AppenderRef ref="ACCESS-LOG"/>
15+
<Logger name="ACCESS-LOG" level="info" additivity="false">
16+
<AppenderRef ref="access-log"/>
1717
</Logger>
1818
<Logger name="io.netty.channel" level="error" additivity="false">
1919
<AppenderRef ref="console-log"/>

guice/src/main/java/com/flipkart/gjex/grpc/interceptor/FilterInterceptor.java

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public void sendHeaders(final Metadata responseHeaders) {
135135
RequestParams requestParams = RequestParams.builder()
136136
.clientIp(getClientIp(call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)))
137137
.resourcePath(call.getMethodDescriptor().getFullMethodName().toLowerCase())
138+
.method(call.getMethodDescriptor().getType().name())
138139
.metadata(headers)
139140
.build();
140141

0 commit comments

Comments
 (0)