为了账号安全,请及时绑定邮箱和手机立即绑定

基于slf4j的日志工具类

标签:
Java

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);

}

}


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消