usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

Retrieve a log4j2 configuration property at runtime (if defined)

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?

Upvotes: 1

Views: 1953

Answers (2)

Daniel Bubenheim
Daniel Bubenheim

Reputation: 4229

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");

Upvotes: 4

rgoers
rgoers

Reputation: 9141

Map<String, String> properties = ((LoggerContext) LogManager.getContext(false)).getConfiguration().getProperties();

Upvotes: 2

Related Questions