Results 1 to 9 of 9

Thread: Pointcut to log4j.info/debug/error calls.

  1. #1
    Join Date
    Jun 2012
    Posts
    5

    Default 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?

  2. #2
    Join Date
    Jun 2012
    Posts
    5

    Default

    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

  3. #3
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jun 2012
    Posts
    5

    Default

    Can u post link to example of such method?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Jun 2012
    Posts
    5

    Default

    #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.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Jun 2012
    Posts
    5

    Default

    @Marten Deinum
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •