com.base4j.util.LogUtil Maven / Gradle / Ivy
The newest version!
package com.base4j.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogUtil {
public static Logger logger = LoggerFactory.getLogger(LogUtil.class.getName());
/**
* 记录对象字段的变化日志
*
* @param message 日志描述
* @param oldBean 待比较对象
* @param newBean 比较的对象
* @param names 描述的字段
*/
@SuppressWarnings("rawtypes")
public final static void changeLog(String message, Object oldBean, Object newBean, String... names) {
StringBuffer sfStr = new StringBuffer();
StringBuffer sfName = new StringBuffer();
try {
if (ValidatorUtil.isEmpty(oldBean) || ValidatorUtil.isEmpty(newBean)) {
return;
}
if (oldBean instanceof Map && newBean instanceof Map) {
Map m1 = (Map) oldBean;
Map m2 = (Map) newBean;
String m1value;
String m2value;
Map.Entry entry1;
@SuppressWarnings("unchecked")
Iterator iter1 = m1.entrySet().iterator();
while (iter1.hasNext()) {
entry1 = iter1.next();
m1value = entry1.getValue() == null ? "" : entry1.getValue().toString();
m2value = m2.get(entry1.getKey()) == null ? "" : m2.get(entry1.getKey().toString()).toString();
if (!m1value.equals(m2value)) {// 若两个map中相同key对应的value不相等
sfStr.append(entry1.getKey().toString() + "[" + m1value + "] -> [" + m2value + "]");
}
}
if (!ValidatorUtil.isEmpty(sfStr.toString())) {
logger.info(getTraceInfo() + " " + message + " 对象[" + oldBean.getClass().getSimpleName() + "] "
+ sfName.toString());
}
return;
}
Class clazz = oldBean.getClass();
Field[] fields = oldBean.getClass().getDeclaredFields();
int i = 1;
PropertyDescriptor pd = null;
Method getMethod = null;
Object o1 = null;
Object o2 = null;
String o1Str = "";
String o2Str = "";
for (Field field : fields) {
if ("serialVersionUID".equals(field.getName())) {
continue;
}
try {
pd = new PropertyDescriptor(field.getName(), clazz);
getMethod = pd.getReadMethod();
o1 = getMethod.invoke(oldBean);
o2 = getMethod.invoke(newBean);
if (names != null && names.length > 0) {
for (String name : names) {
if (name.equals(field.getName())) {
sfName.append(name + ":" + (o1 == null ? "" : o1.toString()) + " ");
}
}
}
o1Str = obj2Str(o1);
o2Str = obj2Str(o2);
if ("id".equals(field.getName()) || !o1Str.equals(o2Str)) {
if (i != 1) {
sfStr.append(",");
}
sfStr.append(field.getName() + "[" + o1Str + "] -> [" + o2Str + "]");
i++;
}
} catch (Exception e) {
logger.debug(getTraceInfo() + e.getMessage());
}
}
if (!ValidatorUtil.isEmpty(sfStr.toString())) {
logger.info(getTraceInfo() + " " + message + " 对象[" + oldBean.getClass().getSimpleName() + "] "
+ sfName.toString() + "修改 " + sfStr.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 记录对象字段的变化日志
*
* @param message
* @param bean
*/
@SuppressWarnings("rawtypes")
public final static void beanLog(String message, Object bean) {
StringBuffer sfStr = new StringBuffer();
try {
if (ValidatorUtil.isEmpty(bean)) {
return;
}
if (bean instanceof Integer || bean instanceof Long || bean instanceof String) {
logger.info(getTraceInfo() + message + " " + bean.getClass().getSimpleName() + "[" + bean + "]");
return;
}
if (bean instanceof List) {
List list = (List) bean;
for (Object beanIn : list) {
beanLog(message, beanIn);
}
return;
}
if (bean.getClass().isArray()) {
Object[] objs = (Object[]) bean;
for (Object beanIn : objs) {
beanLog(message, beanIn);
}
return;
}
if (bean instanceof Map) {
Map map = (Map) bean;
StringBuffer sf = new StringBuffer();
for (Object key : map.keySet()) {
sf.append(key);
sf.append("[");
sf.append(map.get(key));
sf.append("]");
}
logger.info(getTraceInfo() + message + sf);
return;
}
Class clazz = bean.getClass();
Field[] fields = bean.getClass().getDeclaredFields();
int i = 1;
PropertyDescriptor pd = null;
Method getMethod = null;
Object o1 = null;
String o1Str = "";
for (Field field : fields) {
if ("serialVersionUID".equals(field.getName())) {
continue;
}
try {
pd = new PropertyDescriptor(field.getName(), clazz);
getMethod = pd.getReadMethod();
o1 = getMethod.invoke(bean);
if (o1 == null) {
continue;
}
o1Str = obj2Str(o1);
if (!"".equals(obj2Str(o1))) {
if (i != 1) {
sfStr.append(",");
}
sfStr.append(field.getName() + "[" + o1Str + "]");
i++;
}
} catch (Exception e) {
logger.debug(e.getMessage());
}
}
if (!ValidatorUtil.isEmpty(sfStr.toString())) {
logger.info(getTraceInfo() + " " + message + " 对象[" + bean.getClass().getSimpleName() + "] "
+ sfStr.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private final static String obj2Str(Object obj) {
if (obj == null) {
return "";
}
if (obj instanceof Date) {
return DateUtil.date2str((Date) obj);
}
return obj.toString();
}
/**
* Java中获取当前运行代码的类名、方法名、行号
*
* @return
*/
public final static String getTraceInfo() {
StringBuffer sb = new StringBuffer();
StackTraceElement[] stacks = new Throwable().getStackTrace();
if (stacks.length >= 4) {
if (stacks[3].getLineNumber() > 0) {
sb.append(stacks[3].getFileName()).append("[").append(stacks[3].getLineNumber()).append("]");
} else {
sb.append(stacks[2].getFileName()).append("[").append(stacks[2].getLineNumber()).append("]");
}
}
return sb.toString();
}
/**
* 获取日志中的栈信息
* <功能详细描述>
*
* @param @param e
* @param @return [参数说明]
* @return
* @throws Exception
* @author huangjun
*/
public final static String getStackTrace(Exception e) {
StringBuffer sb = new StringBuffer();
StackTraceElement[] stackTraces = e.getStackTrace();
if (ValidatorUtil.isNotEmpty(stackTraces)) {
for (StackTraceElement stackTrace : stackTraces) {
sb.append(stackTrace.toString() + "\n");
}
}
return sb.toString();
}
}