怎样打印日志
以Slf4j+logbak为例:
代码如下:
@Slf4j public class TestThrowable { public static void main(String args[]){ try{ returnNum(new int[]{1}); }catch (Exception e){ log.error("错误信息" + e.toString()); log.info("----------"); log.error("错误信息" + e.getMessage()); log.info("----------"); log.error("错误信息:{}", e.toString()); log.info("----------"); log.error("错误信息:{}", e); log.info("----------"); log.error("错误信息:", e); log.info("----------"); log.error("错误码:{} 错误信息:", "0001", e); } } private static int returnNum (int [] arr) { if (arr[0] == 1) { return returnA(arr); } return -1; } private static int returnA (int [] arr) { return arr[10]; } }
运行结果如下:
16:11:39.307 [main] ERROR com.test.TestThrowable - 错误信息java.lang.ArrayIndexOutOfBoundsException: 10 16:11:39.314 [main] INFO com.test.TestThrowable - ---------- 16:11:39.314 [main] ERROR com.test.TestThrowable - 错误信息10 16:11:39.314 [main] INFO com.test.TestThrowable - ---------- 16:11:39.314 [main] ERROR com.test.TestThrowable - 错误信息:java.lang.ArrayIndexOutOfBoundsException: 10 16:11:39.317 [main] INFO com.test.TestThrowable - ---------- 16:11:39.323 [main] ERROR com.test.TestThrowable - 错误信息:{} java.lang.ArrayIndexOutOfBoundsException: 10 at com.test.TestThrowable.returnA(TestThrowable.java:45) at com.test.TestThrowable.returnNum(TestThrowable.java:38) at com.test.TestThrowable.main(TestThrowable.java:20) 16:11:39.324 [main] INFO com.test.TestThrowable - ---------- 16:11:39.324 [main] ERROR com.test.TestThrowable - 错误信息: java.lang.ArrayIndexOutOfBoundsException: 10 at com.test.TestThrowable.returnA(TestThrowable.java:45) at com.test.TestThrowable.returnNum(TestThrowable.java:38) at com.test.TestThrowable.main(TestThrowable.java:20) 16:11:39.324 [main] INFO com.test.TestThrowable - ---------- 16:43:54.855 [main] ERROR com.test.TestThrowable - 错误码:0001 错误信息: java.lang.ArrayIndexOutOfBoundsException: 10 at com.test.TestThrowable.returnA(TestThrowable.java:46) at com.test.TestThrowable.returnNum(TestThrowable.java:39) at com.test.TestThrowable.main(TestThrowable.java:20)
得到结果:
1、e.toString()返回异常类型和错误信息描述(堆栈信息不完整,不会显示错误的具体方法,发生在哪行代码)
2、e.getMessage()返回错误信息描述(堆栈信息不完整,不会显示错误的具体方法,发生在哪行代码)
3、log.error("xxx"+e.toString()) 打印堆栈信息不完整,不会显示错误的具体方法,发生在哪行代码
4、log.error("xxx"+e.getMessage()) 打印堆栈信息不完整,不会显示错误的具体方法,发生在哪行代码
5、log.error("xxx", e) 打印完整的堆栈信息,可以查看到错误发生在哪行代码
6、log.error("错误码:{} 错误信息:", "0001", e); 这个打印格式是最好的
========================================================================================
Throwable的toString()源码
/** * Returns a short description of this throwable. * The result is the concatenation of: * <ul> * <li> the {@linkplain Class#getName() name} of the class of this object * <li> ": " (a colon and a space) * <li> the result of invoking this object's {@link #getLocalizedMessage} * method * </ul> * If {@code getLocalizedMessage} returns {@code null}, then just * the class name is returned. * * @return a string representation of this throwable. */ public String toString() { String s = getClass().getName(); String message = getLocalizedMessage(); return (message != null) ? (s + ": " + message) : s; }
Throwable的getLocalizedMessage()源码
/** * Creates a localized description of this throwable. * Subclasses may override this method in order to produce a * locale-specific message. For subclasses that do not override this * method, the default implementation returns the same result as * {@code getMessage()}. * * @return The localized description of this throwable. * @since JDK1.1 */ public String getLocalizedMessage() { return getMessage(); }
Throwable的getMessage()源码
/** * Returns the detail message string of this throwable. * * @return the detail message string of this {@code Throwable} instance * (which may be {@code null}). */ public String getMessage() { return detailMessage; }
log源码未分析,未完、待续。