From 282e97edc6ed673e1f2add4e2a0df71a6988b8b8 Mon Sep 17 00:00:00 2001 From: Kinshuk Bairagi Date: Mon, 5 Aug 2024 16:32:01 +0530 Subject: [PATCH] Formatted AccessLog --- core/build.gradle | 1 + .../gjex/core/context/AccessLogContext.java | 36 +++++++++++++++++++ .../core/context/AccessLogContextTest.java | 23 ++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java create mode 100644 core/src/test/java/com/flipkart/gjex/core/context/AccessLogContextTest.java diff --git a/core/build.gradle b/core/build.gradle index 2ee48c1e..da93c786 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -66,6 +66,7 @@ dependencies { implementation libraries.grpc_stub implementation libraries.grpc_services implementation libraries.lombok + implementation 'org.apache.commons:commons-text:1.12.0' testImplementation libraries.junit4 diff --git a/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java b/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java new file mode 100644 index 00000000..9405cc46 --- /dev/null +++ b/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java @@ -0,0 +1,36 @@ +package com.flipkart.gjex.core.context; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.SneakyThrows; +import org.apache.commons.text.StringSubstitutor; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +@AllArgsConstructor +public class AccessLogContext { + String clientIp; + String resourcePath; + Integer contentLength; + Long responseTime; + Map headers; + + @SneakyThrows + Map getValueMap() { + Map params = new HashMap<>(); + for (Field field : this.getClass().getDeclaredFields()) { + params.put(field.getName(), field.get(this)); + } + for (Map.Entry entry : headers.entrySet()) { + params.put("headers." + entry.getKey(), entry.getValue()); + } + return params; + } + + public String format(String templateFormat) { + return StringSubstitutor.replace(templateFormat, getValueMap(), "{", "}"); + } +} diff --git a/core/src/test/java/com/flipkart/gjex/core/context/AccessLogContextTest.java b/core/src/test/java/com/flipkart/gjex/core/context/AccessLogContextTest.java new file mode 100644 index 00000000..9b62e3c6 --- /dev/null +++ b/core/src/test/java/com/flipkart/gjex/core/context/AccessLogContextTest.java @@ -0,0 +1,23 @@ +package com.flipkart.gjex.core.context; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +public class AccessLogContextTest { + + @Before + public void setUp() { + } + + private String template = "Formatted {clientIp} {resourcePath} {contentLength} {responseTime} {headers.x-header}"; + + @Test + public void testSimpleRender() { + AccessLogContext context = new AccessLogContext("127.0.0.1", "/path", 100, 1000L, Map.of("x-header", "value")); + String result = context.format(template); + Assert.assertEquals("Formatted 127.0.0.1 /path 100 1000 value", result); + } +}