【logger.error()】介绍;(只是将日志打印在Console控制台)

本篇博客主要目的是:在OA系统十八:请假功能四:编写Controller代码;(实现请假申请的控制器;仅仅涉及新增部分的Controller代码)遇到了打印日志的问题;对其中的【logger.error()】不是特别清楚,本篇博客就是详细阐述这个问题。

首先可以参考:MyBatis进阶一:MyBatis日志管理;(【如何输出日志到日志文件中】待补充……)这篇博客介绍了日志的基本内容。

本篇博客参考自:【java】打印错误日志推荐使用logger.error("异常", e);作者是:【coolcoffee168】 。

 目录

创建测试类LoggerTest:来演示logger.error()的相关方法

说明:

附:让日志输出到一个日志文件中,而不是输出到Console控制台中(待解决)


创建测试类LoggerTest:来演示logger.error()的相关方法

package com.tt;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggerTest {
    private Logger logger = LoggerFactory.getLogger(LoggerTest.class);
    @Test
    public void testLogger(){
        try {
            int i = 10 / 0;
        }catch (Exception e){
            logger.info("直接打印e:error(String.valueOf(e))");
            logger.error(String.valueOf(e));
            logger.info("也算是直接打印e了:error(\"异常\" + e)");
            logger.error("异常" + e);
            logger.info("打印e的toString():error(e.toString())");
            logger.error(e.toString());
            logger.info("打印e的message:error(e.getMessage())");
            logger.error(e.getMessage());
            logger.info("通过这种方式打印e:error(\"异常\", e)");
            logger.error("异常", e);
        }
    }
}

运行结果:

"C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53508,suspend=y,server=n -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Users\dell\.IntelliJIdea2019.3\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;E:\java\imooc-oa\target\test-classes;E:\java\imooc-oa\target\classes;E:\maven-repo\org\mybatis\mybatis\3.5.3\mybatis-3.5.3.jar;E:\maven-repo\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar;E:\maven-repo\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;E:\maven-repo\com\alibaba\druid\1.1.14\druid-1.1.14.jar;E:\maven-repo\junit\junit\4.12\junit-4.12.jar;E:\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\maven-repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\maven-repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\maven-repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\maven-repo\org\freemarker\freemarker\2.3.9\freemarker-2.3.9.jar;E:\maven-repo\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;E:\maven-repo\com\alibaba\fastjson\1.2.62\fastjson-1.2.62.jar;E:\maven-repo\commons-codec\commons-codec\1.14\commons-codec-1.14.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.tt.LoggerTest,testLogger
Connected to the target VM, address: '127.0.0.1:53508', transport: 'socket'
08:18:42:715 [main] INFO  com.tt.LoggerTest - 直接打印e:error(String.valueOf(e))
08:18:42:717 [main] ERROR com.tt.LoggerTest - java.lang.ArithmeticException: / by zero
08:18:42:718 [main] INFO  com.tt.LoggerTest - 也算是直接打印e了:error("异常" + e)
08:18:42:718 [main] ERROR com.tt.LoggerTest - 异常java.lang.ArithmeticException: / by zero
08:18:42:718 [main] INFO  com.tt.LoggerTest - 打印e的toString():error(e.toString())
08:18:42:718 [main] ERROR com.tt.LoggerTest - java.lang.ArithmeticException: / by zero
08:18:42:718 [main] INFO  com.tt.LoggerTest - 打印e的message:error(e.getMessage())
08:18:42:718 [main] ERROR com.tt.LoggerTest - / by zero
08:18:42:718 [main] INFO  com.tt.LoggerTest - 通过这种方式打印e:error("异常", e)
08:18:42:720 [main] ERROR com.tt.LoggerTest - 异常
java.lang.ArithmeticException: / by zero
	at com.tt.LoggerTest.testLogger(LoggerTest.java:12)
	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:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Disconnected from the target VM, address: '127.0.0.1:53508', transport: 'socket'

Process finished with exit code 0

 

说明:

(1)

        ● 经观察结果发现,只有logger.error("异常", e)这种写法可以详细的打印栈跟踪信息,这种方式能够定位到具体异常发生位置,能够让我们迅速定位到错误原因;(很显然,只有打印了异常的堆栈信息,才能据此定位异常嘛)

        ● 强烈不推荐logger.error(e.getMessage())这种方式,因为e.getMessage()可能为空,导致看不到任何错误信息。

【logger.error()】介绍;(只是将日志打印在Console控制台)

(2)补充说明,error()方法的几种重载形式;

        ● error()几种重载形式,强烈建议使用error(String e,Throwable throwable)这种形式。

【logger.error()】介绍;(只是将日志打印在Console控制台)

 

附:让日志输出到一个日志文件中,而不是输出到Console控制台中(待解决)

本部分,待补充……

 

上一篇:Java异常处理总结


下一篇:00使用Junit