With the current implementation, when logging.path is set to some value and logging.file is not set, the logging file name is always spring.log.
https://github.com/spring-projects/spring-boot/blob/c115ee1550ecf4bc8164e59a4f69b59addd50cc6/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java#L105-L108
https://github.com/spring-projects/spring-boot/blob/c115ee1550ecf4bc8164e59a4f69b59addd50cc6/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java#L117-L122
I want to be able to set logging.path for several microservices at once, using environment variable for example:
LOGGING_PATH=/var/log/services
This works, but all services start to write their logs into signle /var/log/services/spring.log file.
To prevent this, I added custom logback-spring.xml to each application and also one logback-base.xml into common module (that all microservices depend on).
I added these lines into logback-base.xml:
<included>
<springProperty scope="context" name="APPLICATION_NAME" source="spring.application.name"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${java.io.tmpdir:-/tmp}/services}/${APPLICATION_NAME:-service}.log}"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</included>
This is how logback-spring.xml looks in each application:
<configuration>
<include resource="logback-base.xml"/>
</configuration>
The idea is to store all logs in the same path, but each log file have it's own name, based on spring.application.name.
But this doesn't work when only logging.path is set, because LogFile class always explicitly sets LOG_FILE system property, using it's toString() method and adding spring.log as file name.
Maybe LogFile can be enhanced to allow to set some strategy that provides an ability to customize log file name (when it is not explicitly set)? Some LogFileNameCustomizer bean or something.
Or maybe at least we can make spring.application.name to be default log file name if this property is set and logging.file is not set? (this is breaking change though, but it can be hidden under some flag).
Comment From: mbhave
I think this would be a generally useful feature. Let's see what the rest of the team thinks.
Comment From: wilkinsona
This sounds useful to me too.
Some LogFileNameCustomizer bean or something.
This approach won't be possible as logging configuration is needed before any beans are available.
Comment From: rhamedy
Depending on the outcome of the design work as well as complexity of the fix, I would be happy to work on this issue 🙂
Comment From: ckoutsouridis
in our case, we created a an extra application listener and registered it under: META-INF/spring.factories
org.springframework.context.ApplicationListener=\
com.OurListener
the content of the listener was something along:
private void onApplicationEnvironmentPreparedEvent(
ApplicationEnvironmentPreparedEvent event) {
Environment environment = event.getEnvironment();
String springApp = environment.getProperty("spring.application.name");
String applicationName = springApp == null ? "application" : springApp;
if (environment.getProperty("logging.path") == null) {
System.setProperty("logging.path", "logs");
}
if (environment.getProperty("logging.file") == null) {
String logFileName = applicationName + ".log";
System.setProperty("logging.file", environment.getProperty("logging.path") + "/" + logFileName);
}
and the order of the listener was: LoggingApplicationListener.DEFAULT_ORDER - 1;
the main motivation for the above was to have a property logging.path that would point to a folder, so that we could use it as well in configuring logback-access to write in the same directory without duplicating the path.
Since this is a breaking change with existing semantics and i think in 2.2 the properties will be renamed to logging.file.name and logging.file.path i think it's a nice excuse to apply this behaviour in the new properties as defaults and as @xak2000 suggested, make spring.application.name the default file name instead of spring.
Comment From: jwenjian
I finally know that why my logging.file.name not working, I'm using 2.1.11.
But there is no version description in spring doc: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html
So maybe put a new "Version since"column in the right of the table to do this?
Comment From: mbhave
@jwenjian The docs are tied to the version of Spring Boot. https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html points to the current GA release which at the time of writing is the 2.2.x line. You can get the 2.1.x docs, by going to https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/common-application-properties.html.
Comment From: pgerhard
I believe this ticket is no longer required. Using logging.file.name a path and a file name can be set.
Example: /logs/${spring.application.name}.log
Comment From: m4nh6n
There is still in my opinion a somewhat misleading behavior with logging.file.name and logging.file.path:
They are mutually exclusive. It's either the name (which can actually also be an absolute path), or the path (which then uses a default file name).
But you cannot use a combination of both, for instance to set an output directory with logging.file.path and to use then an actual file name in logging.file.name which would log to that file in that directory.
Wouldn't it make sense to support this? Or at least mention it in the docs and issue a warning if people configure both properties?