[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Question about logging:


Ranjith,

We often make a distinction between tracing and logging based on the (not necessarily valid) ability to separate one but not the other using an aspect. Tracing is used to record information at well defined points during program execution such as method entry/exit while logging tends to require knowledge of the program and tends to be rather ad hoc. Of course all significant changes to program state and flow involve a join point which can be advised as Dean illustrates. One approach I have explored is to use explicit calls for ad hoc logging such as info and debug but use inversion of  control and an aspect to supply the implementation of the methods. The same pertypewithin aspect could also implement entry/exit trace for the same classes.


public class Test {

        private static TracingService log;
       
        public static void setTracingService (TracingService service) {
                log = service;
        }

        public  void increment(){
               
                int i=0;
               
                log.info(" About to increase the value of i");
               
                i=i+1;
               
                if(i==1){
               
                        log.info(" Value of i is 1 ");
               
                }
        }
       
        public static void main (String[] args) {
                new Test().increment();
        }
}


import java.lang.reflect.Method;
import java.util.logging.*;

public aspect Tracing implements TracingService pertypewithin(Test) {
       
        private Logger logger;
       
        before () : staticinitialization(*) {
                Class clazz = thisJoinPointStaticPart.getSignature().getDeclaringType();
                logger = Logger.getLogger(clazz.getName());

                try {
                        Class[] parameterTypes = new Class[] { TracingService.class };
                        Method setter = clazz.getDeclaredMethod("setTracingService",parameterTypes);
                        setter.invoke(null,new Object[] {this});
                }
                catch (Exception ex) {
                        ex.printStackTrace();
                }
        }
       
        public void debug(String string) {
        }

        public void event(String string) {
        }

        public void info (String string) {
                logger.info(string);
        }

        public void warning(String string) {
        }

}


public interface TracingService {

        public void debug (String string);

        public void event (String string);

        public void info (String string);
       
        public void warning (String string);

}

Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester,  SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx

http://w3.hursley.ibm.com/~websterm/

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:        aspectj-users-bounces@xxxxxxxxxxx

To:        aspectj-users@xxxxxxxxxxx
cc:        
Subject:        Re: [aspectj-users] Question about logging:


You would have to write a special aspect that uses the "get()" or
"set()" on an instance field, then write advice that looks at the
value and logs appropriately. However, you can only do this on
instance fields, not locally declared variables.  An alternative is to
define special methods that are called when i is set and you write an
aspect that advices those method calls.Since you can't advice blocks
within a method, e.g., a conditional statement, this is the only way
to advice at those (unsupported) join points. I would consider making
those special methods protected or private (then declare the aspect
'privileged' so it can see them...) so they don't clutter the public
interface.

My $0.02,
dean

On 10/21/05, Pillai, Ranjith <rpillai@xxxxxxxx> wrote:
>
>
>
> Hello Gurus,
>
> I would like to use AOP for logging. All documents talk about logging
> "entering" a method and "exiting": however I didn't see any documents which
> explain the true logging like logging "change in values inside a method" or
> before "executing a statement inside a method" etc. If somebody explain me
> how to do that or direct me to some web documents that would be very
> helpful.
>
>
>
> To illustrate my requirement here is a simple method, what I would like to
> accomplish is remove this cross cutting concern (log statement) and write an
> aspect to achieve the same.
>
>
>
> public  void increment(){
>
>             int i=0;
>
>             log.info(" About to increase the value of i");
>
>             i=i+1;
>
>             if(i==1){
>
>                         log.info(" Value of i is 1 ")
>
>             }
>
> }
>
>
>
> Any help will be deeply appreciated,
>
> Thanks,
>
> Ranjith Pillai.
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
>


--
Dean Wampler
http://www.aspectprogramming.com
http://www.newaspects.com
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users