Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j and AOP, how to get actual class name

I'm implementing a logger as an aspect using Spring AOP and Log4J, but I've noticed that the class name in log file is always the LoggerAspect class name, so... is there a way to trace the actual class name in my log?

like image 260
davioooh Avatar asked Jul 16 '12 13:07

davioooh


People also ask

How do you get the class name from ProceedingJoinPoint?

The ProceedingJoinPoint. getSignature() method returns everything you need to get the actual class name, method name, return type and parameters for the joinpoint.

Is log4j AOP?

log4j is the underlying physical logging framework. AOP allows you to intercept Class/Method calls and log accordingly either using log4j, or a facade pattern such as commons logging or (recommended) slf4j. without the programmer having to do it.

What is ProceedingJoinPoint?

ProceedingJoinPoint exposes the proceed(..) method in order to support around advice in @AJ aspects. Nested Class Summary. Nested classes/interfaces inherited from interface org.aspectj.lang.JoinPoint. JoinPoint.EnclosingStaticPart, JoinPoint.StaticPart.

What is AOP logging?

AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does this by adding additional behavior to existing code without modifying the code itself. Instead, we can declare the new code and the new behaviors separately.


1 Answers

Use : pjp.getTarget().getClass().getCanonicalName()

Also, to add logs at class level of which method is being exexuted instead using logger of advice class, use class level logger within advice method, as below

Logger logger = Logger.getLogger(pjp.getTarget().getClass().getCanonicalName());
like image 120
Nitul Avatar answered Oct 14 '22 20:10

Nitul