美文网首页
Spring Boot日志打印问题及资料

Spring Boot日志打印问题及资料

作者: 梦想又照进现实 | 来源:发表于2020-03-09 09:55 被阅读0次

    背景

    最近在一个Boot应用中使用一个SDK包时候发现这个SDK包中的日志打印不出来,经过结合其源码的调试,发现到了日志打印行执行了,但确实没有打印到控制台,基本断定是日志配置问题。

    冲突原因

    Boot应用中的日志配置有 log4j.properties,但没有在pom文件中添加log4j的依赖和web的排除,相当于失效了,打印日志还是按默认的打印,而SDK包中代码理使用的是log4j的使用方式,这样就导致Boot应用打印不出SDK包中的日志信息;

    解决过程

    默认打印日志格式:

     
    2020-03-09 09:36:54.720  INFO 39140 --- [  restartedMain] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
    2020-03-09 09:36:54.725  INFO 39140 --- [  restartedMain] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
    2020-03-09 09:36:55.526  INFO 39140 --- [  restartedMain] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application adms-query-service with eureka with status UP
    2020-03-09 09:36:55.578  INFO 39140 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8082 (http)
    2020-03-09 09:36:55.579  INFO 39140 --- [  restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8082
    2020-03-09 09:36:55.582  INFO 39140 --- [  restartedMain] c.c.c.r.a.AdmsQueryServiceApplication    : Started AdmsQueryServiceApplication in 6.992 seconds (JVM running for 8.427)
    2020-03-09 09:37:21.723  INFO 39140 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
    2020-03-09 09:37:21.750  INFO 39140 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 27 ms
    2020-03-09 09:37:21.790  INFO 39140 --- [nio-8082-exec-1] c.c.c.r.a.c.PadLifeQueryController       : uid=CMBFRT---signature=ddd0ea7f5f16175720ce72c6699edf34
    2020-03-09 09:37:21.834  INFO 39140 --- [nio-8082-exec-1] c.c.c.r.a.c.PadLifeQueryController       : 请求入参:{"ctfNbr":"100000000000000001","ctfCod":"01"}
    

    在Boot应用进行了Log4j的依赖定义,pom依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion> 
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.3.8.RELEASE</version>
    </dependency>
    

    添加后的打印日志已经按log4j.properties定义的进行打印,以[INFO ]为前缀打印info级别日志:

     method:org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:201)Tomcat started on port(s): 8082 (http)
    [INFO ] 2020-03-09 09:40:13,326 method:org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.onApplicationEvent(EurekaAutoServiceRegistration.java:124)Updating port to 8082
    [INFO ] 2020-03-09 09:40:13,329 method:org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:57)Started AdmsQueryServiceApplication in 6.726 seconds (JVM running for 8.02)
    [INFO ] 2020-03-09 09:40:13,345 method:com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:813)DiscoveryClient_ADMS-QUERY-SERVICE/80374563-ZDY.itc.cmbchina.cn:adms-query-service:8082 - registration status: 204
    [INFO ] 2020-03-09 09:40:29,485 method:org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)FrameworkServlet 'dispatcherServlet': initialization started
    [INFO ] 2020-03-09 09:40:29,510 method:org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:508)FrameworkServlet 'dispatcherServlet': initialization completed in 25 ms
    

    相关log配置

    springboot默认日志:Logback

    日志格式:

    时间日期:精确到毫秒
    日志级别:ERROR, WARN, INFO, DEBUG or TRACE
    进程ID
    分隔符:--- 标识实际日志的开始
    线程名:方括号括起来(可能会截断控制台输出)
    Logger名:通常使用源代码的类名
    日志内容

    springboot中的jar依赖

    spring-boot-starter中spring-boot-starter-logging

    日志配置文件

    Springboot下自定义日志配置,不同的日志系统,可以按如下规则组织配置文件名:
    Logback:logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
    Log4j:log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml
    Log4j2:log4j2-spring.xml, log4j2.xml
    JDK (Java Util Logging):logging.properties

    相关文章

      网友评论

          本文标题:Spring Boot日志打印问题及资料

          本文链接:https://www.haomeiwen.com/subject/vqtudhtx.html