Files
mgm-daylight-mcrm-api-mobile/src/main/java/com/mgm/mcrm/common/utils/LogUtil.java
T
2026-09-15 17:04:06 +08:00

85 lines
2.5 KiB
Java

package com.mgm.mcrm.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.mgm.mcrm.common.constants.SysCommon;
import com.mgm.mcrm.common.dtos.BaseApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogUtil {
private static String loggerName = LogUtil.class.getName();
private static Logger logger = LoggerFactory.getLogger(loggerName);
public static Logger getLogger(Class callClass){
try
{
logger = LoggerFactory.getLogger(callClass.getName());
}catch(Exception err){
loggerName = LogUtil.class.getName();
logger = LoggerFactory.getLogger(loggerName);
}
return logger;
}
public static void info(Class callClass,String message) {
if(SysCommon.appDebugLog == true)
getLogger(callClass).info(message);
}
public static void warn(Class callClass,String message) {
if(SysCommon.appDebugLog == true)
getLogger(callClass).warn(message);
}
public static void error(Class callClass,String message) {
if(SysCommon.appDebugLog == true)
getLogger(callClass).error(message);
}
public static String getObjectAttr(Object srcObj, String[] attrNames) {
StringBuilder attrText = new StringBuilder();
if(srcObj!=null && attrNames!=null) {
try {
JSONObject jsonObject = (JSONObject)JSONObject.toJSON(srcObj);
for(String attr:attrNames) {
attrText.append(attr).append("=");
if(jsonObject.containsKey(attr)) {
attrText.append(jsonObject.getString(attr));
}
attrText.append(";");
}
}catch(Exception ignored) {
}
}
return attrText.toString();
}
public static String getFormatLog(String operationTag, String dataTag, boolean isSuccess, String resultTag,String message) {
String logMessage = "";
String statusMsg = (resultTag==null?"":resultTag);
String errorMsg = (message==null?"":message);
logMessage = operationTag+(StringUtils.isBlank(dataTag)?"":" ["+dataTag+"]");
if(isSuccess) {
logMessage += (" successed" + (StringUtils.isBlank(statusMsg)?"":" with ["+statusMsg+"]"));
}else {
logMessage += (" failed with error ["+errorMsg+"]");
}
return logMessage;
}
public static String getAjaxFormatLog(BaseApiResponse result, String operation, String[] reqAttr, String[] resultAttr) {
String logMessage = "";
if(result!=null) {
String reqTxt = getObjectAttr(result.getRequestData(),reqAttr);
String resultTxt = getObjectAttr(result.getData(),resultAttr);
logMessage = getFormatLog(operation,reqTxt,result.isSuccess(),resultTxt,result.getMessage());
}
return logMessage;
}
}