Logger类下有多个不同的error方法,根据传入参数的个数及类型的不同,自动选择不同的重载方法。
使用+,只打印异常,不打印堆栈信息
private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);@Testpublic void logEorrorTest(){try {int a=0;int c = 5/a;System.out.println(c);}catch (Exception e){
// 使用“+”,知打印异常,不打堆栈信息logger.error("use + : "+e);}}
}
输出:
11:17:40.068 [main] ERROR c.p.b.LoggerTest - use + : java.lang.ArithmeticException: / by zero
使用“,”,打印堆栈信息
private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);@Testpublic void logEorrorTest(){try {int a=0;int c = 5/a;System.out.println(c);}catch (Exception e){
// 使用“,”,只打印异常,不打堆栈信息logger.error("use , : ",e);}}
输出:
java.lang.ArithmeticException: / by zeroat com.pingan.bank.LoggerTest.logEorrorTest(LoggerTest.java:14)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.junit.runner.JUnitCore.run(JUnitCore.java:160)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
使用分隔符,第二个参数为Throwable,会发现分隔符没有起作用
private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);@Testpublic void logEorrorTest(){try {int a=0;int c = 5/a;System.out.println(c);}catch (Exception e){
// 使用“{}”分隔符,不起作用logger.error("use , : {}",e);}}
输出:
11:24:34.082 [main] ERROR c.p.b.LoggerTest - use , : {}
java.lang.ArithmeticException: / by zeroat com.pingan.bank.LoggerTest.logEorrorTest(LoggerTest.java:14)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.junit.runner.JUnitCore.run(JUnitCore.java:160)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
使用分隔符,第二个参数为Object,会发现分隔符起作用
private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);@Testpublic void logEorrorTest(){try {int a=0;int c = 5/a;System.out.println(c);}catch (Exception e){
// 第二个参数为Oject类,分割符起作用logger.error("use , : {}","end");}}
输出:
11:27:07.019 [main] ERROR c.p.b.LoggerTest - use , : end