1

I have created a jar file for Automatic logging using spring boot and AOP, now I have added the jar in other spring boot project, and jar is added in maven dependency, but it is not logging anything.

I am taking, base package name from property files, and accessing it using xml configuration, in my AOP project(jar that i created).

here is the AOP code

@Aspect
@Component
public class LoggingAdvice {    
    private LogDetails logDetails = new LogDetails();
    Logger log = LoggerFactory.getLogger(LoggingAdvice.class);  
    public Object logger(ProceedingJoinPoint pjp) throws Throwable {
        ObjectMapper mapper = new ObjectMapper();       
        Instant start = Instant.now();
        Object[] array = pjp.getArgs();
        Object object = pjp.proceed();
        Instant end = Instant.now();
        long timeTaken = Duration.between(start, end).toMillis();
        logDetails.setServiceName(pjp.getSignature().getName());
        logDetails.setTimeStamp(new SimpleDateFormat("mm:ss:SSS").format(new Date(timeTaken)));
        logDetails.setMessage(pjp.getTarget().getClass().toString() + ", Method:-> " + pjp.getSignature().getName()
                + ", Arguments:-> " + mapper.writeValueAsString(array));        
            log.info(mapper.writeValueAsString(logDetails));        
        return object;
    }
}

This is the xml file, in which i am getting base package from properties file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="LoggingAdvice"
        class="com.cerner.generic.logger.advice.LoggingAdvice"></bean>

    <aop:config>
        <aop:aspect id="logallaspect" ref="LoggingAdvice">
            <!-- @Around -->
            <aop:pointcut id="logAllMethodsAround"
                expression="within(${base.package}..*)" />
            <aop:around method="logger"
                pointcut-ref="logAllMethodsAround" />
            <!-- @After-Throwing -->
            <aop:after-throwing
                pointcut-ref="logAllMethodsAround" throwing="e"
                method="afterThrowing" />
        </aop:aspect>
    </aop:config>

</beans>

And this is how added this jar in, other project, basically it should produce logs, but it is not producing.

<dependency>
    <groupId>com.cerner.generic</groupId>
    <artifactId>logger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>runtime</scope>          
</dependency>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Priya Rani
  • 11
  • 1

1 Answers1

0

I Registered the LoggingAdvice bean as suggested by R.G in above comments. Here is the little modification that i did.

@SpringBootApplication(scanBasePackages =
  {"basePackageOfPatientApplication", "basePackageOfLoggingAdvice"}
)
public class PatientApplication {
    public static void main(String[] args) {
        SpringApplication.run(PatientApplication.class, args);
    }
}

And it worked for me!!

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Priya Rani
  • 11
  • 1