3

I want to use restart timestamp in log file name, can I get it somehow in properties file?

Current implementation:

logging.file.name=abc_services-${PID}.log

Something like below should be helpful:

logging.file.name=abc_services-${PID}-${timestamp}.log

Other ways to achieve are:

  1. Using system property LOG_FILE and set file name having timestamp
  2. Using logback-spring.xml file and use the way explained in this answer.

But achieving this in property file is what I find nice approach. Spring boot version 2.6.5

Vipin
  • 4,851
  • 3
  • 35
  • 65
  • 2
    Unless you have some environment property named timestamp you cannot do that. PID exists as an environment variable. You have others like java variables, and in windows/Linux specific OS variables. The alternative is to use file name pattern for rolling policy. In this case you can use %d{yyy-MM-dd}, but only the "rolled" files will have this pattern. But probably the best approach is to use a log configuration file as you have in your question. – pringi Mar 29 '22 at 12:07

2 Answers2

0

I prefer the standard spring logback xml mechanism as you have mentioned in your question.

I tried following with latest demo spring web project from https://start.spring.io/

  1. I followed Add Timestamp Variable to Folder Path Value in Application Properties. At startup I set the timestamp and accessed the same in properties. But this will fix the file name till server restarts.

example filename: abc_services-52855-1648696549862.log

I set the timestamp variable as below. We can control the format as required and there are multiple ways to set the environment variable.

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
    System.setProperty("timestamp",String.valueOf(System.currentTimeMillis()));

    SpringApplication.run(DemoApplication.class, args);
    }
}
  1. Instead of generating custome time stamp we can use maven build timestamp

    logging.file.name=abc_services-${PID}-@maven.build.timestamp@.log

  2. If application running in Docker/Cloud environment/Container, it should have an environment variable storing the startup time. We can use that time stamp to define the timestamp in file names

Sachin
  • 51
  • 6
  • Your 1st Approach is same as I have given in question and second approach has build time, what I need to application restart time. So overall so far it is not answer to question. – Vipin Apr 04 '22 at 09:46
  • updated question with restart time – Vipin Apr 06 '22 at 15:10
-1

The intended way here is probably to use a logback.xml with an appropriate FileNamePattern as described here.

kistlers
  • 97
  • 5
  • 1
    I have specifically mentioned this way of fix in question, link is also given in case anyone want to see how implement this, thanks for the your attention but this answer is part of question as well. – Vipin Mar 30 '22 at 14:58