Skip to content

logging.path(define in application.yml) doesn't load in logback-spring.xml initially #18306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maoqyhz opened this issue Sep 23, 2019 · 5 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@maoqyhz
Copy link

maoqyhz commented Sep 23, 2019

I need to configure logback in xml file, so i create logback-spring.xml and application.yml. I define logging.path in the yml file.

# application.yml
logging:
  path: D:/logs

then, use the variable in logback-spring.xml.As we known from offical document, some other properties are transferred from the Spring Environment to System properties.logging.path corresponds to LOG_PATH.

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${LOG_PATH}/runtime.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <maxFileSize>200MB</maxFileSize>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="FILE_ALL"/>
    </root>
</configuration>

then ,i run the application.There will be two log fiile generated, one is in ${logging.path} directory, the other is in a folder named "LOG_PATH_IS_UNDEFINED" generated in. .This does not match my expectations.

And i try to rename the application.yml to application.properties, only one log file generate in the right folder.So as the document says:

Because the standard logback.xml configuration file is loaded too early, you cannot use extensions in it. You need to either use logback-spring.xml or define a logging.config property.

In other words, logback-spring.xml will load after application.properties. But why i change the properties to yml file, the results will be different? So i think maybe this is a bug.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 23, 2019
@wilkinsona
Copy link
Member

Things should behave the same with application.properties or application.yml so it sounds like something is wrong. I can't tell from your description if that's due to a bug in Boot or if something isn't quite right in your application. To help us figure that out, can you please create a complete, minimal sample that reproduces the behaviour you have described and zip it up and attach it to this issue?

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Sep 23, 2019
@maoqyhz
Copy link
Author

maoqyhz commented Sep 23, 2019

It's my fault. But there is another problem. I test the logback in pure Spring Boot project, the config work as i excepted. Once i add Spring Cloud dependency, maybe someting wrong.

This is the Minimal Sample

Still the same config, in the sample ,i add a Eureka dependency,the logback config may occur unexcepted results which generate LOG_PATH_IS_UNDEFINED folder in .

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 23, 2019
@wilkinsona
Copy link
Member

Thanks for the additional information. There are some known problems with Spring Cloud's interaction with Spring Boot's logging system. I suspect that the problem you are seeing is related to spring-cloud/spring-cloud-commons#608. This almost certainly needs to be looked at by the Cloud team, but lets leave this issue here for now and I'll try to determine if this is the same problem as spring-cloud/spring-cloud-commons#608 or a different one.

@maoqyhz
Copy link
Author

maoqyhz commented Sep 23, 2019

OK, thx anyway!
And then, i try to debug the souce code, maybe something wrong in LoggingApplicationListener's initialize(ConfigurableEnvironment environment, ClassLoader classLoader).When calling initialize() method, logFile will get from environment, but the path value of PATH_PROPERTY is null.

	public static LogFile get(PropertyResolver propertyResolver) {
		String file = propertyResolver.getProperty(FILE_PROPERTY);
		String path = propertyResolver.getProperty(PATH_PROPERTY);
		if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) {
			return new LogFile(file, path);
		}
		return null;
	}

If i add some parameters when spring boot application start, eg:

java -jar xxx.jar --logging.path=F:\logs

the path value is right value.

maybe the load order is wrong. The initialize() should execute after loading the application.yml file.

@wilkinsona
Copy link
Member

Thanks, that's helped to remind me of exactly what's happening here.

This isn't a bug in Boot or Cloud, it's expected behaviour due to Spring Cloud's bootstrap context and its requirements for logging configuration. In short, you need to move the configuration of logging.path into bootstrap.properties or bootstrap.yml.

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants