StackTraceElement s= e.getStackTrace()[0];
s.getFileName()
s.getMethodName()
s.getLineNumber()
e.getMessage()
e.getClassName()
logger.error("错误堆栈", e);
获取堆栈信息,如果直接打印e,则会将它toString
/**
* 获取堆栈信息
* @param throwable
* @return
*/
public static String getStackTrace(Throwable throwable){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try
{
throwable.printStackTrace(pw);
return sw.toString();
} finally
{
pw.close();
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Desc: 异常打印日志 ,提供给打印非正常异常
* @Author HealerJean
* @Date 2018/8/21 下午6:41.
*/
public static void log(Throwable e,Class c){
Logger logger = LoggerFactory.getLogger(c);
// logger.error("错误堆栈", e);
StackTraceElement s= e.getStackTrace()[0];//数组长度为 1
logger.error("\n\n-----------------"+
"\n报错文件名:"+s.getFileName()+
"\n报错的类:"+s.getClassName()+
"\n报错方法::"+s.getMethodName()+
"\n报错的行:"+ s.getLineNumber()+
"\n报错的message:"+ e.getMessage()+
"\n错误堆栈:\n"+getStackTrace(e)+
"\n------------------\n\n");
}
public static void main(String[] args) {
try {
int i =1/0 ;
}catch (Exception e){
log(e,ExceptionLogUtils.class );
}
}
11:40:53.732 [main] ERROR com.duodian.youhui.admin.utils.ExceptionLogUtils -
-----------------
报错文件名:ExceptionLogUtils.java
报错的类:com.duodian.youhui.admin.utils.ExceptionLogUtils
报错方法::main
报错的行:68
报错的message:/ by zero
错误堆栈:
java.lang.ArithmeticException: / by zero
at com.duodian.youhui.admin.utils.ExceptionLogUtils.main(ExceptionLogUtils.java:68)
------------------
log.info("cacheMsg RECUR_FIGHT A: roomId = {}, userId = {}, toUid = {}", room.getRoomId(), ctx.getUserId(), toUid);
public static void logInfo(String msg,Class c){
Logger logger = LoggerFactory.getLogger(c);
String location="";
StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
System.out.println(stacks.length); //长度为3
for(StackTraceElement stackTraceElement:stacks){
logger.info("\n\n**************"+
"\n打印文件名:"+stackTraceElement.getFileName() +
"\n打印类名:"+ stackTraceElement.getClassName() +
"\n方法名:" + stackTraceElement.getMethodName() +
"\n行号:" + stackTraceElement.getLineNumber() +
"\n打印内容:"+msg+
"\n**************\n\n");
System.out.println(location);
}
}
1、是当前线程,2、当前打印所在方法,3、是调用该方法的类,那么我们需要的就是第三个
public static void main(String[] args) {
logInfo("HealerJean",ExceptionLogUtils.class);
}
3
11:44:47.685 [main] INFO com.duodian.youhui.admin.utils.ExceptionLogUtils -
**************
打印文件名:Thread.java
打印类名:java.lang.Thread
方法名:getStackTrace
行号:1559
打印内容:HealerJean
**************
11:44:47.689 [main] INFO com.duodian.youhui.admin.utils.ExceptionLogUtils -
**************
打印文件名:ExceptionLogUtils.java
打印类名:com.duodian.youhui.admin.utils.ExceptionLogUtils
方法名:logInfo
行号:31
打印内容:HealerJean
**************
11:44:47.689 [main] INFO com.duodian.youhui.admin.utils.ExceptionLogUtils -
**************
打印文件名:ExceptionLogUtils.java
打印类名:com.duodian.youhui.admin.utils.ExceptionLogUtils
方法名:main
行号:49
打印内容:HealerJean
**************
package com.duodian.youhui.admin.utils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* @Desc: 异常打印日志 ,提供给打印非正常异常
* @Author HealerJean
* @Date 2018/8/21 下午6:41.
*/
@Slf4j
public class ExceptionLogUtils {
public static void log(Throwable e,Class c){
Logger logger = LoggerFactory.getLogger(c);
// logger.error("错误堆栈", e);
StackTraceElement s= e.getStackTrace()[0];//数组长度为 1
logger.error("\n\n-----------------"+
"\n报错文件名:"+s.getFileName()+
"\n报错的类:"+s.getClassName()+
"\n报错方法::"+s.getMethodName()+
"\n报错的行:"+ s.getLineNumber()+
"\n报错的message:"+ e.getMessage()+
"\n错误堆栈:\n"+getStackTrace(e)+
"\n------------------\n\n");
}
/**
* 获取堆栈信息
* @param throwable
* @return
*/
public static String getStackTrace(Throwable throwable){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try
{
throwable.printStackTrace(pw);
return sw.toString();
} finally
{
pw.close();
}
}
public static void logInfo(String msg,Class c){
Logger logger = LoggerFactory.getLogger(c);
StackTraceElement[] stacks = Thread.currentThread().getStackTrace();//数组长度为 3
logger.info("\n\n**************"+
"\n打印文件名:"+stacks[2].getFileName() +
"\n打印类名:"+ stacks[2].getClassName() +
"\n方法名:" + stacks[2].getMethodName() +
"\n行号:" + stacks[2].getLineNumber() +
"\n打印内容:"+msg+
"\n**************\n\n");
}
}
log.info("admin[{}],[{}],[{}];请求地址:[{}];访问ip:[{}]", CasConfig.RemoteUserUtil.getRemoteUserId(),CasConfig.RemoteUserUtil.getRemoteUserAccount(),CasConfig.RemoteUserUtil.getRemoteUserName(),request.getRequestURL(), IpUtil.getIp());