Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] general tracing aspect

To me, this is one of the most important use cases for extending AspectJ
for generics (see JSR-14, which is slated for inclusion in J2SDK 1.5).
Here is how I would like it to work. Note that you also need to
initialize the logger, either lazily (as Jon implied) or with advice on
static initialization.

package my.package;

/* doesn’t need to be privileged */
abstract aspect Trace<Type implements Trace.Traceable>
{
      static private Logger Type.logger;
      protected interface Traceable {}

	pointcut method(): within(Type) && execution(* *(..));
	
	before(): method() 
	{
	
 
Type.logger.enter(thisJoinPointStaticPart.getSignature().getName(
) ,
		thisJoinPoint.getArgs());
	}
		
	after() returning(Object o): method() 
	{
	
 
Type.logger.leave(thisJoinPointStaticPart.getSignature().getName(
), o);
	}	
      before() : staticinitialization(Type) {
          Type.logger = new Logger();
      }
}

aspect TraceMyPackage extends Trace {
     declare parents: my.package..* implements Traceable;
}

-----Original Message-----
From: aspectj-users-admin@xxxxxxxxxxx
[mailto:aspectj-users-admin@xxxxxxxxxxx] On Behalf Of Nuno Oliveira
Sent: Wednesday, April 23, 2003 9:38 AM
To: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users] general tracing aspect

Yes, it works just just fine. Thank you very much!

However, even as a newbie in this subject, I would be so bold as to
suggest
that aspectJ sintax is limited in that it doesn't allow the transfer of
all
knowledge about the joinpoint context to the body of advice. I guess I
could
try to use Reflection inside the body of advice to get to that context,
but
hey! I could write the logging code in the original source too...Maybe
Type
patterns as the arguments of advice would do the trick (??)
In this particular case, what solved the situation to reflect the true
cross-cutting nature of the issue was the fact that log4j semantics
allow
the "weaving" of this tracing aspect into the original code.

Best regards,
Nuno

-----Original Message-----
From: Jon Cundill [mailto:jcundill@xxxxxxxxxxxxx]
Sent: Wednesday, April 23, 2003 3:17 PM
To: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users] general tracing aspect


Hi,

 we use something like

    public static void traceEntry(Signature sig) {
    	Log log = LogFactory.getLog( sig.getDeclaringType() );
    	if( log.isDebugEnabled() )
    	{
    		log.debug( "ENTERED " + sig.getName());
    	}
    }

This is for jakarta commons logging, but I would think plain Log4j would
work similarly.
To stop the overhead of getLog() on every invocation, lazt init the Logs
into a Map held as an instance variable in the aspect and
keyed on the Class retrieved from sig.getDeclaringType().

Jon

-----Original Message-----
From: aspectj-users-admin@xxxxxxxxxxx
[mailto:aspectj-users-admin@xxxxxxxxxxx]On Behalf Of Nuno Oliveira
Sent: 23 April 2003 14:50
To: rhill@xxxxxxxxxx
Cc: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users] general tracing aspect


Your reply was most welcome! thanks :)

The solution you provided solves the problem of the class name to use in
advice but doesn't comply with my whish to have a single Logger instance
per
class. This way every class will be logging to the same logger (the
defined
one). What I would need would be a way to create a static logger
instance
per-class using a type patern instead of a specific Type or a way to
specify
that the logging is to take place in the logger variable that each class
already has.

Thanks again.
Nuno

-----Original Message-----
From: Gilles DODINET [mailto:rhill@xxxxxxxxxx]
Sent: Wednesday, April 23, 2003 1:20 PM
To: Nuno Oliveira
Subject: Re: [aspectj-users] general tracing aspect


what about something like

interface Loggable {}
declare parent : my.package.* implements Loggable;

//init logger as soon as possible (<init> ? <clinit> ?)
private Logger Loggable.anotherLogger;

pointcut method(Loggable o) :
               within(Loggable+)
               && execution(* *(..))
               && this(o);

before(Loggable o): method(o)
{
 	     o.anotherLogger.enter(

thisJoinPointStaticPart.getSignature().getName() ,
  		               thisJoinPoint.getArgs());
}

i have not tested it, tho i think that it should work, with quite little
modifications. please provide me with some feedbacks :)

-- gd


> Message du 23/04/03 12:40
> De : Nuno Oliveira <noliveira@xxxxxx>
> A : aspectj-users@xxxxxxxxxxx
> Copie à :
> Objet : [aspectj-users] general tracing aspect
> Hello all,
>
> I have a bunch of classes each with its own static instance of a log4j
> logger. This instances all have the same name 'logger'. I want to add
> enter-leave tracing to each of the methods of all classes but would be
> interested in making this with a single aspect. However I'm having
trouble
> with the sintax to refer to the logger in my advice, without using the
class
> name -which would defeat the purpose. I cannot use type patterns in
advice,
> rigth ? Can anyone provide any hints ? Using aspectJ, a sintatically
wrong
> expression of my needs would be:
>
> package my.package;
>
> privileged aspect TraceEnterLeave
> {
> 	pointcut method(): within(my.package..*) && execution(* *(..));
>
> 	before(): method()
> 	{
>
>
(my.package..*).logger.enter(thisJoinPointStaticPart.getSignature().getN
ame(
> ) ,
> 		thisJoinPoint.getArgs());
> 	}
>
> 	after() returning(Object o): method()
> 	{
>
>
(my.package..*).logger.leave(thisJoinPointStaticPart.getSignature().getN
ame(
> ), o);
> 	}
> }
>
> A related question would be how to migrate the static logger
declaration
in
> each class to that tracing aspect ? Could I use a type pattern to get
the
> class name to instatiate each logger ?
>
> Thanks in advance.
>
> Nuno
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top