Announcement Announcement Module
Collapse
No announcement yet.
Detailed Logging qu Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detailed Logging qu

    Hi,

    I understand I can use Spring to log information on methods called and their results etc..

    However, in the app I'm trying to convert there are lots of places where logging is used within methods (as I'm sure many do) eg:

    Code:
     if (log.isDebugEnabled ())
                    log.debug ("Created a new socket connection to: " +
                               InetAddress.getByName (address) +
                               ":" + port);
    Is there a neat way to use Spring to be able to log this more detailed kind of information.

    I guess at the least, is there a way to make a log object available in all classes?

    Many thanks,

    David

  • #2
    I do not think AOP is sweetable to log such a detailed kind of information.
    Spring uses Commons Logging internally for logging :wink:

    Comment


    • #3
      Re: Detailed Logging qu

      Originally posted by hay7777

      Code:
       if (log.isDebugEnabled ())
                      log.debug ("Created a new socket connection to: " +
                                 InetAddress.getByName (address) +
                                 ":" + port);
      Is there a neat way to use Spring to be able to log this more detailed kind of information.
      David,
      You should try to refactor your code, one of sidefect benefits is improved logging. Since resonable way to do logging is AOP way, you could obtain the same effect by extracting method:
      createSocket(InetAddreas address, Int port)
      This way, you get detailed logging without writing superfluous code and using logging aspects...

      Good luck,

      Danijel

      Comment


      • #4
        David,

        If you really want to add logging behavior using AOP then you should look at either AspectJ or AspectWerkz which modify your bytecode at runtime and allow you to access many more joinpoints than Spring AOP.

        That said, I don't think that logging is that good a use of AOP - I see it more as method tracing and I prefer to have logging in the code so that it is explicit. After all logging is part of your applications Quality of Service. Spring itself has TONS of logging that is all done 'inline'.

        Rob

        Comment


        • #5
          Originally posted by robh
          D I see it more as method tracing and I prefer to have logging in the code so that it is explicit. After all logging is part of your applications Quality of Service.
          Hi Rob,

          I understand your point.
          Still, logging just looks too much like separate concern and crosscutting concern (or another “dimension”).
          I think that combining AOP with annotations could give a solution that is both explicit and flexible enough (for example can be internationalized) and also addresses the separation of concerns “aspect” of the problem (No pun intended

          Danijel

          Comment


          • #6
            Danijel,

            Your approach is perfectly valid - AOP is ideal for this kind of thing. My point of view is that with AOP you unable to give as much contextualized information as you would be able to with logging messages directly in the code. AOP is great for applying trace messages to see which methods are invoked, but to be able to provide quality log messages you need to be fully aware of the logical context of the message not just the runtime information.

            Rob

            Comment


            • #7
              Originally posted by robh
              My point of view is that with AOP you unable to give as much contextualized information as you would be able to with logging messages directly in the code.
              +1. Spot on.

              Comment


              • #8
                Originally posted by robh
                My point of view is that with AOP you unable to give as much contextualized information as you would be able to with logging messages directly in the code.
                I guess I failed to provide an example in order to make my point. The clue is in combining annotations with pure AOP and well refactored code.
                All contextualized information you can log comes from runtime (variable values etc.) or from annotations we make explicitly in our code, in example above it is message:
                “Created a new socket connection to:”
                This information can be moved to annotations part of code, as in this sample using java 1.5 style annotations:

                @LogMethodInvoc(
                message = “Created a new socket connection to:”,
                params=”I am trying to access URL:|on port:”

                )
                createSocket(InetAddreas address, Int port){
                //…
                }

                This looks clearer to me. You can also use AOP to for example translate logged text.
                I hope this is valid approach, since I would like to put it into the practice on project that I am working on , looking forward to hearing your opinion.

                Comment


                • #9
                  That is an interesting use of annotations to be sure. I guess you could use a simple templating mechanism for combining the content in the annotations with the runtime data. What concerns me about this approach is how well it will perform at runtime with the additional overhead of Annotation access.

                  Also, unless you're going refactor so agressively that you have methods with only one or two lines, I'm not sure that this approach will give you the flexibility you need.

                  Also, I has the downside of being a JDK 1.5 specific solution. You might want to look at Annogen as a mechanism for making this work with other source level metadata solutions like Commons Attributes.

                  I'd be interested to see what becomes of this implementation and how useful you find it.

                  Rob

                  Comment

                  Working...
                  X