0

I am trying to create a generic Logger which would be a small standalone code. Different applications can use this Logger for logging. Let's say, there are two different codebases- CB1 and CB2.
CB1 needs to capture all public methods of all classes under package- CB1/a/b/c
CB2 needs to capture all public methods of all classes under package- CB2/d/e/f

Now, what I have done till now is as below-
A new codebase, say LogUtility which has an Aspect GenericLogger-

public class GenericLogger {
   public Object aroundLog(ProceedingJoinPoint jp) {
      //logging code goes here
   }
}

in some_context.xml-

<aop:config>

    <aop:aspect id="loggerAspect" ref="myLogger">

        <aop:pointcut id="sample" expression="${logger.pointcutExpr}" />

        <aop:around method="aroundLog" pointcut-ref="sample" />

    </aop:aspect>

</aop:config>

If CB1 needs to use this LogUtility, CB1 will add LogUtility to its pom/ivy dependency and provide the value of ${logger.pointcutExpr} via a property file at application startup time.

So, it works fine this way for CB1, CB2,...

The only disadvantage of this approach that I think is the long list in the properties file which has the single key i.e.logger.pointcutExpr Good thing is, whenever any codebase needs to change it can just add a new pointcut in its own properties file. So a single Aspect can serve multiple codebases.

Earlier, I was trying to do something like this,

@Aspect
@Component
public class GenericLogger {

   @Around(<can't make this dynamic>)
   public object aroundLog(ProceedingJoinPoint jp) {
      //logging code goes here
   }
}

The problem with the above is that values passed to any annotation must be final, so can't go with this approach.

I was wondering if there is anything that can be done to achieve this on the fly.
1. Any way in which different codebases can provide the value of the key logger.pointcutExpr without explicitly creating a properties file.
2. Or is it possible to register pointcut with an Aspect on the fly?

I've been googling a lot on this and I'm finding basic AOP tutorials everywhere. I think to do something like this I need to dig deeper in AspectJ along with Spring AOP. I found below links-
https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-choosing how to apply spring aop for legacy code by taking pointcut as input from user https://eclipse.org/aspectj/doc/next/devguide/ltw-configuration.html

I have basic knowledge of AspectJ, what I'm looking for could be silly.

5
  • I know this one is old, but in my feed it was listed as new, so sorry if the topic is long forgotten. If you want the pointcut in code instead of in a config file, have you thought about making the aspect's pointcut abstract and implementing one sub-aspect per code base with a concrete pointcut specific to each code base?
    – kriegaex
    Dec 28, 2017 at 7:12
  • Interesting.. haven't thought about this but in that case, all sub-aspects would have to be at one place i.e. in one codebase? Dec 28, 2017 at 7:41
  • No, each sub-aspect could reside in the code base it relates to or, optionally, in its own module (one per code base), but deployed together with the core aspect library and the specific code base.
    – kriegaex
    Dec 28, 2017 at 9:53
  • Did you ever figure this out?
    – mad_fox
    Dec 10, 2018 at 15:16
  • @mad_fox not really, AspectJ might have something but didn't look into that. I solved my problem using XML config with Spring AOP. Dec 20, 2018 at 7:30

1 Answer 1

0

You can make what is in the Around method dynamic (sort of) by using a static final expression.

But I would suggest something else.

You can definitely do this sort of thing in AspectJ, it's just that you will need to firstly think of the Aspect as describing what expression will execute for the super set of all your cases. Then within the aspect define the behaviour you are wanting to achieve. So for instance you can use Object target = joinPoint.getTarget(); to get the target (class which was executing the method), and then use String canonicalName = taget.getCanonicalName() which will include the package in the name, and then you can do stuff like:

if(getCanonicalName.contains("some/package") {
    System.out.println("You can do better than this if statement");
}

And make whatever if statements you need to differentiate between the various packages which are contained in the canonical name. That way you can have greater control over what happens for each package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.