java - AspectJ: How to log the successful completion of a method? -


so working aspectj refactor program remove logging calls main classes. instead, logging calls occur via aspects. far, here's i've done successfully:

  • catching exceptions. since have exception handling pretty unified begin (exceptions propagate call stack until reach controller, caught), wasn't difficult. using handle(exception) i've got every catch block tied aspect log exception before catch block executes.

  • debug information logging. basic things entering particular method, etc. again, relatively simple using before() and, sometimes, args() or thisjoinpoint.getargs() capture parameters.

the final thing need do, though, log successful completion of key methods. here minor example, of block of code exists in program, can better explain meaning here:

private static void loaddefaultproperties(){     defaultproperties = new properties();      try {         defaultproperties.load(                 main.class.getclassloader().getresourceasstream(                         "default.properties"));          if(defaultproperties.size() == 0){             throw new ioexception();         }          //the current logging line needs moved aspect         logger.logp(level.info, main.class.getname(),                 "loaddefaultproperties()",                 "default properties file loaded successfully");      }     catch (ioexception ex){         //this exception logged using handle(exception) in aspect         displayexceptiondialog(ex);     } } 

so, method loads default properties classpath. if fails, ioexception thrown. however, if exception not thrown, , performs task successfully, want log well.

this particular example relatively minor item wouldn't huge deal miss, style used everywhere in application.

how configure pointcut/advice in aspectj run after method completes, , recognize successfully?

ps. i've considered propagating exception further call stack. allow me use simple after() returning(object o) advice accomplish this. if exception thrown, advice not run because method won't return properly.

the problem there aren't many levels higher in call stack. particular method in example called directly main method following program's initialization. in controller class, there's 1 level higher in call stack of methods there well.

i guess this, want see if there's option first, rather pool of exceptions in 1 place.

you can use pointcut wrap in call stack, including main():

public aspect wrapmain {     pointcut mainmethod() : execution(public static void main(string[]));     before() : mainmethod() {         system.out.println("starting application...");     }     after() : mainmethod() {         system.out.println("terminating application...");     } } 

if generating aspects using java, can use @afterreturning , @afterthrowing annotations manage successful or exceptional cases.

@pointcut("execution(your-method-signature-here) public void yourpointcut(){}  @afterreturning(pointcut = "yourpointcut()", returning ="returnvalue") public void aftersuccessadvice(joinpoint jp, object returnvalue) {     // log here }  @afterthrowing(pointcut = "yourpointcut()", returning ="returnvalue") public void afterfailureadvice(joinpoint jp, object returnvalue) {     // log here } 

here's javaworld article examples explains logging method entry , exit.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -