0

I posted this on another forum and wanted to see if I can reach more people.

I am working on an application that consists of different Spring web apps.

Say we have:

  • ComponentA.jar
  • ComponentB.jar

And WAR files:

  • Foo.war (contains ComponentA)
  • Baa.war (contains ComponentA & ComponentB)

We are using Logback to log to our debug log. So say that the various classes of the application have the the following logger declaration:

private static final Log log = LoggerFactory.getLogger(NAME_OF_WAR_FILE + "." + NAME_OF_CONTAINING_COMPONENT + "." + PACKAGE.CLASS_NAME);

So example:

package a.b.c;

public class SomeClass {
   private static final Log log = LoggerFactory.getLogger("Foo.war" + "." + "ComponentA" + "." + SomeClass.class);
}

package x.y.z;

public class SomeOtherClass {

   private static final Log log = LoggerFactory.getLogger("Baa.war" + "." + "ComponentA" + "." + SomeOtherClass .class);
}

Assume that the name of the war file and component is set by a property and not hard-coded.

Is it possible to have an Aspect and Advice that can do something like the following (pseudo since I'm not sure it can be done):

@Aspect
public class TheAspect{

   @Around("execution of a method")
   public Object aroundSomething(ProceedingJoinPoint pjp){

      Log log = get the log instance from the class that this advice is running on

      if(log.isDebugEnabled())
         // log something

      Object o = pjp.proceed();

       if(log.isDebugEnabled())
         // log something else

     return o;

   }
}

The point here is to write to the log file using the log of the class instance which contains the method which is being intercepted by the Advice.

The application is presented as a single web app composed of Foo.war and Baa.war. Both Foo.war and Baa.war write to the same log file.

Example:

2011-09-22 14:35:35.159 MDT,DEBUG,Foo.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentB.x.y.z.SomeOtherClass,Hello World Debug message

Thanks in advance.

Jcx Jc
  • 201
  • 4
  • 8

1 Answers1

0

You can use thisJoinPoint inside your aroundSomething method.

To get the class name:

Signature sig = thisJoinPoint.getSignature();
String className = sig.getDeclaringTypeName();

You can also get the class object:

Class<?> type = sig.getDeclaringType();

And maybe you can use the package to identify your war file:

Package pack type.getPackage();
Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
  • thanks for the info. I thought about this. The only problem is that I am dealing with existing code that is riddled with log declarations as `private static final Log LOG = ... ` and editing all the classes to put getters/setters for the log would be overkill. I was mainly asking the question to see what others would think, but it may be that it is not worth the hassle. – Jcx Jc Sep 23 '11 at 15:25
  • @JcxJc If you want to access a private field with an aspect you can make it [privileged](http://www.eclipse.org/aspectj/doc/released/progguide/semantics-aspects.html#aspect-privilege). – Dario Seidl Sep 23 '11 at 15:51