File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Logging Implementation using Spring AOP or AspectJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Logging Implementation using Spring AOP or AspectJ" Watch "Logging Implementation using Spring AOP or AspectJ" New topic

Logging Implementation using Spring AOP or AspectJ

Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
HI ranchers,

I am currently working on a web project which uses struts for MVC, spring for IOC. We have used log4j for logging as usual.

But i want to remove that logging concern which is in each and every method, and centralize it using Aspect oriented programming.

Even based on the exception i want to log a particular level like if ValidationError log ERROR and if DBException log FATAL.

I want to incorporate AOP logging in all the business classes and database access classes. can this be achieved using AOP?

For AOP i have spring AOP and AspectJ. Please tell me the advantages over each other. so that using one of it adds some value to my application without any performance overhead.

Please share some urls...

Thanks for your time in advance.

Vivek K Singh
Ranch Hand

Joined: Dec 22, 2009
Posts: 85

Well I see two things here:

1) Can you use AOP for logging ? Well yes you can use AOP for implementing cross cutting concerns like Logging, security but under the covers you will still be using your log4j for actually adding lines to your log files.
Also, IMHO you will be better off using both log4j directly and spring AOP for logging. Just choose what you need to implement where.

2) Is Spring AOP better or AspectJ : This is a passage from Spring online documentation Documentation section 6.1.2 Hope it solves your problem.

Spring AOP capabilities and goals

Spring AOP's approach to AOP differs from that of most other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable); it is rather to provide a close integration between AOP implementation and Spring IoC to help solve common problems in enterprise applications.

Thus, for example, the Spring Framework's AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured using normal bean definition syntax (although this allows powerful "autoproxying" capabilities): this is a crucial difference from other AOP implementations. There are some things you cannot do easily or efficiently with Spring AOP, such as advise very fine-grained objects (such as domain objects typically): AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in J2EE applications that are amenable to AOP.

Spring AOP will never strive to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks like Spring AOP and full-blown frameworks such as AspectJ are valuable, and that they are complementary, rather than in competition. Spring 2.0 seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP to be catered for within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API: Spring AOP remains backward-compatible.

I agree. Here's the link:
subject: Logging Implementation using Spring AOP or AspectJ
jQuery in Action, 3rd edition