Skip to content

Commit

Permalink
Merge pull request #23 from RHoKAustralia/adding-aspects
Browse files Browse the repository at this point in the history
Added Spring AOPs for application loggings
  • Loading branch information
amitkashyap01 authored Dec 13, 2018
2 parents 79b5046 + fd668a5 commit 1bb9691
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
10 changes: 8 additions & 2 deletions backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand All @@ -15,7 +16,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
Expand All @@ -39,6 +40,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.onemda.onemdabackend.aop;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class ExceptionLoggingAspect {

Logger LOGGER = LoggerFactory.getLogger(ExceptionLoggingAspect.class);


@AfterThrowing(pointcut="com.onemda.onemdabackend.aop.SystemArchitecture.Repository() ||"
+ "com.onemda.onemdabackend.aop.SystemArchitecture.Service()",
throwing="ex")
public void logException(Exception ex) {
LOGGER.error("Exception Occured: "+ex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.onemda.onemdabackend.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
public class PerformanceAspect {

Logger LOGGER = LoggerFactory.getLogger(PerformanceAspect.class);

@Around("com.onemda.onemdabackend.aop.SystemArchitecture.Repository()")
public void logPerformance(ProceedingJoinPoint joinPoint) throws Throwable {
String methodInformation = joinPoint.getStaticPart().getSignature().toString();

StopWatch stopWatch = new StopWatch(methodInformation);
stopWatch.start();

try {
joinPoint.proceed();
} catch (Exception e) {
// TODO: handle exception
}finally {
stopWatch.stop();
LOGGER.info(stopWatch.shortSummary());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.onemda.onemdabackend.aop;

import org.aspectj.lang.annotation.Pointcut;

public class SystemArchitecture {

//Any class annotated with @Repository
@Pointcut("execution(* (@org.springframework.stereotype.Repository *).*(..))")
public void Repository() {

}

@Pointcut("execution(* (@org.springframework.stereotype.Service *).*(..))")
public void Service() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.onemda.onemdabackend.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TracingAspect {

Logger LOGGER = LoggerFactory.getLogger(TracingAspect.class);

@Before("com.onemda.onemdabackend.aop.SystemArchitecture.Repository() ||"
+ "com.onemda.onemdabackend.aop.SystemArchitecture.Service()")
public void logTheEntry(JoinPoint joinPoint ) {
LOGGER.info("Entering into : "+joinPoint.getStaticPart().getSignature().toString());
}

@After("com.onemda.onemdabackend.aop.SystemArchitecture.Repository() ||"
+ "com.onemda.onemdabackend.aop.SystemArchitecture.Service()")
public void logTheExit(JoinPoint joinPoint ) {
LOGGER.info("Exiting from : "+joinPoint.getStaticPart().getSignature().toString());
}
}

0 comments on commit 1bb9691

Please sign in to comment.