
net.jmatrix.aspects.AbstractLoggingAspect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmutils Show documentation
Show all versions of jmutils Show documentation
PerfTrack and Async utilities.
package net.jmatrix.aspects;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.jmatrix.annotations.Logged;
import net.jmatrix.annotations.PerfTracked;
import net.jmatrix.utils.ClassLogFactory;
import net.jmatrix.utils.DebugUtils;
import net.jmatrix.utils.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public abstract class AbstractLoggingAspect
{
private static final Logger log = ClassLogFactory.getLog();
static final String[] emptyArray = new String[] {};
protected static Pattern pattern = Pattern.compile("%(<|([0-9]+)\\$)?([-#+ 0,(]*[0-9]*(?:\\.[0-9]+)?([bhscdoxefgat%jJdD]))");
protected static ObjectMapper compactObjectMapper = createObjectMapper(false);
protected static ObjectMapper indentingObjectMapper = createObjectMapper(true);
protected boolean hasLogAnnotation(ProceedingJoinPoint thisJoinPoint)
{
Signature sig = thisJoinPoint.getStaticPart().getSignature();
if (sig instanceof MethodSignature) {
// this must be a call or execution join point
Method method = ((MethodSignature)sig).getMethod();
Logged loggedAnnotation = method.getAnnotation(Logged.class);
if (loggedAnnotation != null) return true;
}
return false;
}
protected boolean hasPerfTrackedAnnotation(ProceedingJoinPoint thisJoinPoint)
{
Signature sig = thisJoinPoint.getStaticPart().getSignature();
if (sig instanceof MethodSignature) {
// this must be a call or execution join point
Method method = ((MethodSignature)sig).getMethod();
PerfTracked perfTrackedAnnotation = method.getAnnotation(PerfTracked.class);
if (perfTrackedAnnotation != null) return true;
}
return false;
}
protected String[] getParamNames(ProceedingJoinPoint thisJoinPoint)
{
Signature sig = thisJoinPoint.getStaticPart().getSignature();
if (sig instanceof MethodSignature) {
// this must be a call or execution join point
return ((MethodSignature)sig).getParameterNames();
}
return emptyArray;
}
/**
* constructMethodSignature
*
* @deprecated Use {@link #formatMethodSignature(ProceedingJoinPoint, String)} instead
* @param thisJoinPoint
* @param paramNames
* @return
*/
protected String constructMethodSignature(ProceedingJoinPoint thisJoinPoint, String paramNames)
{
String methodName;
String paramString = "";
methodName = thisJoinPoint.getSignature().toShortString();
methodName = methodName.substring(0,methodName.indexOf('(')+1);
String[] params = (paramNames==null?"":paramNames).split(", *",-1);
String seperator = "";
int i = 0;
for (Object arg : thisJoinPoint.getArgs())
{
String paramSpec;
if (i < params.length && !StringUtil.empty(paramSpec = params[i].trim()))
{
String[] paramOptions = Arrays.copyOf(paramSpec.split(" *: *",3),3);
String paramName = paramOptions[0];
int maxLength = 256;
String lengthOption = paramOptions[1];
if (!StringUtil.empty(lengthOption)) {
try {
maxLength = Integer.parseInt(lengthOption);
} catch (Exception ex) {
System.out.println ("Error parsing max length from token '"+lengthOption+"'"+ex.toString());
}
}
String compactOption = paramOptions[2];
boolean compact = true;
if (!StringUtil.empty(compactOption))
{
compact = Boolean.parseBoolean(compactOption);
}
arg = DebugUtils.debugString(arg, 0, maxLength, compact);
paramString = paramString + seperator + paramName +"="+arg;
}
seperator = ", ";
i++;
}
methodName = methodName + paramString + ")";
//System.out.println(methodName);
return methodName;
}
/**
* formatMethodSignature
*
* @param thisJoinPoint an AspectJ {@link ProceedingJoinPoint}
* @param format a String of the form used by {@link Formatter} with
* extensions described in {@link Logged} and {@link PerfTracked}
* @return a String containing a formatted method signature for logging.
*/
protected String formatMethodSignature(ProceedingJoinPoint thisJoinPoint, String format)
{
String methodName;
String paramString = "";
methodName = thisJoinPoint.getSignature().toShortString();
methodName = methodName.substring(0,methodName.indexOf('(')+1);
paramString = format(format, thisJoinPoint.getArgs());
methodName = methodName + paramString + ")";
//System.out.println(methodName);
return methodName;
}
public static String format(String format, Object arg)
{
return format(format, new Object[] {arg});
}
/**
* format
implements the extensions to the formatting described in
* {@link Formatter} listed below
*
* - J - print arg in JSON notation with indentation
* - j - print arg in JSON notation without indentation
* - D - print arg in {@link DebugUtils#debugString(Object)} format with indentation
* - d - print arg in {@link DebugUtils#debugString(Object)} format without indentation
* NOTE that this conversion directive conflicts with the standard Formatter's 'd'
* directive for formatting numbers. If the arg in question descends from {@link Number}
* then it will be formatted using the standard conversion. Any other arg type
* is formatted using {@link DebugUtils#debugString(Object)}
*
* @param format a String containing a printf-like formatting directive described above
* @param args an Array of Objects to be formatted using the given format string
* @return
*/
public static String format(String format, Object[] args)
{
try
{
if (StringUtil.empty(format))
{
return "";
}
// List of arguments that may or may not be the same size as the input parameter 'args'
// This list will contain one argument for each conversion spec matched by the RE below
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy