SpringBoot使用日志打印

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014248473/article/details/88072072

一、使用默认配置

当你没有配置任何日志打印设置,看看SpringBoot默认打印方式如何?

在一个服务方法中这样写:

使用的是commons-logging,被整在spring-jcl模块中去了。

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

在类中声明一个log变量。

private static final Log log = LogFactory.getLog(StudentService.class);

服务方法中调用日志打印方法。

log.trace("trace info...");
log.debug("debug info...");
log.info("info info...");
log.warn("warn info...");
log.error("error info...");
log.fatal("fatal info...");

最后在控制台的输出效果:

2019-03-02 08:40:55.708  INFO 4904 --- [nio-8080-exec-1] com.yale.StudentService                  : info info...
2019-03-02 08:40:55.708  WARN 4904 --- [nio-8080-exec-1] com.yale.StudentService                  : warn info...
2019-03-02 08:40:55.708 ERROR 4904 --- [nio-8080-exec-1] com.yale.StudentService                  : error info...
2019-03-02 08:40:55.708 ERROR 4904 --- [nio-8080-exec-1] com.yale.StudentService                  : fatal info...

可以看出,trace和debug并没有打印。默认级别是info。

二、使用application.yml配置

在你开发的时候想用debug,就需要在application.yml中配置了。

logging:
  file: target/app.log
  level:
    ROOT: WARN
    com.yale: DEBUG

设置了三点:
1.日志文件路径:target/app.log;
2.规定了ROOT的级别:WARN;
3.指定包com.yale的级别:DEBUG。

重新启动应用之后,调用服务,

在控制台输出:

2019-03-02 09:07:57.904 DEBUG 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : debug info...
2019-03-02 09:07:57.905  INFO 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : info info...
2019-03-02 09:07:57.905  WARN 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : warn info...
2019-03-02 09:07:57.905 ERROR 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : error info...
2019-03-02 09:07:57.905 ERROR 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : fatal info...

很明显,debug信息也打印出来了!

但是打开app.log文件,去发现只有这些:

2019-03-02 09:03:36.099  INFO 4904 --- [restartedMain] com.yale.Application                     : Starting Application on USER-20160707JH with PID 4904 (D:\idea_workspace\springboot_test\springboot-rest\target\classes started by Administrator in D:\idea_workspace\springboot_test\springboot-rest)
2019-03-02 09:03:36.100 DEBUG 4904 --- [restartedMain] com.yale.Application                     : Running with Spring Boot v2.0.0.RELEASE, Spring v5.0.4.RELEASE
2019-03-02 09:03:36.100  INFO 4904 --- [restartedMain] com.yale.Application                     : No active profile set, falling back to default profiles: default
2019-03-02 09:03:37.412  INFO 4904 --- [restartedMain] com.yale.Application                     : Started Application in 1.423 seconds (JVM running for 1395.999)

然后再调用一次服务,

在控制台输出:

2019-03-02 09:07:57.904 DEBUG 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : debug info...
2019-03-02 09:07:57.905  INFO 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : info info...
2019-03-02 09:07:57.905  WARN 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : warn info...
2019-03-02 09:07:57.905 ERROR 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : error info...
2019-03-02 09:07:57.905 ERROR 4516 --- [nio-8080-exec-1] com.yale.StudentService                  : fatal info...
2019-03-02 09:08:50.085 DEBUG 4516 --- [nio-8080-exec-4] com.yale.StudentService                  : debug info...
2019-03-02 09:08:50.086  INFO 4516 --- [nio-8080-exec-4] com.yale.StudentService                  : info info...
2019-03-02 09:08:50.086  WARN 4516 --- [nio-8080-exec-4] com.yale.StudentService                  : warn info...
2019-03-02 09:08:50.086 ERROR 4516 --- [nio-8080-exec-4] com.yale.StudentService                  : error info...
2019-03-02 09:08:50.086 ERROR 4516 --- [nio-8080-exec-4] com.yale.StudentService                  : fatal info...

两次打印输出都在。

这个时候,app.log文件神奇的将上一次的也打印出来了:

2019-03-02 09:03:36.099  INFO 4904 --- [restartedMain] com.yale.Application                     : Starting Application on USER-20160707JH with PID 4904 (D:\idea_workspace\springboot_test\springboot-rest\target\classes started by Administrator in D:\idea_workspace\springboot_test\springboot-rest)
2019-03-02 09:03:36.100 DEBUG 4904 --- [restartedMain] com.yale.Application                     : Running with Spring Boot v2.0.0.RELEASE, Spring v5.0.4.RELEASE
2019-03-02 09:03:36.100  INFO 4904 --- [restartedMain] com.yale.Application                     : No active profile set, falling back to default profiles: default
2019-03-02 09:03:37.412  INFO 4904 --- [restartedMain] com.yale.Application                     : Started Application in 1.423 seconds (JVM running for 1395.999)
2019-03-02 09:03:47.852  INFO 4516 --- [restartedMain] com.yale.Application                     : Starting Application on USER-20160707JH with PID 4516 (D:\idea_workspace\springboot_test\springboot-rest\target\classes started by Administrator in D:\idea_workspace\springboot_test\springboot-rest)
2019-03-02 09:03:47.854 DEBUG 4516 --- [restartedMain] com.yale.Application                     : Running with Spring Boot v2.0.0.RELEASE, Spring v5.0.4.RELEASE
2019-03-02 09:03:47.856  INFO 4516 --- [restartedMain] com.yale.Application                     : No active profile set, falling back to default profiles: default
2019-03-02 09:03:51.816  INFO 4516 --- [restartedMain] com.yale.Application                     : Started Application in 4.505 seconds (JVM running for 5.027)
2019-03-02 09:07:57.904 DEBUG 4516 --- [http-nio-8080-exec-1] com.yale.StudentService                  : debug info...
2019-03-02 09:07:57.905  INFO 4516 --- [http-nio-8080-exec-1] com.yale.StudentService                  : info info...
2019-03-02 09:07:57.905  WARN 4516 --- [http-nio-8080-exec-1] com.yale.StudentService                  : warn info...
2019-03-02 09:07:57.905 ERROR 4516 --- [http-nio-8080-exec-1] com.yale.StudentService                  : error info...
2019-03-02 09:07:57.905 ERROR 4516 --- [http-nio-8080-exec-1] com.yale.StudentService                  : fatal info...
2019-03-02 09:08:50.085 DEBUG 4516 --- [http-nio-8080-exec-4] com.yale.StudentService                  : debug info...
2019-03-02 09:08:50.086  INFO 4516 --- [http-nio-8080-exec-4] com.yale.StudentService                  : info info...
2019-03-02 09:08:50.086  WARN 4516 --- [http-nio-8080-exec-4] com.yale.StudentService                  : warn info...
2019-03-02 09:08:50.086 ERROR 4516 --- [http-nio-8080-exec-4] com.yale.StudentService                  : error info...
2019-03-02 09:08:50.086 ERROR 4516 --- [http-nio-8080-exec-4] com.yale.StudentService                  : fatal info...

所以说,第一次使用文件记录日志,第一时间没有打印任何日志的时候,千万不要着急,可以重新调用记录日志的方法,再回过头来看看!

猜你喜欢

转载自blog.csdn.net/u014248473/article/details/88072072