Skip to content

Commit

Permalink
adds loggable in order service
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Apr 8, 2024
1 parent 4c821eb commit 5bd8d12
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void springBeanPointcut() {

@Pointcut(
"""
@within(com.example.inventoryservice.config.logging.Loggable)
|| @annotation(com.example.inventoryservice.config.logging.Loggable)
@within(com.example.catalogservice.config.logging.Loggable)
|| @annotation(com.example.catalogservice.config.logging.Loggable)
""")
public void applicationPackagePointcut() {
// pointcut definition
Expand Down
2 changes: 1 addition & 1 deletion order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.18.1</version>
<version>1.19.2</version>
<style>AOSP</style>
</googleJavaFormat>
<licenseHeader> <!-- specify either content or file, but not both -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/***
<p>
Licensed under MIT License Copyright (c) 2024 Raja Kolli.
</p>
***/

package com.example.inventoryservice.config.logging;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.logging.LogLevel;

public class LogWriter {

public static void write(Class<?> originClass, LogLevel logLevel, String message) {
Logger logger = LoggerFactory.getLogger(originClass);
switch (logLevel) {
case TRACE -> logger.trace(message);
case DEBUG -> logger.debug(message);
case INFO -> logger.info(message);
case WARN -> logger.warn(message);
case ERROR, FATAL -> logger.error(message);
default -> logger.warn("No suitable logLevel found");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***
<p>
Licensed under MIT License Copyright (c) 2021-2022 Raja Kolli.
Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli.
</p>
***/

Expand All @@ -13,8 +13,16 @@ Licensed under MIT License Copyright (c) 2021-2022 Raja Kolli.
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.springframework.boot.logging.LogLevel;

@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Loggable {}
public @interface Loggable {

boolean params() default true;

boolean result() default true;

LogLevel value() default LogLevel.DEBUG;
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
/***
<p>
Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli.
</p>
***/

package com.example.orderservice.config.logging;

import com.example.orderservice.utils.AppConstants;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.logging.LogLevel;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Aspect
@Component
public class LoggingAspect {

private final Logger log = LoggerFactory.getLogger(this.getClass());
private static final Logger log = LoggerFactory.getLogger(LoggingAspect.class);

private final Environment env;

Expand All @@ -40,8 +46,10 @@ public void springBeanPointcut() {
}

@Pointcut(
"@within(com.example.orderservice.config.logging.Loggable) || "
+ "@annotation(com.example.orderservice.config.logging.Loggable)")
"""
@within(com.example.orderservice.config.logging.Loggable)
|| @annotation(com.example.orderservice.config.logging.Loggable)
""")
public void applicationPackagePointcut() {
// pointcut definition
}
Expand All @@ -68,22 +76,76 @@ public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {

@Around("applicationPackagePointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
if (log.isDebugEnabled()) {
log.debug(
"Enter: {}.{}()",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName());
}
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
Loggable methodAnnotation = method.getAnnotation(Loggable.class);

Class<?> originClass = joinPoint.getTarget().getClass();
Loggable classAnnotation = originClass.getAnnotation(Loggable.class);

// get current log level

LogLevel logLevel =
methodAnnotation != null ? methodAnnotation.value() : classAnnotation.value();

// before
String methodName = method.getName();
LogWriter.write(originClass, LogLevel.INFO, methodName + "() start execution");

printParamsIfEnabled(
joinPoint,
methodSignature.getParameterNames(),
methodAnnotation,
originClass,
classAnnotation,
logLevel,
methodName);

long start = System.currentTimeMillis();
// Start method execution
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug(
"Exit: {}.{}(). Time taken: {} millis",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
end - start);

// show results
if (result != null) {
boolean printResponse =
methodAnnotation != null ? methodAnnotation.result() : classAnnotation.result();
if (printResponse) {
LogWriter.write(originClass, logLevel, methodName + "() Returned : " + result);
}
}

// print results
LogWriter.write(
originClass,
LogLevel.INFO,
methodName
+ "() finished execution and took ("
+ (end - start)
+ ") mills to execute");
return result;
}

private void printParamsIfEnabled(
ProceedingJoinPoint joinPoint,
String[] parameterNames,
Loggable methodAnnotation,
Class<?> originClass,
Loggable classAnnotation,
LogLevel logLevel,
String methodName) {
boolean printParams =
methodAnnotation != null ? methodAnnotation.params() : classAnnotation.params();

if (printParams && !ObjectUtils.isEmpty(joinPoint.getArgs())) {
List<String> stringArrayList = new ArrayList<>();
Object[] args = joinPoint.getArgs();

for (int i = 0; i < args.length; i++) {
stringArrayList.add(parameterNames[i] + " : " + args[i]);
}
String argsString = String.join(", ", stringArrayList);
LogWriter.write(originClass, logLevel, methodName + "() args :: -> " + argsString);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.
package com.example.orderservice.services;

import com.example.orderservice.config.ApplicationProperties;
import com.example.orderservice.config.logging.Loggable;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
@Loggable
public class CatalogService {

private final Logger log = LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.

package com.example.orderservice.services;

import com.example.orderservice.config.logging.Loggable;
import java.util.List;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;

// @HttpExchange("lb://catalog-service/")
// @HttpExchange("http://localhost:18080/catalog-service")
// @HttpExchange(url = "${application.catalog-service-url}")
@Loggable
public interface CatalogServiceProxy {

@GetExchange("/api/catalog/exists")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.
package com.example.orderservice.services;

import com.example.common.dtos.OrderDto;
import com.example.orderservice.config.logging.Loggable;
import com.example.orderservice.utils.AppConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,6 +16,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.
import org.springframework.stereotype.Service;

@Service
@Loggable
public class KafkaOrderProducer {

private final Logger log = LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Licensed under MIT License Copyright (c) 2022-2024 Raja Kolli.

package com.example.orderservice.services;

import com.example.orderservice.config.logging.Loggable;
import com.example.orderservice.model.request.OrderItemRequest;
import com.example.orderservice.model.request.OrderRequest;
import java.math.BigDecimal;
Expand All @@ -16,6 +17,7 @@ Licensed under MIT License Copyright (c) 2022-2024 Raja Kolli.
import org.springframework.stereotype.Service;

@Service
@Loggable
public class OrderGeneratorService {

private static final int NUM_ORDERS = 10_000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.
package com.example.orderservice.services;

import com.example.common.dtos.OrderDto;
import com.example.orderservice.config.logging.Loggable;
import com.example.orderservice.utils.AppConstants;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,6 +22,7 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.
import org.springframework.stereotype.Service;

@Service
@Loggable
public class OrderKafkaStreamService {

private final Logger log = LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.
package com.example.orderservice.services;

import com.example.common.dtos.OrderDto;
import com.example.orderservice.config.logging.Loggable;
import com.example.orderservice.entities.OrderStatus;
import com.example.orderservice.repositories.OrderRepository;
import com.example.orderservice.utils.AppConstants;
Expand All @@ -15,6 +16,7 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.
import org.springframework.stereotype.Service;

@Service
@Loggable
public class OrderManageService {

private static final String ACCEPT = "ACCEPT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli.
package com.example.orderservice.web.controllers;

import com.example.common.dtos.OrderDto;
import com.example.orderservice.config.logging.Loggable;
import com.example.orderservice.exception.ProductNotFoundException;
import com.example.orderservice.model.request.OrderRequest;
import com.example.orderservice.model.response.OrderResponse;
Expand Down Expand Up @@ -40,6 +41,7 @@ Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli.
@RestController
@RequestMapping("/api/orders")
@Validated
@Loggable
public class OrderController implements OrderApi {

private static final Logger log = LoggerFactory.getLogger(OrderController.class);
Expand Down

0 comments on commit 5bd8d12

Please sign in to comment.