spring SpringBoot 与 LogBack 创建 LOG_PATH_IS_UNDEFINED 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25251983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:58:24  来源:igfitidea点击:

SpringBoot with LogBack creating LOG_PATH_IS_UNDEFINED folder

springlogbackspring-boot

提问by Davi Alves

I am using SpringBoot with LogBack and using the below configuration in my yml file:

我正在使用带有 LogBack 的 SpringBoot 并在我的 yml 文件中使用以下配置:

logging:
    path: C:/var/log/pincode

The logging.path Spring Environment Variable is transferred to the LOG_PATH Environment variable and the log file is placed at the correct place, but there is also a directory called LOG_PATH_IS_UNDEFINED created in the root directory of my project.

将 logging.path Spring Environment Variable 转移到 LOG_PATH Environment 变量中,并将日志文件放置在正确的位置,但是在我的项目的根目录中还创建了一个名为 LOG_PATH_IS_UNDEFINED 的目录。

This seems to be caused by the different phase used by SpringBoot to configure LogBack with its Environment variables.

这似乎是由 SpringBoot 使用其环境变量配置 LogBack 的不同阶段引起的。

17:29:21,325 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:29:21,337 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern LOG_PATH_IS_UNDEFINED/catalina.out.%d{yyyy-MM-dd} for the active file
17:29:21,340 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'LOG_PATH_IS_UNDEFINED/catalina.out.%d{yyyy-MM-dd}'.
17:29:21,340 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
17:29:21,343 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Aug 11 17:24:07 BRT 2014
17:29:21,346 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - Active log file name: LOG_PATH_IS_UNDEFINED/catalina.out
17:29:21,346 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - File property is set to [LOG_PATH_IS_UNDEFINED/catalina.out]
...
17:29:21,358 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

And then after that it start configuring logback again but this time using the path i set:

然后它再次开始配置logback,但这次使用我设置的路径:

