spring-bootlog4j2spring-logback

How to use system property variable in log4j2.xml in spring boot using lombok log?


I am having a CommandLineRunner Spring Boot applicatio nwith Sytem property log_prefix.

I am currently using a log24j.xml for setting the log configurations. It is taking the filename etc from the <properites> tag in the log24j.xml. Is there a way to change the log fileName to log_prefix_LogFileName ?

My Application.class

@SpringBootApplication
public class Application implements CommandLineRunner {
    static Logger log = Logger.getLogger(Application.class.getName());
    public static void main(String[] args) {
        System.setProperty("log_prefix",args[0]);// Tried hardcoding a string value
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.run(args);
    } 

I tried setting runID in Application.class and using ${log_prefix} in log42j.xml but not working.

Pom.xml log4j2 dependency.

 <log4j2.version>2.15.0</log4j2.version>

I am using @Slf4j lombok annotaion over the class

import lombok.extern.slf4j.Slf4j;
@Slf4j

Solution

  • As Piotr P Karwaz pointed out my log config file had ${log_prefix} , to get system properties in spring boot log configuration log4j2.xml file we need to prefix sys: the property name in curly braces. ${sys:log_prefix} fixed the issue.

    note this is specific to Spring boot, for normal java application you can refer this answer