1

Our standard log4j2.xml files for our application include a configuration-time property

<Properties>
    <Property name="log-path">/some/writable/path/on/server</Property>
</Properties>

And then referenced in the appenders. Works fine so far.

I need to get the value of this property, which is not known at compile time, and not even guaranteed to be defined, at runtime.

From some facility, I need to get the "log-path" property if it's defined. Otherwise null is a great return value.

How can I accomplish this task?

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
  • Possible duplicate of [Log4J2 - assigning file appender filename at runtime](http://stackoverflow.com/questions/14862770/log4j2-assigning-file-appender-filename-at-runtime) – Jordi Castilla Jun 07 '16 at 10:44
  • Thanks for linking the other question. Unfortunately it's only partially helpful. Surely if I define the "log-path" property as system property I will be able to easily read from both Java application and log4j, but then I need to open a discussion with the people who release and configure the software. Not an easy task. I will consider such a change but I have been explicitly requested to read that property from the log4j config file – usr-local-ΕΨΗΕΛΩΝ Jun 07 '16 at 10:49
  • 1
    @usr-local-ΕΨΗΕΛΩΝ btw its ΕΨΙΛΟΝ not ΕΨΗΕΛΩΝ :) a friend from Greece – Sir. Hedgehog Jun 20 '16 at 09:56
  • (off topic) there is an ancient legend about this typing, use greek keyboard layout in windows with a physical QWERY keyboard to type ECHELON - replace o-micron (small) with o-mega (big) :-) :-) :-) – usr-local-ΕΨΗΕΛΩΝ Jun 20 '16 at 10:43

2 Answers2

4
final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(true);
final Configuration config = loggerContext.getConfiguration();
final StrSubstitutor strSubstitutor = config.getStrSubstitutor();
final StrLookup variableResolver = strSubstitutor.getVariableResolver();
final String propertyValue = variableResolver.lookup("propertyName");
Daniel Bubenheim
  • 4,043
  • 3
  • 14
  • 20
2
Map<String, String> properties = ((LoggerContext) LogManager.getContext(false)).getConfiguration().getProperties();
rgoers
  • 8,696
  • 1
  • 22
  • 24