I am trying to intercept a class in third party jar from my spring boot application.
I am trying to do it as
<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
<bean id="instrumentationLoadTimeWeaver"
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
While my Aspect logger class looks like
@Aspect
public class LogInterceptor
{
private static final Logger PERF_LOGGER = LoggerFactory.getLogger("PERFLOG");
private static final Logger LOGGER = LoggerFactory.getLogger(LogInterceptor.class);
private static final int SYSTEM_ERROR_STATUS_CODE = -1;
private static final int SYSTEM_SUCCESS_STATUS_CODE = 0;
@Around(
"execution(* com.alpha.executor.dao..*(..)) || "
+ "execution(* com.alpha.executor.resource..*(..)) || "
+ "execution(* com.alpha.executor.client..*(..)) || "
+ "execution(* com.alpha.executor.service..*(..)) || "
+ "execution(* com.alpha.executor.proxy.*.*(..)) ")
public Object logRecordInPerflog(ProceedingJoinPoint joinPoint) throws Throwable
{
String activityName = joinPoint.getTarget().getClass().getSimpleName();
String operationName = activityName + "." + joinPoint.getSignature().getName();
RequestLog reqLog = new RequestLog(operationName, activityName, joinPoint.getArgs());
PERF_LOGGER.info(reqLog.getLoggerString());
try
{
Object result = joinPoint.proceed();
ResponseLog resLog = new ResponseLog(reqLog, result, SYSTEM_SUCCESS_STATUS_CODE);
PERF_LOGGER.info(resLog.getLoggerString());
return result;
}
catch (Exception ex)
{
logException(reqLog, ex);
throw ex;
}
}
private void logException(RequestLog reqLog, Exception ex)
{
ResponseLog resLog;
if (ex instanceof SvxException)
{
SvxException svxException = (SvxException) ex;
ErrorCode errorCode = svxException.getSvxExceptionDetail().getErrorCode();
ErrorResponse errorResponse = new ErrorResponse(errorCode,svxException.getSvxExceptionDetail().getErrorDescription());
resLog = new ResponseLog(reqLog,null,errorCode.getHttpStatus().value(),errorResponse,errorCode.getErrorType().toString());
}
else
{
LOGGER.error("Error: ", ex);
resLog = new ResponseLog(reqLog, null, SYSTEM_ERROR_STATUS_CODE, ErrorType.SYSTEM_ERROR.toString(), ex.getMessage());
}
PERF_LOGGER.info(resLog.getLoggerString());
}
}
However, the above code gives an error that No custom load time weaver found. I don't want to use spring-instrumentation.jar in VM args.
Also already tried the class-based method with annotation, however, read somewhere that Spring might be loading that class after some classes are loaded hence that might not work.
EDIT:
Added the logger and advice. Here I have manually created a proxy, see the last line, which isn't a good solution I believe.