[logback-user] how to add substitutionProperties in code

Tom Eyckmans teyckmans at gmail.com
Fri Feb 29 00:15:47 CET 2008


Hi,

Is it possible to use the substitution property mechanism from code when
initializing logback?

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.shutdownAndReset();

       // Can't set the property here because the execution context is null

        configurator.doConfigure(contextProperties.getProperty("
logging.properties"));

        configurator.getExecutionContext().addSubstitutionProperty("
service.name", "serviceOne"); // I think this is to late to set the property
and it isn't used anymore
}
catch (JoranException je) {
      StatusPrinter.print(lc);
}

with configuration file:
<?xml version="1.0" encoding="UTF-8"?>

<configuration>
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M [%line] -
%msg%n</pattern>
    </layout>
  </appender>
  <appender name="A2" class="ch.qos.logback.core.rolling.RollingFileAppender
">
    <file>log/${service.name}.log</file>
    <Append>true</Append>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <pattern>>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M [%line] -
%msg%n</pattern>
    </layout>
    <triggeringPolicy class="
ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>10MB</MaxFileSize>
    </triggeringPolicy>
    <rollingPolicy class="
ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <FileNamePattern>log/${service.name}.%i.log.zip</FileNamePattern>
      <MinIndex>1</MinIndex>
      <MaxIndex>10</MaxIndex>
    </rollingPolicy>
  </appender>
  <root>
    <level value="INFO"/>
    <appender-ref ref="console"/>
  </root>
</configuration>

making every service that reuses this configuration but log to it's own
logfile.

Thanking you in advance,

Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://qos.ch/pipermail/logback-user/attachments/20080229/08d904b1/attachment.htm 


More information about the Logback-user mailing list