基於slf4j的日誌工具類

welchang發表於2021-09-09

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 objects){

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 m :objects.entrySet())  { 

log.debug("inMethod parameter["+m.getKey()+"] is : ");

log.debug(m.getValue()+"");

}

}

}

//DEBUG級別:方法執行結束日誌輸出,入參為需要列印的方法輸出,process級別的方法

public static void getMethodEndLog(Map objects){

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 m :objects.entrySet())  { 

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部落格 ” ,連結:http://blog.itpub.net/4830/viewspace-2804278/,如需轉載,請註明出處,否則將追究法律責任。

相關文章