17:29:21,672 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:29:21,673 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
17:29:21,673 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern C:/var/log/pincode//catalina.out.%d{yyyy-MM-dd} for the active file
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'C:/var/log/pincode//catalina.out.%d{yyyy-MM-dd}'.
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Aug 11 17:29:21 BRT 2014
17:29:21,674 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - Active log file name: C:/var/log/pincode//catalina.out
17:29:21,674 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - File property is set to [C:/var/log/pincode//catalina.out]
...
17:29:21,685 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

My logback.xml

我的 logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<include resource="org/springframework/boot/logging/logback/basic.xml" />
<property name="FILE_LOG_PATTERN"
    value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex" />

<appender name="serverConsole"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <Append>true</Append>
    <File>${LOG_PATH}/catalina.out</File>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/catalina.out.%d{yyyy-MM-dd}
        </fileNamePattern>
        <maxHistory>15</maxHistory>
    </rollingPolicy>
</appender>

<!-- Plain Text Rolling Appender -->
<appender name="server"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <Append>true</Append>
    <File>${LOG_PATH}/pincode.log</File>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>INFO</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/pincode.log.%d{yyyy-MM-dd}
        </fileNamePattern>
        <maxHistory>15</maxHistory>
    </rollingPolicy>
</appender>

<!-- Plain Text Rolling Appender -->
<appender name="server-error"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <Append>true</Append>
    <File>${LOG_PATH}/pincode-error.log</File>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/pincode-error.log.%d{yyyy-MM-dd}
        </fileNamePattern>
        <maxHistory>15</maxHistory>
    </rollingPolicy>
</appender>

<logger name="com.app" level="INFO">
    <appender-ref ref="server" />
    <appender-ref ref="server-error" />
</logger>

<root level="INFO">
    <appender-ref ref="serverConsole" />
</root> 

If I remove my logback.xml file from the project it doesn't create the folder, so somewhere Spring is loading the xml before parsing the yml?

如果我从项目中删除我的 logback.xml 文件,它不会创建文件夹,所以在解析 yml 之前 Spring 正在加载 xml?

How can I avoid Logback to create this LOG_PATH_IS_UNDEFINED directory?

如何避免 Logback 创建这个 LOG_PATH_IS_UNDEFINED 目录?

回答by Igor Tytar

In your case LOG_PATHis not defined on startup. You should use ${LOG_PATH:-.}instead , See.

在您的情况下LOG_PATH未在启动时定义。您应该${LOG_PATH:-.}改用 ,请参阅

But if you define logging.pathin your application.propertiesyou will see two log files in .and in ${logging.path}directory.

但是如果你logging.path在你的目录中定义,application.properties你会.${logging.path}目录中看到两个日志文件。

Spring container set LOG_PATHafter Logback initialization... Logback is not supported lazy file creation as far as I know. In this case you should use logback-spring.xmlinstead logback.xml.

LOG_PATH在 Logback 初始化后设置 Spring 容器......据我所知,不支持 Logback 延迟文件创建。在这种情况下,你应该使用logback-spring.xml来代替logback.xml

回答by Sabir Khan

I faced similar issue and its easy to solve it. Basically , concept is that Spring Boot already gives you System property - LOG_PATHfor Spring Boot property - logging.pathso you define logging.pathin your application.properties and simply use LOG_PATHin your logback configuration - logback-spring.xml.

我遇到了类似的问题,很容易解决。基本上,概念是 Spring Boot 已经为您提供了系统属性 -LOG_PATH对于 Spring Boot 属性 -logging.path因此您可以logging.path在 application.properties 中定义并LOG_PATH在您的 logback 配置中简单地使用- logback-spring.xml

You shouldn't declare logback <property ...>for LOG_PATH, just use it whenever you want.

你不应该<property ...>为声明 logback LOG_PATH,只要你愿意就可以使用它。

See at near bottom here

此处查看底部附近

回答by prettyvoid

Might not be your case but if you have bootstrap.propertiesmake sure logging.pathis defined there and only there.

可能不是你的情况,但如果你bootstrap.properties确保logging.path在那里并且只在那里定义。

回答by Dexter Legaspi

if you're on Spring Boot Finchley (2.x), you can define spring.application.namein your application.propertiesor application.ymlfile and add the following in your Logback configuration:

如果您使用的是 Spring Boot Finchley (2.x),您可以spring.application.name在您的application.propertiesapplication.yml文件中定义并在您的 Logback 配置中添加以下内容:

<configuration>
  <springProperty scope="context" name="springAppName" source="spring.application.name"/>
</configuration>

you will now have ${springAppName}as a variable at your disposal for your log pattern.

您现在将拥有${springAppName}一个可供您使用的日志变量pattern

回答by Chaucer

I encountered the same problem. put an entry in logback.xml

我遇到了同样的问题。在 logback.xml 中添加一个条目

<property resource="application.properties" />

In application.properties

在 application.properties 中

FILE_LOG_PATTERN=###
LOG_FILE=###

when your application starts,the name of the directory created is what you have defined in the properties file.

当您的应用程序启动时,创建的目录名称就是您在属性文件中定义的名称。

回答by Johnson

Before Spring Boot enviroment is prepared, the Spring Boot main class or the SpringApplicationwill initialize the loggerfactory, which will detect the default configuration file (logback.groovy, logback.xml, logback-test.xml), but at this time the Spring Boot application is not started yet, which means the variable LOG_PATHis not set. So at first you should alter the name of your logback config file and configure the file name manually in the Spring Boot config as logging.config. By this way the logback will configure a console appender by default instead of creating a file appender, and then when the Spring Boot enviroment is ready it will fire an enviromentready event, which causes a logback reconfig by LoggingApplicationListener. You can find the issue at springboot's page https://github.com/spring-projects/spring-boot/issues/2558

在 Spring Boot 环境准备好之前,Spring Boot 主类或者SpringApplication将初始化 loggerfactory,它会检测默认的配置文件(logback.groovy, logback.xml, logback-test.xml),但是此时 Spring Boot 应用程序还没有启动,这意味着变量LOG_PATH不是放。因此,首先您应该更改 logback 配置文件的名称,并在 Spring Boot 配置中手动将文件名配置为logging.config. 通过这种方式,logback 默认会配置一个控制台 appender,而不是创建一个文件 appender,然后当 Spring Boot 环境准备好时,它会触发一个 environmentready 事件,这会导致 logback 重新配置LoggingApplicationListener。您可以在 springboot 的页面上找到问题https://github.com/spring-projects/spring-boot/issues/2558

回答by anschoewe

I encountered the same problem. I tried to define my own logback.xml and had trouble using the logging.path and logging.file properties defined in my application.properties file. Here is how I resolved (and worked around) the issues.

我遇到了同样的问题。我尝试定义自己的 logback.xml,但在使用 application.properties 文件中定义的 logging.path 和 logging.file 属性时遇到了问题。这是我解决(和解决)这些问题的方法。

First, I learned that you cannot define both logging.path and logging.file properties. Since I'm using a RollingFileAppender that will produce multiple files over multiple days, I define logging.file, but use it more like a file prefix.

首先,我了解到您不能同时定义 logging.path 和 logging.file 属性。由于我使用的是会在多天内生成多个文件的 RollingFileAppender,因此我定义了 logging.file,但使用它更像是一个文件前缀。

In application.properties

application.properties

# Don't add the file type at the end.  This will be added in logback.xml
logging.file=logs/my-app-name

In src/main/resources/logback.xml

在 src/main/resources/logback.xml

<configuration>

    <property name="FILE_LOG_PATTERN" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder> 
          <Pattern>${FILE_LOG_PATTERN}</Pattern>
        </encoder> 
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder> 
          <Pattern>${FILE_LOG_PATTERN}</Pattern>
        </encoder> 
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

This seems to work for the most part. I defined my own FILE_LOG_PATTERN in the file, which I think is optional. The interesting part is the fileNamePattern. It correctly translates logging.file from my application.properties file into the variable LOG_FILE. The only real ugliness here is that on startup Logback still complains about the log file being undefined and creates an empty file called LOG_FILE_IS_UNDEFINED_XXXin the current directory. But the actual log file in my property is created and correctly appended to.

这似乎在大多数情况下都有效。我在文件中定义了我自己的 FILE_LOG_PATTERN,我认为这是可选的。有趣的部分是fileNamePattern。它正确地将我的 application.properties 文件中的 logging.file 转换为变量 LOG_FILE。这里唯一真正丑陋的是,在启动时 Logback 仍然抱怨日志文件未定义并LOG_FILE_IS_UNDEFINED_XXX在当前目录中创建一个空文件。但是我的财产中的实际日志文件已创建并正确附加到。

Andrew

安德鲁

回答by mikelinjie

somewhere Spring is loading the xml before parsing the yml

在解析 yml 之前,Spring 正在某处加载 xml

so just rename logback.xmlto your-logback.xmland add logging.config=classpath:your-logback.xmlin your application.properties

所以只需将logback.xml重命名为your-logback.xml并添加logging.config=classpath:your-logback.xml您的 application.properties

回答by Massimo Da Ros

I had the same problem since I configured logging.path and logging.file on application.properties but some logs was produced before Spring Boot LogBack configuration and so they were written into LOG_PATH_IS_UNDEFINED/LOG_FILE_IS_UNDEFINED file and then the logs switched to the right location.

自从我在 application.properties 上配置 logging.path 和 logging.file 以来,我遇到了同样的问题,但是在 Spring Boot LogBack 配置之前生成了一些日志,因此它们被写入 LOG_PATH_IS_UNDEFINED/LOG_FILE_IS_UNDEFINED 文件,然后日志切换到正确的位置。

I found 2 possible solutions:

我找到了两种可能的解决方案:

1) Configure logging.path and logging.file in bootstrap.properties since the configuration in bootstrap take place before

