How to define log4j2 path by application.properties?

java, log4j, log4j2, spring

Solution

I solved it as follows: log4j2.xml: `${main:spring.profiles.active}`

MainMapLookup.setMainArguments(new String[] {"spring.profiles.active", "dev"});
SpringApplication.run(source, args);

You can get the vmargs as follows, and set the profile dynamically before running the spring app: `ManagementFactory.getRuntimeMXBean().getInputArguments()`

Or even better, coming back to this after years: use `${sys:spring.profiles.active}`, as any arguments given with `-D` count as SystemProperties. You don't need the `MainMapLookup` in this case.

As an alternative: just logging to the classpath `logs` dir, and setting a soft link inside the execution directory, eg `ln -s /var/log logs` to redirect the log directory by the running system.

Problem

I want to have different `log4j2` log directories based on the current active profile. But it does not work. ``` #application.properties: spring.profiles.active=dev log.path=d:/${spring.profiles.active} #log4j2.xml: <Properties> <property name="path">${bundle:application:log.path}</property> </Properties> ``` Result: a folder is created on d:/ called `${spring.profiles.active}` instead of resolving to the real spring profile name. Why?

Original source