继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

基于slf4j的日志工具类

大叔_fighting
关注TA
已关注
手记 81
粉丝 44
获赞 400

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<String,Object> 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<start;i++){

sb.append(PROCESS_FILL_FLAG);

}

out = sb.toString()+out;

sb = new StringBuffer();

for(int i=0;i<end;i++){

sb.append(PROCESS_FILL_FLAG);

}

out = out+sb.toString();

log.debug(out);

if(objects !=null){

for (Map.Entry<String, Object> m :objects.entrySet())  { 

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

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

}

}

}

//DEBUG级别:方法执行结束日志输出,入参为需要打印的方法输出,process级别的方法

public static void getMethodEndLog(Map<String,Object> 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<start;i++){

sb.append(PROCESS_FILL_FLAG);

}

out = sb.toString()+out;

sb = new StringBuffer();

for(int i=0;i<end;i++){

sb.append(PROCESS_FILL_FLAG);

}

out = out+sb.toString();

if(objects !=null){

for (Map.Entry<String, Object> 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);

}

}


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP