Jun 6th, 2012, 03:14 AM
Pointcut to log4j.info/debug/error calls.
I have no much experience in AOP, so any help will be useful.
I need to modify log messages produced by my service - add some extra data, which i can get only from caller of service.
Simply it looks like this : Caller -> service method -> logger.info.
I thought that if I could wrap logger.info call in Around aspect, then i could suppress original call or modify it's arguments to add what i need. But so long i can't figure out pointcut to match such call.
Ideal pointcut would be something like "any call of org.apache.log4j.Logger.info(debug/error, etc) where caller is service from my package", but, so far i didn't seen any example of second part of my condition (about caller).
I think, it's not big problem, if i can get this info inside of aspect. But, as i told, i can't even get pointcut to "any call of org.apache.log4j.Logger.info".
Here http://forum.springsource.org/showth...261#post357261 i found that "spring AOP works only on spring beans". I didn't seen it in docs, but, it seems so.
So is there any way to solve this task using AOP, maybe using different AOP engine?
Jun 6th, 2012, 03:44 AM
I found that AspectJ can "advise fine-grained objects that aren't created by the Spring IoC container", http://static.springsource.org/sprin...e/aspectj.html
Jun 6th, 2012, 03:50 AM
Yes you'll need to use full-blown AspectJ (either ctw or ltw) as your AOP engine. This overcomes Spring AOP proxy-based strategy limitations (only Spring beans are advised, internal calls are not advised, etc.). Please refer to appropriate section of the Spring Reference Guide.
Jun 6th, 2012, 04:09 AM
Why so complex? I suggest you take a look at the diagnostic context in which you can add attributes and you can use that to add extra messages/information to your logfile. That way you can simply intercept the service method, set the attributes on the NDC/MDC . IMHO much easier and less intrusive.
Jun 6th, 2012, 04:12 AM
Can u post link to example of such method?
Jun 6th, 2012, 04:21 AM
Of what method?! I suggest a little googling and searching on the forum as that has been answered before.
A blog post explaining log4j and mdc the post after that sets stuff in the mdc. You simple need to write an aspect which sets the needed things in the MDC and write your pattern layout in such a way that that information is included.
Jun 6th, 2012, 04:22 AM
#4: maybe it's really to complex solution for this task. But method of advising third-party libraries calls would be useful to me. I had few situations when it could be fine solution.
Jun 6th, 2012, 04:25 AM
I NOWHERE mentioned the fact that the solution you choose cannot be useful I only mentioned that using a fullblown aspectj compile time weaving solution is a bit of an overkill to add some additional information to the log4j message. Compile time weaving etc. can be useful and saved me also in some of the more darker corners of hibernate bugs (which we fixed/worked around with this) but for this solution it is overkill IMHO...
Jun 6th, 2012, 04:52 AM
Many thanks for sharing your experience. I'm really grateful. I'll try your suggestion first.
Last edited by ru5t; Jun 6th, 2012 at 05:03 AM.
Tags for this Thread