0

I am trying to make an trace logger using AOP programming, simular to this. The problem is that even after getting a new logger like this:

final Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass().getName());

All logs are still like this:

TRACE 2015-11-05 14:35:43,588 LoggingAspect.logMethod(LoggingAspect.java:42) - Some log

Instead of:

TRACE 2015-11-05 14:35:43,588 MyClass.SomeMethod(MyClass.java:10) - Some log

The class to log from does not change. Any suggestions?

Community
  • 1
  • 1
Eduan Bekker
  • 411
  • 2
  • 7
  • 18

1 Answers1

0
joinPoint.getTarget()

returns the target object the method is executed on. This may be null if your object was executed in a static context, I think. But it may be null. So do not use it. And as you can see, It behaves a little bit strange (I think it has to do with proxying or subclassing).

You should better use

joinPoint.getSignature()

to retrieve the signature of the method, containing the defining class. So better use

joinPoint.getSignature().getDeclaringType()

to retrieve the class for the logger.

Fabian Damken
  • 1,477
  • 1
  • 15
  • 24