Spring启动不会加载logback-spring.xml

时间:2018-05-29 04:53:06

标签: java spring spring-boot logback

我有一个示例Spring Boot应用程序,它使用Logback进行日志记录。 所以我在jar旁边有logback-spring.xml来配置日志记录,但是除非我用logging.config指定它,例如logging.config=logback-spring.xml,否则它不起作用。

我已查看Spring Boot ignoring logback-spring.xml,其中可能是因为某处spring.xml已经存在org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(LoggingInitializationContext, LogFile),但在{{1}}上设置断点会显示logFile为空。< / p>

我在这里做错了吗?

6 个答案:

答案 0 :(得分:11)

默认情况下,Spring会查找jar文件之外的资源。 如果要使用外部logback配置文件,则必须在启动jar时传递它的位置:

$ java -jar -Dlogback.configurationFile=/full_path/logback.xml app.jar

请不要将logback.xml包含到最终的Jar文件中,它会在类路径中导致多个logback.xml文件。

答案 1 :(得分:2)

根据问题描述,您使用的是日志配置的外化版本。该文件保存在jar外面。因此,您必须将路径作为运行时参数提及如下:

-Dlogging.config=file:logback-spring.xml

或者在application.properties中提及相同的属性,如下所示:

logging.config=file:logback-spring.xml

它从资源文件夹中获取文件的原因,因为它以spring的方式配置。 Spring通过classpath中的以下名称获取logback文件。

logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy

请查看spring-boot custom log configuration

上的相关文档

答案 2 :(得分:0)

这种行为可能有两个原因:

原因1: logback-spring.xml在某种程度上不在类路径中。您可以在运行时通过打印System.getProperty("java.class.path")并检查输出中是否存在包含logback-spring.xml的文件夹来验证这一点。

原因2 :如果原因1不是问题,那么类路径中已经存在名为logback.xml或logback-spring.xml的文件,这可能会导致冲突。现在,你必须找到该文件。您可以尝试将logback-spring.xml重命名为logback.xml并检查行为。

答案 3 :(得分:0)

只需在 logback-spring.xml

中定义这些行
    var data = new FormData();
    $.each(files, function(key, value)
    {
        data.append("file", value);
    });

    $.ajax({
        url: 'upload.php?files',
        type: 'POST',
        data: data,
        cache: false,
        dataType: 'json',
        processData: false, // Don't process the files
        contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        success: function(data, textStatus, jqXHR)
        {
            alert(data);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            // Handle errors here
            alert('ERRORS: ' + textStatus);
            // STOP LOADING SPINNER
        }
    });

答案 4 :(得分:0)

我不知道为什么它不适合你。我在资源文件夹中创建了一个 logback-spring.xml 文件,它运行正常。

以下是日志文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <property name="LOGDIR" value="logs"></property>
    <property name="APP_NAME" value="spring-boot-sample"></property>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d ${APP_NAME} %-5level [%thread] %logger: %msg%n</Pattern>
        </layout>
    </appender>

    <appender name="ROLLINGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGDIR}/${APP_NAME}-log.%d{MM-dd-yyyy}.log</fileNamePattern>
            <maxHistory>90</maxHistory>
        </rollingPolicy>
        <encoder>
            <charset>utf-8</charset>
            <Pattern>%d ${APP_NAME} %-5level [%thread] %logger: %msg%n</Pattern>
        </encoder>
    </appender>

    <springProfile name="local">
        <root level="debug">
            <appender-ref ref="CONSOLE"/>
        </root>
        <logger name="co.jp.oha" additivity="false" level="debug">
            <appender-ref ref="ROLLINGFILE"/>
            <appender-ref ref="STDOUT"/>
        </logger>
    </springProfile>
</configuration>

您可以尝试使用它们。我希望它会对你有所帮助。

答案 5 :(得分:0)

Dockerfile:

COPY /rootProjectName/src/main/resources/logback-spring.xml /deployments/  

application-dev.properties:

logging.config=classpath:logback-spring.xml

我正在运行一个Docker容器,必须将资源文件夹复制到我的Docker File中的部署文件夹中...但是一旦复制到该文件夹​​中
这是最适合我的logging.config值(添加类路径字)

相关问题