java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot logback-spring.xml无法加载

Springboot logback-spring.xml无法加载问题

作者:zhangnero

这篇文章主要介绍了Springboot logback-spring.xml无法加载问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Springboot logback-spring.xml无法加载

现象

SpringBoot项目,启动后,日志文件没有出现在logback-spring.xml指定的目录下

且启动日志开头如下:

"C:\Program Files\Java\jdk1.8.0_144\bin\java"...
Connected to the target VM, address: '127.0.0.1:52494', transport: 'socket'
18:34:58,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
18:34:58,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
18:34:58,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/D:/mvn_repo/xxx-1.0.2.jar!/logback.xml]
18:34:58,014 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@707194ba - URL [jar:file:/D:/mvn_repo/yyy-1.0.2.jar!/logback.xml] is not of type file
18:34:58,112 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [jar:file:/D:/mvn_repo/zzz-1.0.2.jar!/logback.xml] 

logback-spring.xml文件一直未改动过,如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

    <springProperty scope="context" name="logPath" source="log.path"/>
    <springProperty scope="context" name="logLevel" source="log.level"/>


    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} %line - %msg%n</pattern>
        </layout>
    </appender>

    <appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--输出路径-->
            <fileNamePattern>${logPath}/server.%d.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} %line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--输出路径-->
            <fileNamePattern>${logPath}/server.%d.error</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} %line - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <root level="${logLevel}">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</Configuration>

原因分析

1.配置文件位置正确

classes/
├── application.properties
├── com
│   └── nero
│       └── test
│           ├── api
│           │   ├── Api.class

2.分析启动日志,分析可能是依赖的jar中有logback.xml配置,冲突导致的问题

解决方案

在application.properties中加入配置:

logging.config=classpath:logback-spring.xml

通过该配置,强制指定日志的配置文件,问题得以解决

springboot logback-spring.xml 配置, 使用不同配置文件中的配置

application.yml

spring:
  profiles:
    active: prod  # active 哪一个配置文件

application-dev.yml

logging:
  file:
    path: d:/var/log

application-prod.yml

logging:
  file:
    path: /var/log/question

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>wechat-service</contextName>
    <!-- !!!!!!!!!获取 application-{xxx}.yml 的配置 !!!!!-->
    <springProperty scope="context" name="log.path" source="logging.file.path"/>
    <!--<property name="log.path" value="log" />-->
    <property name="log.maxHistory" value="15" />
    <property name="log.colorPattern" value="%d{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | %msg%n"/>
    <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n" />
    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.colorPattern}</pattern>
        </encoder>
    </appender>
    <!--输出到文件 info 级别的 log-->
    <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/info/info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <MaxHistory>${log.maxHistory}</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!--输出到文件 error 级别的 log-->
    <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/error/error.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <root level="debug">
        <appender-ref ref="console" />
    </root>
    <root level="info">
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    </root>

</configuration>

从application-xxx.yml 中拿配置

<springProperty scope="context" name="log.path" source="logging.file.path"/>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文