I have an administration console I'm building, and I want to display the logs created in Logback
for my application. However, where those logs are stored is different per environment. I have several property files that define where the logs are stored:
<configuration>
<property resource='log.properties'/>
<property resource='log.${ENV:-prod}.properties'/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/sync.log</file>
...
</configuration>
I'd like to find the value of ${log.dir}
from Logback's
Java API. I'd tried the following, but it doesn't have any of the properties defined in the resources. For example:
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
String logDir = loggerContext.getProperty("log.dir"); // this always returns null
So my question is what API should I be using?
By default, properties are defined in "local" scope. However, you can force a property to have context scope, in which case it's pretty easy to get the value of the property:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
String val = lc.getProperty(aString);
Defining properties in "context" scope is considered a little heavy handed. Instead of defining all/many properties in context scope, you could define only a single property in context scope. Here is an example:
<configuration>
<!-- get many props from a file -->
<property resource='log.properties'/>
<-- set only one to be in context scope -->
<property scope="context" name="log.dir.ctx" value="${log.dir}" />
...
</configuration>
You could then obtain the value you are looking for with:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
String val = lc.getProperty("log.dir.ctx");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With