开发者

Traditional logging vs AOP logging

I'm starting this new project and we are thrashing out our logging/debugging approach and I wanted to put the question to the rest of you on SO, given

private final static Logger logger = LoggerFactory.getLogger(getClass());
...
public void doSumething(){
...
if(logger.isDebugEnabled())
    logger.debug("...");
}

or

 @After("execution(* *.doSomething())")
    public void logAfter(JoinPoint jp){
        logger.debug("...");
    }

Is the AOP approach really any better开发者_开发技巧 than using the traditional approach? Or does AOP excel in a particular use case for logging/profiling?


For performance, AOP approach certainly has a little overhead against the traditional one.

One of the many strengths of AOP is that it allows you to separate your non-business concerns from your business logic. It also helps you in mundane tasks such putting a logging logic in each of your method or place a try-catch statement on each method.

I think the real issue is that if the performance overhead (in my experience which is only minimal) would compensate on the mundane tasks that you have to go through while developing.

A friend told me before that it's better to have a slower app that is maintainable and scalable than having a faster app that would give you hell in maintenance. Slowness could be compensated in many ways, such as upgrading hardware and etc.

This is just my two cents, hope this helps.


I dont think these should be viewed as mutually exclusive alternatives.

I think AOP is great for tracing (i.e. logging method entry/exit and parameter values).

I still use regular logging as well:

  1. For Info/Warning/Error Messages
  2. For debug messages during development to see the value of certain variables or to see which if/then path is used etc.


I'm quite found of the aspect oriented approach. It feels right to separate logging from logic. I'm not sure of the performance overhead though.

Even if you decide not to go with AOP there are better ways of doing logging than this:

if(logger.isDebugEnabled())

Have a look at log4j which will enable you to change log levels, different appenders and a whole lot of other things.


I agree with @daxsorbito answer - and wanted to build on top of that.

  • The cross cutting advice can be configured using config files that way you can have a different aop advice for production logging. This may improve performance.

  • AOP Can reduce a lot of code clutter and encourage developers to focus on main task.

  • A lot of debugging becomes easier if the code has sufficient logging. AOP makes it independent of a developer. Some developer may not follow a discipline of logging enough. Or may feel it unnecessary.

  • Log file has consistent messages. (no more "you say potato, I say batata... :) " ) - Each developer may log it different style. However if we AOP-fy it you can make it consistent across application and put a lot of good analytic on top of it easily.

  • This also encourages you to write small reusable (and even better if those are sometimes testable) methods that can be easily debugged.

  • Sometimes, This will also encourage you to follow consistent naming conventions since it will make it easier to apply advices for logging those methods during debugging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