[logback-user] Access Properties Defined in Logback programmatically

ceki ceki at qos.ch
Fri Jan 11 23:30:50 CET 2013


Hi Charlie,

By default, properties are defined in "local" scope [1]. 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");

HTH,

[1] http://logback.qos.ch/manual/configuration.html#scopes

On 11.01.2013 22:43, Charlie Hubbard wrote:
> 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 calls should I be using?
>
>
>
> Thanks
>
>
> Charlie
>
> If you'd rather have the stackoverflow karma I originally posted this there:
>
>
> http://stackoverflow.com/questions/14271253/access-properties-defined-in-logback-programmatically


-- 
Ceki
65% of statistics are made up on the spot


More information about the Logback-user mailing list