How to Enforce a common log format using Spring AOP. Want to append a string e.g. service name in each logger





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Tried below example but it's not working with spring.Getting err in editor like "call pointcut designator isn't supported by Spring".



https://dzone.com/articles/enforcing-common-log-format



Any code example would be appreciated.










share|improve this question























  • Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer blog.trifork.com/2013/06/06/…

    – Abinash Pattnaik
    Nov 29 '18 at 6:25




















1















Tried below example but it's not working with spring.Getting err in editor like "call pointcut designator isn't supported by Spring".



https://dzone.com/articles/enforcing-common-log-format



Any code example would be appreciated.










share|improve this question























  • Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer blog.trifork.com/2013/06/06/…

    – Abinash Pattnaik
    Nov 29 '18 at 6:25
















1












1








1


1






Tried below example but it's not working with spring.Getting err in editor like "call pointcut designator isn't supported by Spring".



https://dzone.com/articles/enforcing-common-log-format



Any code example would be appreciated.










share|improve this question














Tried below example but it's not working with spring.Getting err in editor like "call pointcut designator isn't supported by Spring".



https://dzone.com/articles/enforcing-common-log-format



Any code example would be appreciated.







java spring aspectj spring-aop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 15:41









Abinash PattnaikAbinash Pattnaik

91




91













  • Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer blog.trifork.com/2013/06/06/…

    – Abinash Pattnaik
    Nov 29 '18 at 6:25





















  • Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer blog.trifork.com/2013/06/06/…

    – Abinash Pattnaik
    Nov 29 '18 at 6:25



















Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer blog.trifork.com/2013/06/06/…

– Abinash Pattnaik
Nov 29 '18 at 6:25







Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer blog.trifork.com/2013/06/06/…

– Abinash Pattnaik
Nov 29 '18 at 6:25














3 Answers
3






active

oldest

votes


















0














There are many ways to do this, here is 1.



1) Create an annotation to add to classes/methods that need logging added:



@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecution {
}


2) Create an aspect to do the logging:



@Aspect
@Component
public class LogAspect {

private List<String> messages = new ArrayList<>();

@Around("@annotation(hello.LogExecution)")
public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

Object proceed = null;
try {
// log before
proceed = joinPoint.proceed();
// log after
}
catch (Exception e) {
// log exception
throw e;
}
return proceed;
}
}


Working example here






share|improve this answer
























  • Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

    – Abinash Pattnaik
    Nov 27 '18 at 6:45



















0














It sounds like you don't have Aspect J correctly configured.



See the answer to this question for the correct setup:



Spring + AspectJ weaving for java 8 using aspectj-maven-plugin



After that, spring should recognize your pointcuts






share|improve this answer
























  • This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

    – kriegaex
    Dec 11 '18 at 8:04





















0














The canonical way to use AspectJ from within Spring applications as described by the manual is to use AspectJ load-time weaving (LTW). It does not make sense to weave code into Spring or Java EE binaries and create new JARs etc., so IMO source or binary compile-time weaving is not an option if you want to target 3rd party code. Instead, a dynamic Java weaving agent as described in the manual chapter I linked to above.



