Skip to content
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

Ability to customize log file name when logging.path is set #16231

Open
xak2000 opened this issue Mar 14, 2019 · 8 comments
Open

Ability to customize log file name when logging.path is set #16231

xak2000 opened this issue Mar 14, 2019 · 8 comments
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Milestone

Comments

@xak2000
Copy link
Contributor

xak2000 commented Mar 14, 2019

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.

public void applyTo(Properties properties) {
put(properties, LoggingSystemProperties.LOG_PATH, this.path);
put(properties, LoggingSystemProperties.LOG_FILE, toString());
}

public String toString() {
if (StringUtils.hasLength(this.file)) {
return this.file;
}
return new File(this.path, "spring.log").getPath();
}

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).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 14, 2019
@mbhave
Copy link
Contributor

mbhave commented Mar 15, 2019

I think this would be a generally useful feature. Let's see what the rest of the team thinks.

@mbhave mbhave added the for: team-attention An issue we'd like other members of the team to review label Mar 15, 2019
@wilkinsona
Copy link
Member

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.

@mbhave mbhave added type: enhancement A general enhancement status: pending-design-work Needs design work before any code can be developed and removed for: team-attention An issue we'd like other members of the team to review status: pending-design-work Needs design work before any code can be developed labels Mar 20, 2019
@mbhave mbhave added this to the 2.x milestone Mar 20, 2019
@mbhave mbhave added status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 20, 2019
@rhamedy
Copy link
Contributor

rhamedy commented Mar 23, 2019

Depending on the outcome of the design work as well as complexity of the fix, I would be happy to work on this issue 🙂

@ckoutsouridis
Copy link

ckoutsouridis commented Sep 15, 2019

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.

@jwenjian

This comment has been minimized.

@mbhave

This comment has been minimized.

@pgerhard
Copy link

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

@philwebb philwebb modified the milestones: 2.x, 3.x Aug 19, 2022
@m4nh6n
Copy link

m4nh6n commented Jan 14, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

10 participants