首页 > IT职业 > IT生活 > 基于slf4j的日志工具类
public class LogUtil {
private static final Logger log = LoggerFactory
.getLogger(LogUtil.class);
private static final int PROCESS_TOTAL_LENGTH = 150;
private static final String PROCESS_FILL_FLAG = "*";
//DEBUG级别:方法执行开始日志输出,入参为需要打印的方法入参
public static void getMethodStartLog(Map
if(!log.isInfoEnabled()){
return;
}
String clazz = Thread.currentThread() .getStackTrace()[2].getClassName();
String method = Thread.currentThread() .getStackTrace()[2].getMethodName();
String out = clazz+" "+method+" is starting";
StringBuffer sb = new StringBuffer();
int outLength = out.length();
int start = (PROCESS_TOTAL_LENGTH - outLength)/2;
int end = 0;
if(outLength%2==0){
end = (PROCESS_TOTAL_LENGTH - outLength)/2;
}else{
end = (PROCESS_TOTAL_LENGTH - outLength)/2+1;
}
for(int i=0;i sb.append(PROCESS_FILL_FLAG); } out = sb.toString()+out; sb = new StringBuffer(); for(int i=0;i sb.append(PROCESS_FILL_FLAG); } out = out+sb.toString(); log.debug(out); if(objects !=null){ for (Map.Entry log.debug("inMethod parameter["+m.getKey()+"] is : "); log.debug(m.getValue()+""); } } } //DEBUG级别:方法执行结束日志输出,入参为需要打印的方法输出,process级别的方法 public static void getMethodEndLog(Map if(!log.isInfoEnabled()){ return; } String clazz = Thread.currentThread() .getStackTrace()[2].getClassName(); String method = Thread.currentThread() .getStackTrace()[2].getMethodName(); String out = clazz+" "+method+" is end"; StringBuffer sb = new StringBuffer(); int outLength = out.length(); int start = (PROCESS_TOTAL_LENGTH - outLength)/2; int end = 0; if(outLength%2==0){ end = (PROCESS_TOTAL_LENGTH - outLength)/2; }else{ end = (PROCESS_TOTAL_LENGTH - outLength)/2+1; } for(int i=0;i sb.append(PROCESS_FILL_FLAG); } out = sb.toString()+out; sb = new StringBuffer(); for(int i=0;i sb.append(PROCESS_FILL_FLAG); } out = out+sb.toString(); if(objects !=null){ for (Map.Entry log.debug("inMethod parameter["+m.getKey()+"] is : "); log.debug(m.getValue()+""); } } log.debug(out); } @Deprecated public static void getMethodDebugProcessLog(String msg){ if(!log.isDebugEnabled()){ return; }else{ log.debug(msg); } }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4830/viewspace-2804278/ ,如需转载,请注明出处,否则将追究法律责任。