Having said that, it was just a reply to the AOP-related suggestions I read about here. It would work the way I described it, but I am happy the OP has solved his problems by means of Mapped Diagnostic Context already.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484551%2fhow-to-enforce-a-common-log-format-using-spring-aop-want-to-append-a-string-e-g%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There are many ways to do this, here is 1.



    1) Create an annotation to add to classes/methods that need logging added:



    @Documented
    @Target(ElementType.METHOD)
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LogExecution {
    }


    2) Create an aspect to do the logging:



    @Aspect
    @Component
    public class LogAspect {

    private List<String> messages = new ArrayList<>();

    @Around("@annotation(hello.LogExecution)")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

    Object proceed = null;
    try {
    // log before
    proceed = joinPoint.proceed();
    // log after
    }
    catch (Exception e) {
    // log exception
    throw e;
    }
    return proceed;
    }
    }


    Working example here






    share|improve this answer
























    • Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

      – Abinash Pattnaik
      Nov 27 '18 at 6:45
















    0














    There are many ways to do this, here is 1.



    1) Create an annotation to add to classes/methods that need logging added:



    @Documented
    @Target(ElementType.METHOD)
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LogExecution {
    }


    2) Create an aspect to do the logging:



    @Aspect
    @Component
    public class LogAspect {

    private List<String> messages = new ArrayList<>();

    @Around("@annotation(hello.LogExecution)")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

    Object proceed = null;
    try {
    // log before
    proceed = joinPoint.proceed();
    // log after
    }
    catch (Exception e) {
    // log exception
    throw e;
    }
    return proceed;
    }
    }


    Working example here






    share|improve this answer
























    • Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

      – Abinash Pattnaik
      Nov 27 '18 at 6:45














    0












    0








    0







    There are many ways to do this, here is 1.



    1) Create an annotation to add to classes/methods that need logging added:



    @Documented
    @Target(ElementType.METHOD)
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LogExecution {
    }


    2) Create an aspect to do the logging:



    @Aspect
    @Component
    public class LogAspect {

    private List<String> messages = new ArrayList<>();

    @Around("@annotation(hello.LogExecution)")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

    Object proceed = null;
    try {
    // log before
    proceed = joinPoint.proceed();
    // log after
    }
    catch (Exception e) {
    // log exception
    throw e;
    }
    return proceed;
    }
    }


    Working example here






    share|improve this answer













    There are many ways to do this, here is 1.



    1) Create an annotation to add to classes/methods that need logging added:



    @Documented
    @Target(ElementType.METHOD)
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LogExecution {
    }


    2) Create an aspect to do the logging:



    @Aspect
    @Component
    public class LogAspect {

    private List<String> messages = new ArrayList<>();

    @Around("@annotation(hello.LogExecution)")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

    Object proceed = null;
    try {
    // log before
    proceed = joinPoint.proceed();
    // log after
    }
    catch (Exception e) {
    // log exception
    throw e;
    }
    return proceed;
    }
    }


    Working example here







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 26 '18 at 15:53









    Essex BoyEssex Boy

    4,7731916




    4,7731916













    • Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

      – Abinash Pattnaik
      Nov 27 '18 at 6:45



















    • Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

      – Abinash Pattnaik
      Nov 27 '18 at 6:45

















    Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

    – Abinash Pattnaik
    Nov 27 '18 at 6:45





    Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class.

    – Abinash Pattnaik
    Nov 27 '18 at 6:45













    0














    It sounds like you don't have Aspect J correctly configured.



    See the answer to this question for the correct setup:



    Spring + AspectJ weaving for java 8 using aspectj-maven-plugin



    After that, spring should recognize your pointcuts






    share|improve this answer
























    • This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

      – kriegaex
      Dec 11 '18 at 8:04


















    0














    It sounds like you don't have Aspect J correctly configured.



    See the answer to this question for the correct setup:



    Spring + AspectJ weaving for java 8 using aspectj-maven-plugin



    After that, spring should recognize your pointcuts






    share|improve this answer
























    • This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

      – kriegaex
      Dec 11 '18 at 8:04
















    0












    0








    0







    It sounds like you don't have Aspect J correctly configured.



    See the answer to this question for the correct setup:



    Spring + AspectJ weaving for java 8 using aspectj-maven-plugin



    After that, spring should recognize your pointcuts






    share|improve this answer













    It sounds like you don't have Aspect J correctly configured.



    See the answer to this question for the correct setup:



    Spring + AspectJ weaving for java 8 using aspectj-maven-plugin



    After that, spring should recognize your pointcuts







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 26 '18 at 15:57









    mad_foxmad_fox

    1,24921429




    1,24921429













    • This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

      – kriegaex
      Dec 11 '18 at 8:04





















    • This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

      – kriegaex
      Dec 11 '18 at 8:04



















    This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

    – kriegaex
    Dec 11 '18 at 8:04







    This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly.

    – kriegaex
    Dec 11 '18 at 8:04













    0














    The canonical way to use AspectJ from within Spring applications as described by the manual is to use AspectJ load-time weaving (LTW). It does not make sense to weave code into Spring or Java EE binaries and create new JARs etc., so IMO source or binary compile-time weaving is not an option if you want to target 3rd party code. Instead, a dynamic Java weaving agent as described in the manual chapter I linked to above.



    Having said that, it was just a reply to the AOP-related suggestions I read about here. It would work the way I described it, but I am happy the OP has solved his problems by means of Mapped Diagnostic Context already.






    share|improve this answer




























      0














      The canonical way to use AspectJ from within Spring applications as described by the manual is to use AspectJ load-time weaving (LTW). It does not make sense to weave code into Spring or Java EE binaries and create new JARs etc., so IMO source or binary compile-time weaving is not an option if you want to target 3rd party code. Instead, a dynamic Java weaving agent as described in the manual chapter I linked to above.



      Having said that, it was just a reply to the AOP-related suggestions I read about here. It would work the way I described it, but I am happy the OP has solved his problems by means of Mapped Diagnostic Context already.






      share|improve this answer


























        0












        0








        0







        The canonical way to use AspectJ from within Spring applications as described by the manual is to use AspectJ load-time weaving (LTW). It does not make sense to weave code into Spring or Java EE binaries and create new JARs etc., so IMO source or binary compile-time weaving is not an option if you want to target 3rd party code. Instead, a dynamic Java weaving agent as described in the manual chapter I linked to above.



        Having said that, it was just a reply to the AOP-related suggestions I read about here. It would work the way I described it, but I am happy the OP has solved his problems by means of Mapped Diagnostic Context already.






        share|improve this answer













        The canonical way to use AspectJ from within Spring applications as described by the manual is to use AspectJ load-time weaving (LTW). It does not make sense to weave code into Spring or Java EE binaries and create new JARs etc., so IMO source or binary compile-time weaving is not an option if you want to target 3rd party code. Instead, a dynamic Java weaving agent as described in the manual chapter I linked to above.



        Having said that, it was just a reply to the AOP-related suggestions I read about here. It would work the way I described it, but I am happy the OP has solved his problems by means of Mapped Diagnostic Context already.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 11 '18 at 8:05









        kriegaexkriegaex

        32.1k367103




        32.1k367103






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484551%2fhow-to-enforce-a-common-log-format-using-spring-aop-want-to-append-a-string-e-g%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Marschland

            Redirect URL with Chrome Remote Debugging Android Devices