1) 在 bootstrap.properties 中配置 logging.path 和 logging.file 因为 bootstrap 中的配置发生在

or

或者

2) Set logging.path and logging.file as system properties with -Dlogging.path=... -Dlogging.file=... when launching the application

2) 启动应用程序时,使用 -Dlogging.path=... -Dlogging.file=... 将 logging.path 和 logging.file 设置为系统属性

回答by Ravi Gupta

Do below to create dev/log directory only. Do not add log.pathin application.properties

执行以下操作仅创建 dev/log 目录。不要添加log.pathapplication.properties

Add log.path=dev/logsin your bootstrap.properties.

添加log.path=dev/logs在您的bootstrap.properties.

Add below line in your logback-spring.xml.

在您的 logback-spring.xml 中添加以下行。

<springProperty scope="context" name="LOG_PATH" source="log.path"/>
<property name="LOG_FILE" value="${LOG_PATH}/app/current"/>

NoteMake sure you include the below line only.

注意请确保仅包含以下行。

<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

Do not use the below line else console logs will never be disabled and spring.log file will be created in temp directory(if you dont not provide logging.path in application.properties). The check the code of below file to know more.

不要使用下面的行,否则控制台日志永远不会被禁用,并且 spring.log 文件将在临时目录中创建(如果你没有在 application.properties 中提供 logging.path)。检查以下文件的代码以了解更多信息。

 <include resource="org/springframework/boot/logging/logback/base.xml"/>